Zhège chángshù shì duì shù ma? - 霍里卡·马卡雷纳
Let a valid tree be a rooted tree with nodes distinctly labeled with integers from to , with node as the root. Two valid trees are considered distinct if there exist two nodes and such that is a child of 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 nodes that produce this yìn, modulo .
Input data
The first line contains a valid yìn of length , consisting only of the characters and .
Output data
Print a single integer: the number of distinct valid trees with nodes (where is the length of the given yìn) that produce this yìn, modulo .
Constraints and clarifications
- It is guaranteed that there exists at least one valid tree that produces this yìn.
| # | Score | Constraints |
|---|---|---|
| 1 | 20 | |
| 2 | 20 | The yìn does not contain the substring . |
| 3 | 10 | Every other than the first character of the yìn is immediately followed by |
| 4 | 15 | |
| 5 | 15 | |
| 6 | 20 | No additional restrictions |
Example 1
stdin
((()))
stdout
2
Explanation
The valid labelings are shown below.

Example 2
stdin
(()(()))
stdout
3
Explanation
The valid labelings are shown below.
