#P16958. [SCCPC 2026] 括号序列

    ID: 19042 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>递推四川组合数学2026省赛/邀请赛

[SCCPC 2026] 括号序列

Problem Description

For strings SS and TT, define that SS is lexicographically smaller than TT if and only if one of the following holds:

  • SS is the empty string (length 00), and TT is non-empty;
  • SS and TT are non-empty, and S[0]S[0] is lexicographically smaller than T[0]T[0], where S[0]S[0] and T[0]T[0] denote the first character of SS and TT;
  • SS and TT are non-empty, and S[0]=T[0]S[0]=T[0], and S[1,]S[1,\cdots] is lexicographically smaller than T[1,]T[1,\cdots], where S[1,]S[1,\cdots] and T[1,]T[1,\cdots] denote the strings obtained by deleting the first character from SS and TT.

For a bracket string SS consisting of {‘(’,‘)’}\texttt{\{`(',`)'\}}, call it matchable if and only if one of the following holds:

  • SS is the empty string (length 00);
  • S=(A)S = (A), where AA is a matchable bracket string;
  • S=ABS = AB, where both AA and BB are non-empty matchable bracket strings.

Now a matchable bracket string TT is given. Find how many bracket strings SS satisfy all of the following four conditions:

  • SS is not the empty string (length >0> 0);
  • SS is matchable;
  • S=TS=T, or SS is lexicographically smaller than TT;
  • The length of SS is less than or equal to the length of TT.

Note that the character ‘(’\texttt{`('} is lexicographically smaller than ‘)’\texttt{`)'}.

This problem uses multiple test cases. The answer may be very large, so output the result modulo 998244353998244353.

Input Format

The first line of each test file contains a positive integer tt (1t5×1051 \le t \le 5\times 10^5), the number of test cases.

Then follow tt test cases. For each test case, the first line contains an even integer nn (2n1062\le n\le 10^6), the length of the string TT.

The second line contains a bracket string of length nn over {‘(’,‘)’}\texttt{\{`(',`)'\}}. It is guaranteed that TT is matchable.

It is guaranteed that the sum of lengths of all bracket strings in a single test file does not exceed 10610^6.

Output Format

Output tt lines. The ii-th line should contain the answer for the ii-th test case modulo 998244353998244353.

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 SS:

  • ()\texttt{()}
  • (())\texttt{(())}
  • ()(())\texttt{()(())}
  • (())()\texttt{(())()}
  • ((()))\texttt{((()))}
  • (()())\texttt{(()())}

Translated by ChatGPT 5