QOJ.ac

QOJ

时间限制: 2 s 内存限制: 512 MB 总分: 100 可 Hack ✓

#20239. Divide and Conquer

统计

Segment tree is a data structure based on the divide-and-conquer algorithm, but this has nothing to do with this problem---it's just to make the title start with D.

For this problem, a segment tree is a perfect binary tree. A segment tree of depth $n$ has exactly $2^{n+1}-1$ nodes. Note that the root has depth $0$, so a segment tree of depth $n$ has $n+1$ levels.

To make the structure of a segment tree easier to describe, we assign indices to its nodes. The root is assigned index $1$. Every non-leaf node is assigned an index $k$ satisfying $1\leq k< 2^n$. It has exactly two children: its left child has index $2k$, and its right child has index $2k+1$.

Each node of a segment tree also has a value. Let $a_k$ denote the value of node $k$. For every non-leaf node $k$, its value $a_k$ must be the maximum of the values of its two children; that is, $$ a_k=\max\{a_{2k},a_{2k+1}\}. $$

The leaf nodes have indices $2^n,2^n+1,\ldots,2^{n+1}-1$, and their values must form a permutation of $1,2,\ldots,2^n$. In other words, the elements of the sequence $a_{2^n},a_{2^n+1},\ldots,a_{2^{n+1}-1}$ must be pairwise distinct integers, and $1\leq a_{2^n+i}\leq 2^n$ must hold for every $0\leq i< 2^n$.

There are now $q$ constraints. The $i$-th constraint is of the form $(u_i,x_i)$. Here, $u_i$ is a node index and $x_i$ is a specified value; the constraint requires the value $a_{u_i}$ of node $u_i$ to be equal to $x_i$. Calculate the number of segment trees satisfying all $q$ constraints, or equivalently, the number of distinct valid sequences $a_1,a_2,\ldots,a_{2^{n+1}-1}$. Output the answer modulo $998244353$.

Input

The first line contains two integers $n$ $(1\le n\le 18)$ and $q$ $(0\le q\le 2^{n+1}-1)$.

Each of the next $q$ lines contains two integers $u_i$ $(1\le u_i< 2^{n+1})$ and $x_i$ $(1\le x_i\le 2^n)$, meaning that the value $a_{u_i}$ of node $u_i$ is required to be equal to $x_i$.

Output

Output one integer: the number of valid leaf permutations modulo $998244353$.

Examples

Input 1

2 1
2 2

Output 1

4

Input 2

2 2
1 4
2 4

Output 2

12

Input 3

2 2
2 3
4 4

Output 3

0

Input 4

4 2
1 2
1 3

Output 4

0

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#2704EditorialOpen题解KobicGend2026-09-14 13:09:50View

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.