#P16958. [SCCPC 2026] 括号序列
[SCCPC 2026] 括号序列
Problem Description
For strings and , define that is lexicographically smaller than if and only if one of the following holds:
- is the empty string (length ), and is non-empty;
- and are non-empty, and is lexicographically smaller than , where and denote the first character of and ;
- and are non-empty, and , and is lexicographically smaller than , where and denote the strings obtained by deleting the first character from and .
For a bracket string consisting of , call it matchable if and only if one of the following holds:
- is the empty string (length );
- , where is a matchable bracket string;
- , where both and are non-empty matchable bracket strings.
Now a matchable bracket string is given. Find how many bracket strings satisfy all of the following four conditions:
- is not the empty string (length );
- is matchable;
- , or is lexicographically smaller than ;
- The length of is less than or equal to the length of .
Note that the character is lexicographically smaller than .
This problem uses multiple test cases. The answer may be very large, so output the result modulo .
Input Format
The first line of each test file contains a positive integer (), the number of test cases.
Then follow test cases. For each test case, the first line contains an even integer (), the length of the string .
The second line contains a bracket string of length over . It is guaranteed that is matchable.
It is guaranteed that the sum of lengths of all bracket strings in a single test file does not exceed .
Output Format
Output lines. The -th line should contain the answer for the -th test case modulo .
6
2
()
4
()()
4
(())
6
()(())
8
(())(())
20
((()())(()())(()()))
1
3
1
6
11
7346
Hint
For the fourth test case, there are the following six valid strings :
Translated by ChatGPT 5