#P10960. [2015杭电多校]Easy Sequence

[2015杭电多校]Easy Sequence

Easy Sequence

Problem Description

soda has a string containing only two characters -- '(' and ')'. For every character in the string, soda wants to know the number of valid substrings which contain that character. Note: An empty string is valid. If SS is valid, (S)(S) is valid. If U,VU,V are valid, UVUV is valid.

Input

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case: A string ss consisting of '(' or ')' (1s106)(1 \le |s| \le 10^6).

Output

For each test case, output an integer $m =\displaystyle \sum_{i = 1}^{|s|} (i\cdot ans_i \text{ mod } 1000000007)$, where ansians_i is the number of valid substrings which contain ii-th character.

Sample Input

2
()()
((()))

Sample Output

20
42

Hint

For the second case, ans={1,2,3,3,2,1}ans = \{1, 2, 3, 3, 2, 1\}, then $m=1 \cdot 1 + 2 \cdot 2 + 3 \cdot 3 + 4 \cdot 3 + 5 \cdot 2 + 6 \cdot 1 = 42$

Author

zimpha@zju

Source

2015 Multi-University Training Contest 6