D. Anton and School - 2

D. Anton and School - 2

time limit per test

2 seconds

memory limit per test

256 megabytes


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.

 

AC代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <set>
#include <cassert> 
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
const ll mod = 1e9+7;
const int N = 1e6; 

ll n;	ll ans=0;
char s[200005];
ll a[200006],b[200006];

ll md[200005],mu[200005];

ll Qpow(ll a,ll mi){
	ll res=1;
	while(mi){
		if(mi&1)
			res = res*a%mod;
		a = a*a%mod; 
		mi/=2;
	}
	res=res%mod;
	return res;
}

void init(){
	md[0]=mu[0]=1;
	for(int i=1;i<200001;i++){
		md[i]=i*md[i-1]%mod;
		mu[i] = Qpow(md[i], mod-2);
	}
}
ll C(ll a, ll b){
	return md[a]%mod*mu[a-b]%mod*mu[b]%mod; // ((md[a]%mod)*(mu[b]%mod)*(mu[a-b]%mod))%mod;
}
bool is(){
	if(n%2) return false;
	for(int i=0;i<n/2;i++){
		if(s[i]!='(') return false;
	}
	for(int i=n/2;i<n;i++){
		if(s[i]!=')') return false;
	}
	return true;
}
int main() {
	// init
	init();
//	cout<<C(3,1)<<'\n';
//	for(int i=0;i<=20;i++){
//		cout<<md[i]<<' ';
//	}
//	cout<<'\n';
//	for(int i=0;i<=20;i++){
//		cout<<mu[i]<<' ';
//	}
	
	
//	cin>>s+1;
	scanf("%s",s);
	n = strlen(s);
//	cout<<n;
	int I=0,J=n-1;
	while(s[I]!='(') I++;
	while(s[J]!=')') J--;
	
	if(I>=J){
		cout<<0;
		return 0;
	}
	a[I] = 1;
	b[I] = 0;
	for(int i=I+1;i<=J;i++){
		if(s[i]=='('){
			a[i]=a[i-1]+1;
			b[i]=b[i-1];
		}
		else{
			b[i]=b[i-1]+1;
			a[i]=a[i-1];
		}
	}
//	for(int i=I;i<=J;i++){
//		cout<<i<<' '<<a[i]<<' '<<b[i]<<'\n';
//	}
//	cout<<I<<' '<<J<<'\n';
	for(int i=I;i<=J;i++){
		if(s[i]=='('){
			ll A = a[i];
			ll B = b[J] - b[i]; 
			ans = ( ans + C(A+B-1,A) )%mod;
//			cout<<A+B-1<<' '<<A<<' '<<C(A+B-1,A)<<'\n';
		}	
	}	
	
//    if(!is()) ans++;
	cout<<ans<<'\n'; 
	return 0; 
} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章