Anton and School - 2 CodeForces - 785D 组合数学 范德蒙恒等式

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 charactes of the sequence are equal to "(".
  • Last 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

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

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

Examples

Input

)(()()

Output

6

Input

()()()

Output

7

Input

)))

Output

0

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.

 

范德蒙恒等式

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<deque>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const int M = 1e6 + 10;
const int mod=1e9+7;
char a[M];
int fi[M],la[M];
ll f[M],sum[M];
ll mul(ll x,ll n)//快速幂求逆元
{
    ll res=1;
    while(n)
    {
        if(n&1)
            res=res*x%mod;
            x=x*x%mod;
            n>>=1;
    }
    return res%mod;
}
void init()//打表求n!
{
    f[0]=1;
    ll i;
    for(i=1;i<M;i++)
        f[i]=(f[i-1]*i)%mod;
}
ll C(int m,int n)//求组合数c(m,n)
{
    ll res=((f[m]*mul(f[n],mod-2))%mod*mul(f[m-n],mod-2))%mod;
    return res;
}
int main()
{
    init();
    while(~scanf("%s",a))
    {
        int l=0,r=0;
        int len=strlen(a);
        for(int i=0;i<len;i++)
        {
            if(a[i]=='(')
                fi[l++]=i;//统计前括号的位置
            else
                la[r++]=i;//统计后括号的位置
        }
        ll ans=0;
        sum[0]=1;
        for(int i=0;i<l;i++)
        {
            int p=upper_bound(la,la+r,fi[i])-la;//前括号取的最后位置为fi[i],二分找后括号的第一个位置
            ans=(ans+C(i+r-p,i+1))%mod;//   r-p为前括号取的最后位置为fi[i],符合的后括号的个数。
            ans=ans%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liluoyu_1016/article/details/81391534