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{n}{2}$ charactes of the sequence are equal to "(".  Last $\frac{n}{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 10^9 + 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 10^9 + 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.

Difficulty: HARD
Tags: Number theory

Combinatorics

Dynamic programming

Mathematics
Skill types: Dynamic programming

Starter code:

Solutions:
mod = 10 ** 9 + 7
(F, inv, iF) = ([1, 1], [0, 1], [1, 1])
for i in range(2, 200005):
	F.append(F[-1] * i % mod)
	inv.append(inv[mod % i] * (mod - mod // i) % mod)
	iF.append(iF[-1] * inv[-1] % mod)

def C(n, k):
	if k < 0 or k > n:
		return 0
	return F[n] * iF[k] * iF[n - k]
s = input()
(open, close) = (0, s.count(')'))
ans = 0
for c in s:
	if c == '(':
		open += 1
		ans += C(close + open - 1, open)
	else:
		close -= 1
print(ans % mod)


mod = 10 ** 9 + 7
s = input()
n = len(s)
f = [1] * (n + 1)
for i in range(1, n + 1):
	f[i] = i * f[i - 1] % mod
finv = [pow(x, mod - 2, mod) for x in f]
op = 0
cl = s.count(')')
ans = 0
if cl > 0:
	for c in s:
		if c == '(':
			op += 1
			ans += f[op + cl - 1] * finv[cl - 1] * finv[op]
		elif cl <= 1:
			break
		else:
			cl -= 1
print(ans % mod)


def ri():
	return list(map(int, input().split()))
m = 10 ** 9 + 7
s = input()
n = len(s)
o = [0 for i in range(len(s))]
c = [0 for i in range(len(s))]
fac = [0 for i in range(n)]
fac[0] = 1
for i in range(1, n):
	fac[i] = fac[i - 1] * i % m
invfac = [pow(fac[i], m - 2, m) for i in range(n)]
if s[0] == '(':
	o[0] = 1
for i in range(1, n):
	if s[i] == '(':
		o[i] = o[i - 1] + 1
	else:
		o[i] = o[i - 1]
if s[n - 1] == ')':
	c[n - 1] = 1
for i in range(n - 2, -1, -1):
	if s[i] == ')':
		c[i] = c[i + 1] + 1
	else:
		c[i] = c[i + 1]
ans = 0
for i in range(n):
	if s[i] == '(':
		a = o[i]
		b = c[i]
		if a != 0 and b != 0:
			ans += fac[a + b - 1] * invfac[a] * invfac[b - 1]
			ans %= m
print(ans)


mod = 10 ** 9 + 7
maxn = 200001

def C(n, k):
	if k < 0 or k > n:
		return 0
	return fact[n] * invfact[k] * invfact[n - k] % mod
fact = [1, 1]
inv = [0, 1]
invfact = [1, 1]
for i in range(2, maxn):
	fact.append(fact[-1] * i % mod)
	inv.append(inv[mod % i] * (mod - mod // i) % mod)
	invfact.append(invfact[-1] * inv[-1] % mod)
s = input()
op = 0
cl = s.count(')')
ans = 0
for x in s:
	if x == '(':
		op += 1
		cur = C(cl + op - 1, op) % mod
		ans += cur % mod
	else:
		cl -= 1
print(ans % mod)


s = input()
MOD = 10 ** 9 + 7
num = 0
N = len(s)
R = [0] * (N + 1)
L = [0] * (N + 1)
d = []
for i in range(N):
	if s[i] == ')':
		R[i + 1] = 1
	else:
		L[i + 1] = 1
		d.append(i)
for i in range(1, N + 1):
	R[i] += R[i - 1]
	L[i] += L[i - 1]
M = 200005
fact = [0] * M
fact[0] = 1
for i in range(1, M):
	fact[i] = fact[i - 1] * i
	fact[i] %= MOD
rfact = [0] * M
rfact[M - 1] = pow(fact[M - 1], MOD - 2, MOD)
for i in range(M - 2, -1, -1):
	rfact[i] = rfact[i + 1] * (i + 1)
	rfact[i] %= MOD

def comb(n, k):
	if k < 0 or k > n:
		return 0
	return fact[n] * rfact[n - k] * rfact[k] % MOD
for i in d:
	if s[i] == '(':
		l = L[i + 1] - 1
		r = R[N] - R[i + 1]
		num += comb(l + r, l + 1)
		num %= MOD
print(num)


t = input()
(n, m) = (len(t) + 1, 1000000007)
(a, b) = (0, t.count(')') - 1)
f = [1] * n
for i in range(2, n):
	f[i] = i * f[i - 1] % m
g = [pow(q, m - 2, m) for q in f]
s = 0
for q in t:
	if b < 0:
		break
	if q == '(':
		a += 1
		s += f[a + b] * g[a] * g[b]
	else:
		b -= 1
print(s % m)


t = input()
(n, m) = (len(t) + 1, 1000000007)
(x, y) = (0, t.count(')') - 1)
fact = [1] * n
for i in range(2, n):
	fact[i] = i * fact[i - 1] % m
invMult = [pow(x, m - 2, m) for x in fact]
s = 0
for b in t:
	if y < 0:
		break
	if b == '(':
		x += 1
		s += fact[x + y] * invMult[x] * invMult[y] % m
	else:
		y -= 1
print(s % m)


p = 10 ** 9 + 7

def power(x, y, p):
	b = bin(y)[2:]
	answer = 1
	start = x % p
	for i in range(len(b)):
		if b[len(b) - 1 - i] == '1':
			answer = answer * start % p
		start = start * start % p
	return answer

def process(S):
	answer = 0
	n = len(S)
	left_count = 0
	right_count = 0
	p_lc = 1
	p_rc = 1
	p_num = 1
	num = 0
	for c in S:
		if c == ')':
			right_count += 1
			p_rc = p_rc * (right_count + 1) % p
			num += 1
			p_num = p_num * num % p
	for i in range(n):
		if S[i] == '(':
			left_count += 1
			if left_count > 1:
				p_lc = p_lc * (left_count - 1) % p
			num += 1
			p_num = p_num * num % p
		else:
			p_rc = p_rc * power(right_count + 1, p - 2, p) % p
			p_num = p_num * power(num, p - 2, p) % p
			right_count -= 1
			num -= 1
			if left_count > 0:
				entry = p_num * power(p_lc, p - 2, p) * power(p_rc, p - 2, p) % p
				answer = (answer + entry) % p
	return answer
S = input()
print(process(S))