#P10600. BZOJ4350 括号序列再战猪猪侠

BZOJ4350 括号序列再战猪猪侠

Background

This problem comes from the original BZOJ. We acknowledge that the copyright of the statement and the original testdata belongs to the original BZOJ or the problem setter who authorized BZOJ to use it. If you are the copyright owner and believe that we have infringed your rights, please contact us.

Problem Description

A bracket sequence is a sequence consisting only of () . The following bracket sequences are valid:

  1. () is a valid sequence.
  2. If A is a valid sequence, then (A) is also a valid sequence.
  3. If both A and B are valid sequences, then AB is also a valid sequence.

Define matchimatch_i as follows: counting from left to right, for the ii-th left parenthesis, matchimatch_i is the index (among right parentheses, from left to right) of the right parenthesis that matches it.

Now you are given a bracket sequence of length 2n2n. You are also given mm pieces of information. The ii-th piece is of the form ai,bia_i, b_i, meaning matchai<matchbimatch_{a_i} < match_{b_i}.

Ask: based on this information, how many ways are there to restore a valid bracket sequence? Output the answer modulo 998244353998244353.

Input Format

The first line contains a positive integer TT, the number of test cases.

For each test case, the first line contains two positive integers n,mn, m. Here nn is the number of left parentheses, and mm is the number of constraints.

The next mm lines each contain two positive integers ai,bia_i, b_i.

Output Format

For each test case, output one integer representing the answer.

5
1 0
5 0
3 2
1 2
2 3
3 2
2 1
2 3
3 3
1 2
2 3
3 1
1
42
1
2
0

Hint

For all test cases, it is guaranteed that 1T51 \leq T \leq 5, 1n3001 \leq n \leq 300, and 1ai,bin1 \leq a_i, b_i \leq n.

Translated by ChatGPT 5