Counting Is Not Fun (Easy Version)
Time limit3sMemory limit2048 MB
After each clue fixing a good pair, count the balanced bracket sequences with n good pairs that contain all clues so far, modulo 998244353.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Combinatorics, String
- Solved
- No attempts yet
Statement
This is the easy version of the problem. The only difference is that this version has smaller limits on and . You can hack only if you solved all versions of this problem.
Now Little John is rich, so he finally buys a house big enough to fit himself and his favorite bracket sequence. But somehow he ended up with a lot of brackets! Frustrated, he penetrates the ceiling with the "buddha palm".
A bracket sequence is called balanced if it can be constructed by the following grammar.
- The empty sequence is balanced.
- If the bracket sequence is balanced, then is also balanced.
- If the bracket sequences and are balanced, then the concatenation is also balanced.
For example, "(())()", "()", "(()(()))", and the empty sequence are balanced, while "(()" and "(()))(" are not.
Given a balanced bracket sequence , a pair of indices with is called a good pair if is (, is ), and the two brackets were added together by Rule 2 while building . For example, "(())()" has three good pairs: , , and . Every balanced bracket sequence of brackets has exactly good pairs, and any order of applying the rules to build the same sequence gives the same set of good pairs.
Emily plays a bracket guessing game with John. John has a balanced bracket sequence with good pairs, and Emily does not know it. John tells Emily the value of and asks her to guess the sequence. Over turns, John gives one clue per turn:
- : the sequence contains the good pair .
The clues are pairwise distinct and do not contradict each other.
At some point Emily can be certain that only one balanced bracket sequence matches the clues so far. For example, suppose Emily knows has good pairs and contains the good pair . Of the balanced bracket sequences with good pairs, only "((()))" contains , so Emily does not need all turns.
After each turn, Emily wants to know how many balanced bracket sequences match the clues so far. Counting these is hard when there are many good pairs. Help her: before and after each turn, find the count modulo .
Input
Each test contains multiple test cases. The first line contains (), the number of test cases. The first line of each test case contains (), the number of good pairs. Then lines follow, and the -th of them contains two integers and (), the -th clue.
The clues in one test case are pairwise distinct and do not contradict each other. The sum of over all test cases does not exceed .
Output
For each test case, output integers on one line. The first integer is the count before any clue. For each , the -th integer is the count after the -th clue. All values are modulo .
Hints
The first test case of the sample is explained in the statement. The third test case is explained as follows. There are balanced bracket sequences with good pairs. After the first clue , there are sequences with that good pair. After the second clue , there are sequences with good pairs and . After the third clue , there are sequences with those three good pairs: "(()())()(())" and "(()())()()()". After the fourth clue , only "(()())()(())" remains. The counts after the fifth and sixth clues are both , since the content of is already known.