Counting Bracket Sequences

View as PDF

Submit solution


Points: 0.10 (partial)
Time limit: 5.0s
Memory limit: 512M
Input: stdin
Output: stdout

Author:
Problem type
Allowed languages
C, C++, Go, Java, Kotlin, Pascal, PyPy, Python, Rust, Scratch

You are given a positive even integer ~n~, and ~k~ pairs of numbers ~(l_i, r_i)~ satisfying the condition ~1 \le l_1 < r_1 < l_2 < r_2 < \ldots < l_k < r_k \le n~. Count the number of valid bracket sequences~^\dagger~ ~s~ of length ~n~ satisfying the following condition, modulo ~998\,244\,353~:

  • For every ~1 \le i \le k~, ~s_{l_i} \neq s_{r_i}~.

~^\dagger~ A string ~s~ formed from two types of characters ~\mathtt{(}~ and ~\mathtt{)}~ is called a valid bracket sequence if ~s~ satisfies one of the following conditions:

  • ~s~ is empty.

  • ~s~ has the form "~\mathtt{(} t \mathtt{)}~", where ~t~ is also a bracket sequence.

  • ~s~ has the form "~ab~", where ~a~ and ~b~ are two valid bracket sequences.

Input

Each test contains multiple test cases. The first line contains the number of test cases ~t~ (~1 \leq t \leq 10\,000~). The description of each test case is as follows.

The first line contains two integers ~n~ and ~k~ (~2 \le n \le 2 \cdot 10^5~, ~n~ is even, ~0 \le k \le \frac{n}{2}~) — the length of string ~s~ and the number of given pairs.

The ~i~-th line in the next ~k~ lines contains two integers ~l_i~ and ~r_i~ representing the ~i~-th given pair. It is guaranteed that the condition ~1 \le l_1 < r_1 < l_2 < r_2 < \ldots < l_k < r_k \le n~ holds.

The sum of ~n~ over all test cases is guaranteed not to exceed ~2 \cdot 10^5~.

Output

For each test case, output an integer which is the number of valid strings ~s~ satisfying the given condition, modulo ~998\,244\,353~.

Scoring

Subtask Score Constraints
1 ~1000~ The sum of ~n~ does not exceed ~2000~
2 ~1500~ ~l_i + 1 = r_i~ for every ~1 \le i \le k~
3 ~1500~ No additional constraints
Total ~4000~

Sample Input 1

5
6 0
4 2
1 2
3 4
8 1
1 8
8 2
1 3
5 8
20 4
2 8
9 10
15 17
19 20

Sample Output 1

5
1
14
3
692

Notes

In the first test case, valid bracket sequences are ~\mathtt{((()))}~, ~\mathtt{(()())}~, ~\mathtt{(())()}~, ~\mathtt{()(())}~, ~\mathtt{()()()}~.

In the second test case, the only valid bracket sequence is ~\mathtt{()()}~.

In the fourth test case, valid bracket sequences are ~\mathtt{(())(())}~, ~\mathtt{(())()()}~, ~\mathtt{(()(()))}~.


Comments

Please read the guidelines before commenting.


There are no comments at the moment.