Shù Yìn

Time limit: 0.2s Memory limit: 256MB Input: Output:

Zhège chángshù shì duì shù ma? - 霍里卡·马卡雷纳

Let a valid tree be a rooted tree with NN nodes distinctly labeled with integers from 11 to NN, with node 11 as the root. Two valid trees are considered distinct if there exist two nodes uu and vv such that vv is a child of uu in one tree but not in the other.

We define the yìn of such a tree using the following function:

function DFS(v):
begin
    result = "("
    for each child u of v, in increasing order of label:
        result += DFS(u)
    result += ")"
    return result
end

The yìn of the tree is the string returned by DFS(1).

Task

Given a yìn, determine the number of distinct valid trees with NN nodes that produce this yìn, modulo 109+710^9 + 7.

Input data

The first line contains a valid yìn of length 2N2 \cdot N, consisting only of the characters (\text{(} and )\text{)}.

Output data

Print a single integer: the number of distinct valid trees with NN nodes (where 2N2 \cdot N is the length of the given yìn) that produce this yìn, modulo 109+710^9 + 7.

Constraints and clarifications

  • 1N1061 \leq N \leq 10^6
  • It is guaranteed that there exists at least one valid tree that produces this yìn.
# Score Constraints
1 20 N8N \leq 8
2 20 The yìn does not contain the substring )(\text{)(}.
3 10 Every (\text{(} other than the first character of the yìn is immediately followed by )\text{)}
4 15 N103N \leq 10^3
5 15 N105N \leq 10^5
6 20 No additional restrictions

Example 1

stdin

((())) 

stdout

2

Explanation

The 22 valid labelings are shown below.

Example 2

stdin

(()(()))

stdout

3

Explanation

The 33 valid labelings are shown below.

Log in or sign up to be able to send submissions!