-
Notifications
You must be signed in to change notification settings - Fork 0
/
EXPCH.cpp
81 lines (74 loc) · 1.81 KB
/
EXPCH.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>
#define lli long long int
#define MP make_pair
#define PB push_back
#define rep(i, a, b) for (lli i=a; i<b; i++)
using namespace std;
long int power(long int x, int y, long int p)
{
long int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
lli t , n , op , cl , c ;
string str ;
cin>>t ;
while(t--)
{
// vector<lli> sub_sum ;
cin>>n ;
lli sm=0;
lli q = (n*(n+1))/2 ;
stack <lli> st;
cin>>str ;
rep(i,0,n)
{
op = 0 ;
cl = 0 ;
c = 0 ;
rep(j,i,n)
{
if(str[j]=='(')
op+=1 ;
else if (str[j]==')')
cl+=1 ;
if ((op<cl) )
{
c+=1;
op+=1 ;
cl-=1 ;
}
if(st.size()>5)
sm+=c;
else
st.push(c);
}
}
q = power(q,1000000005,1000000007) ;
while(!st.empty())
{
sm+=st.top();
st.pop();
}
// cout<<sm<<endl;
// lli s = accumulate(sub_sum.begin(),sub_sum.end(),0) ;
// cout<<s<<endl ;
lli res = (sm*q)%(1000000007) ;
cout<<res<<endl ;
}
return 0;
}