#P17460. [GESP202609 七级] 括号序列

[GESP202609 七级] 括号序列

Problem Description

For strings SS and TT, if deleting any number of characters from SS can produce TT, then TT is a subsequence of SS. In other words, TT is formed by selecting some characters from SS and concatenating them in index order. Two subsequences are different if and only if the sets of chosen indices are different.

For example, sun is a subsequence of sequence, because deleting eq, e, and ce from sequence can produce sun. The string sequence has 282^8 different subsequences, including the empty string. It also has three different subsequences e, because the 22nd, 55th, and 88th characters of sequence are all e, and keeping each of these characters produces a different subsequence.

For a string SS, if SS satisfies the following conditions, then SS is a valid bracket sequence:

  • SS is the empty string, or
  • SS can be obtained by concatenating (, a valid bracket sequence, and ), or
  • SS can be obtained by concatenating two valid bracket sequences.

For example, (), ()(), (()), and (()()) are all valid bracket sequences. But (() and )( are not valid bracket sequences.

Given a string SS of length nn that contains only ( and ), find how many of the 2n2^n subsequences of SS are valid bracket sequences. Since the answer may be very large, output the result modulo 10910^9.

For example, when SS is ))(()(, there are 33 subsequences that are valid bracket sequences: the empty string and two different subsequences ().

Input Format

The first line contains a positive integer nn, indicating the length of the string SS.

The second line contains a string SS of length nn consisting only of ( and ).

Output Format

Output one line containing an integer, representing the number of valid bracket subsequences of SS modulo 10910^9.

6
))(()(
3
34
((((((((((((((((()))))))))))))))))
333606220

Hint

For 40%40\% of the testdata, it is guaranteed that 1≤n≤4001 \le n \le 400.

For all testdata, it is guaranteed that 1≤n≤20001 \le n \le 2000.

Translated by ChatGPT 5