Anton and School - 2
Contest: Codeforces Round 404 (Div. 2)
Difficulty rating: 2300
Tags: combinatorics, dp, math, number theory

As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (without quotes)).

On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence s of length n is an RSBS if the following conditions are met:

- It is not empty (that is n ≠ 0).
- The length of the sequence is even.
- First $$\frac{\pi}{2}$$ charactes of the sequence are equal to "(".
- Last $$\frac{\pi}{2}$$ charactes of the sequence are equal to ")".

For example, the sequence "((()))" is an RSBS but the sequences "((())" and "(()())" are not RSBS.

Elena Ivanovna, Anton's teacher, gave him the following task as a homework. Given a bracket sequence s. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of s is a string that can be obtained from s by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted.

Because the answer can be very big and Anton's teacher doesn't like big numbers, she asks Anton to find the answer modulo 109 + 7.

Anton thought of this task for a very long time, but he still doesn't know how to solve it. Help Anton to solve this task and write a program that finds the answer for it!

Input format:
The only line of the input contains a string s — the bracket sequence given in Anton's homework. The string consists only of characters "(" and ")" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200 000.

Output format:
Output one number — the answer for the task modulo 109 + 7.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first sample the following subsequences are possible:

- If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())".
- If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()".
- If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()".
- If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()".
- If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()".
- If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence "()".

The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6.

Example input:
)(()()

Example output:
6

Example input:
()()()

Example output:
7

Editorial:
At first, let's simplify the problem: let our string consists of x + y characters, begins with x characters "(" and ends with y characters ")". How to find the number of RSBS in such string?
Let's prove that this number is equal to $${ \binom { x+y } { x } } = { \frac { ( x + y )! } { x! \cdot y! } }$$. It's easy to observe that this formula also means the number of ways to match the string with the sequence of zeros and ones of the same length, which contains exactly x ones. Now prove that for every such sequence of zeros and ones we can find an RSBS subsequence. How can we do it? Let's consider it on the example of the following string:
Let's include to our subsequence all the opening brackets that match zeros and all the closing brackets that match ones. In our example, we include brackets number 1, 3, 5 and 6, so we get the subsequence "(())", which is an RSBS.
Why every sequence we got in this way is an RSBS? Let the number of ones that match closing brackets is equal to z. So x - z ones match opening brackets (because we have x ones, as we remember) and, therefore, z zeros match opening brackets. So the number of opening brackets is equal to the number of closing brackets in our subsequence. Also opening brackets appear earlier than closing brackers. So such subsequence is always an RSBS, and the statement above is proved.
Now we must understand how to solve the entire problem. Let's iterate over an opening bracket that is the last opening bracket in our subsequence. Now observe that only opening brackets may come before this bracket, and only closing brackets may come after this bracket. The rest of the brackets will definitely not appear in the subsequence. Let's count the number of opening brackets before the iterated one, incluing the iterated one (let this number is equal to x), and also the number of closing brackets after the iterated one (let this number is equal to y). To calculate these numbers, we can precalc them for all the positions in $${\mathcal{O}}(n)$$ using prefix sums.
Now, we have reduced our problem to the already solved, because we have x opening brackets and then y closing brackets. But we also have an additional condition: we must necessarily take the last opening bracket. So the answer is equal to $${ \frac { ( x + y - 1 ) } { x } }$$, not $${ \frac { x + y } { x } }$$, because on the position with the last opening bracket we must put a zero. So we must put x ones on x + y - 1 positions instead of x + y positions.
Time complexity is $${ \mathcal { O } } ( n \cdot \log ( 1 0 ^ { 9 } + 7 ) )$$ (logarithm is to divide by modulo, that is necessary to calculate the number of combinations).