CF BUDDY
← Problems·

1830C · Hyperregular Bracket Strings

2400 · combinatorics, greedy, hashing

Problem: You are given an integer nn and kk intervals. The ii-th interval is [li,ri][l_i,r_i] where 1lirin1 \leq l_i \leq r_i \leq n.

Let us call a regular bracket sequence,^{\dagger,\ddagger} of length nn hyperregular if for each ii such that 1ik1 \leq i \leq k, the substring slisli+1sri\overline{s_{l_i} s_{l_{i}+1} \ldots s_{r_i}} is also a regular bracket sequence.

Your task is to count the number of hyperregular bracket sequences. Since this number can be really large, you are only required to find it modulo 998244353998\,244\,353.

^\dagger A bracket sequence is a string containing only the characters "(" and ")".

^\ddagger A bracket sequence is called regular if one can turn it into a valid math expression by adding characters + and 1. For example, sequences (())(), (), (()(())) and the empty string are regular, while )(, ((), and (()))( are not.

Input Format: Each test contains multiple test cases. The first line of input contains a single integer tt (1t1051 \le t \le 10^5) — the number of test cases. The description of test cases follows.

The first line of each test case contains two integers nn and kk (1n31051 \le n \le 3 \cdot 10^5, 0k31050 \le k \le 3 \cdot 10^5) — the length of the hyperregular bracket sequences and the number of intervals respectively.

The following kk lines of each test case contains two integers lil_i and rir_i (1lrn1 \le l \le r \le n).

It is guaranteed that the sum of nn across all test cases does not exceed 31053 \cdot 10^5 and the sum of kk across all test cases does not exceed 31053 \cdot 10^5.

Output Format: For each test case, output the number of hyperregular bracket sequences modulo 998244353998\,244\,353.

Note:

  • For the first testcase, the 55 hyperregular bracket strings of length 66 are: ((())), (()()), (())(), ()(()) and ()()().
  • For the second testcase, there are no regular bracket strings of length 55, and consequently, there are no hyperregular bracket strings of length 55.
  • For the third testcase, there are no hyperregular bracket strings of length 88 for which the substring [13][1 \ldots 3] is a regular bracket string.
  • For the fourth testcase, there 44 hyperregular bracket strings are: ((())(())), ((())()()), ()()((())) and ()()(()())

Sample Cases

Case 1

Input

7
6 0
5 0
8 1
1 3
10 2
3 4
6 9
1000 3
100 701
200 801
300 901
28 5
1 12
3 20
11 14
4 9
18 19
4 3
1 4
1 4
1 4

Output

5
0
0
4
839415253
140
2

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback