Manasa has a string having N number of A's and 2*N number of B's. She wants to arrange these characters in such a way that in each prefix and in each suffix of the string the number of B's is greater than or equal to the number of A's. Given the value of N, she wants to find the number of ways to do so.

Input Format 

The first line contains an integer T i.e. number of test cases. 

Next T lines will contain an integer N.  

Output Format 

A single line containing number of ways MOD 99991.

Constraints 

1 <= T <= 25
 
1 <= N <= 10^{12}  

Sample Input #00  

2
1
2

Sample Output #00  

1
4

Explanation  

In first case, "BAB" is only valid string.

In second case, "BBAABB", "BABABB" , "BBABAB" and "BABBAB" are valid strings.

Source: hackerrank | Difficulty: MEDIUM
Tags: Combinatorics


Solutions:
def ext_gcd(aneek, sayantan):
	ash = aneek // sayantan
	champa = aneek - sayantan * ash
	shreejit = [1, 0]
	while champa != 0:
		aneek = sayantan
		sayantan = champa
		shreejit.append(shreejit[0] - ash * shreejit[1])
		shreejit = shreejit[1:]
		ash = aneek // sayantan
		champa = aneek - sayantan * ash
	return shreejit[1]

def inv_mod(xavi, prime):
	barca = ext_gcd(xavi, prime)
	if barca < 0:
		barca += prime
	return barca

def C(n, m, p):
	if m == 0 or n == m:
		return 1
	elif m == 1 or n - m == 1:
		return n
	else:
		ans = 1
		ind = 1
		q1 = m
		q2 = n
		while ind != 0:
			m_i = q1 % p
			n_i = q2 % p
			q1 = q1 // p
			q2 = q2 // p
			ind = q1
			if m_i > n_i:
				return 0
			elif m_i == 0 or n_i == m_i:
				continue
			elif m_i == 1 or n_i == m_i + 1:
				ans = ans * n_i % p
			else:
				m_i = min(m_i, n_i - m_i)
				tmp = 1
				for j in range(m_i):
					tmp = tmp * (n_i - j) % p
					tmp = tmp * inv_mod(j + 1, p) % p
				ans = ans * tmp % p
		return ans
t = int(input())
for case in range(t):
	n = int(input())
	Ans = C(3 * n, n, 99991) - 2 * C(3 * n, n - 1, 99991) + C(3 * n, n - 2, 99991)
	print(Ans % 99991)


(MOD, fact) = (99991, [1] * 400001)
for i in range(2, 400001):
	fact[i] = fact[i - 1] * i % MOD

def L(X, Y):
	if X == Y == 0:
		return 1
	(x, y) = (X % MOD, Y % MOD)
	if x < y:
		return 0
	(a, b, c) = (fact[x], fact[y], fact[x - y])
	return L(X // MOD, Y // MOD) * (a * pow(b, MOD - 2, MOD) % MOD * pow(c, MOD - 2, MOD)) % MOD
for T in range(int(input())):
	ans = (lambda N: L(3 * N, N) + 2 * MOD - 2 * L(3 * N, N - 1) + L(3 * N, N - 2))(int(input()))
	print((MOD + ans % MOD) % MOD)


M = 99991

def powM(b, e):
	return b if e == 1 else powM(b, e >> 1) ** 2 * b % M if e & 1 else powM(b, e >> 1) ** 2 % M
(F, VF) = ([1] * (M + 1), [1] * (M + 1))
for i in range(M):
	F[i + 1] = F[i] * (i + 1) % M
	VF[i + 1] = powM(F[i + 1], M - 2)

def C(n, m):
	if m > n:
		return 0
	if m == n:
		return 1
	if m >= M or n >= M:
		return C(n % M, m % M) * C(n // M, m // M) % M
	return F[n] * VF[n - m] * VF[m] % M
for _ in range(int(input())):
	n = int(input())
	print(((C(n * 3, n) - 2 * C(3 * n, n - 1) + C(3 * n, n - 2)) % M + M) % M)


from collections import defaultdict
mod = 99991
factlist = [1]
for i in range(1, mod):
	factlist.append(factlist[-1] * i % mod)
factinvlist = [0] * mod
factinvlist[-1] = pow(factlist[-1], mod - 2, mod)
for i in range(mod - 1, 0, -1):
	factinvlist[i - 1] = factinvlist[i] * i % mod

def choose(n, r):
	currn = n
	currr = r
	toret = 1
	while currn > 0:
		ndig = currn % mod
		rdig = currr % mod
		if rdig > ndig:
			return 0
		toret *= factlist[ndig] * factinvlist[rdig] * factinvlist[ndig - rdig]
		currn //= mod
		currr //= mod
	return toret % mod
t = int(input())
for _ in range(t):
	n = int(input())
	print((choose(3 * n, n) - 2 * choose(3 * n, n - 1) + choose(3 * n, n - 2)) % mod)


(MOD, fact) = (99991, [1] * 400001)
for i in range(2, 400001):
	fact[i] = fact[i - 1] * i % MOD

def L(X, Y):
	if X == Y == 0:
		return 1
	(x, y) = (X % MOD, Y % MOD)
	if x < y:
		return 0
	(a, b, c) = (fact[x], fact[y], fact[x - y])
	return L(X // MOD, Y // MOD) * (a * pow(b, MOD - 2, MOD) % MOD * pow(c, MOD - 2, MOD)) % MOD
for T in range(int(input())):
	ans = (lambda N: L(3 * N, N) + 2 * MOD - 2 * L(3 * N, N - 1) + L(3 * N, N - 2))(int(input()))
	print((MOD + ans % MOD) % MOD)


(mod, fact) = (99991, [1] * 400001)
for i in range(2, 400001):
	fact[i] = fact[i - 1] * i % mod

def L(X, Y):
	if X == Y == 0:
		return 1
	(x, y) = (X % mod, Y % mod)
	if x < y:
		return 0
	(a, b, c) = (fact[x], fact[y], fact[x - y])
	return L(X // mod, Y // mod) * (a * pow(b, mod - 2, mod) % mod * pow(c, mod - 2, mod)) % mod
for T in range(int(input())):
	ans = (lambda N: L(3 * N, N) + 2 * mod - 2 * L(3 * N, N - 1) + L(3 * N, N - 2))(int(input()))
	print((mod + ans % mod) % mod)


def ext_gcd(aneek, sayantan):
	ash = aneek // sayantan
	champa = aneek - sayantan * ash
	shreejit = [1, 0]
	while champa != 0:
		aneek = sayantan
		sayantan = champa
		shreejit.append(shreejit[0] - ash * shreejit[1])
		shreejit = shreejit[1:]
		ash = aneek // sayantan
		champa = aneek - sayantan * ash
	return shreejit[1]

def inv_mod(xavi, prime):
	barca = ext_gcd(xavi, prime)
	if barca < 0:
		barca += prime
	return barca

def C(n, m, p):
	if m == 0 or n == m:
		return 1
	elif m == 1 or n - m == 1:
		return n
	else:
		ans = 1
		ind = 1
		q1 = m
		q2 = n
		while ind != 0:
			m_i = q1 % p
			n_i = q2 % p
			q1 = q1 // p
			q2 = q2 // p
			ind = q1
			if m_i > n_i:
				return 0
			elif m_i == 0 or n_i == m_i:
				continue
			elif m_i == 1 or n_i == m_i + 1:
				ans = ans * n_i % p
			else:
				m_i = min(m_i, n_i - m_i)
				tmp = 1
				for j in range(m_i):
					tmp = tmp * (n_i - j) % p
					tmp = tmp * inv_mod(j + 1, p) % p
				ans = ans * tmp % p
		return ans
t = int(input())
for case in range(t):
	n = int(input())
	Ans = C(3 * n, n, 99991) - 2 * C(3 * n, n - 1, 99991) + C(3 * n, n - 2, 99991)
	print(Ans % 99991)


def ext_gcd(aneek, sayantan):
	ash = aneek // sayantan
	champa = aneek - sayantan * ash
	shreejit = [1, 0]
	while champa != 0:
		aneek = sayantan
		sayantan = champa
		shreejit.append(shreejit[0] - ash * shreejit[1])
		shreejit = shreejit[1:]
		ash = aneek // sayantan
		champa = aneek - sayantan * ash
	return shreejit[1]

def inv_mod(xavi, prime):
	barca = ext_gcd(xavi, prime)
	if barca < 0:
		barca += prime
	return barca

def C(n, m, p):
	if m == 0 or n == m:
		return 1
	elif m == 1 or n - m == 1:
		return n
	else:
		ans = 1
		ind = 1
		q1 = m
		q2 = n
		while ind != 0:
			m_i = q1 % p
			n_i = q2 % p
			q1 = q1 // p
			q2 = q2 // p
			ind = q1
			if m_i > n_i:
				return 0
			elif m_i == 0 or n_i == m_i:
				continue
			elif m_i == 1 or n_i == m_i + 1:
				ans = ans * n_i % p
			else:
				m_i = min(m_i, n_i - m_i)
				tmp = 1
				for j in range(m_i):
					tmp = tmp * (n_i - j) % p
					tmp = tmp * inv_mod(j + 1, p) % p
				ans = ans * tmp % p
		return ans
t = int(input())
for case in range(t):
	n = int(input())
	Ans = C(3 * n, n, 99991) - 2 * C(3 * n, n - 1, 99991) + C(3 * n, n - 2, 99991)
	print(Ans % 99991)


def ext_gcd(aneek, sayantan):
	ash = aneek // sayantan
	champa = aneek - sayantan * ash
	shreejit = [1, 0]
	while champa != 0:
		aneek = sayantan
		sayantan = champa
		shreejit.append(shreejit[0] - ash * shreejit[1])
		shreejit = shreejit[1:]
		ash = aneek // sayantan
		champa = aneek - sayantan * ash
	return shreejit[1]

def inv_mod(xavi, prime):
	barca = ext_gcd(xavi, prime)
	if barca < 0:
		barca += prime
	return barca

def C(n, m, p):
	if m == 0 or n == m:
		return 1
	elif m == 1 or n - m == 1:
		return n
	else:
		ans = 1
		ind = 1
		q1 = m
		q2 = n
		while ind != 0:
			m_i = q1 % p
			n_i = q2 % p
			q1 = q1 // p
			q2 = q2 // p
			ind = q1
			if m_i > n_i:
				return 0
			elif m_i == 0 or n_i == m_i:
				continue
			elif m_i == 1 or n_i == m_i + 1:
				ans = ans * n_i % p
			else:
				m_i = min(m_i, n_i - m_i)
				tmp = 1
				for j in range(m_i):
					tmp = tmp * (n_i - j) % p
					tmp = tmp * inv_mod(j + 1, p) % p
				ans = ans * tmp % p
		return ans
t = int(input())
for case in range(t):
	n = int(input())
	Ans = C(3 * n, n, 99991) - 2 * C(3 * n, n - 1, 99991) + C(3 * n, n - 2, 99991)
	print(Ans % 99991)


def ext_gcd(aneek, sayantan):
	ash = aneek // sayantan
	champa = aneek - sayantan * ash
	shreejit = [1, 0]
	while champa != 0:
		aneek = sayantan
		sayantan = champa
		shreejit.append(shreejit[0] - ash * shreejit[1])
		shreejit = shreejit[1:]
		ash = aneek // sayantan
		champa = aneek - sayantan * ash
	return shreejit[1]

def inv_mod(xavi, prime):
	barca = ext_gcd(xavi, prime)
	if barca < 0:
		barca += prime
	return barca

def C(n, m, p):
	if m == 0 or n == m:
		return 1
	elif m == 1 or n - m == 1:
		return n
	else:
		ans = 1
		ind = 1
		q1 = m
		q2 = n
		while ind != 0:
			m_i = q1 % p
			n_i = q2 % p
			q1 = q1 // p
			q2 = q2 // p
			ind = q1
			if m_i > n_i:
				return 0
			elif m_i == 0 or n_i == m_i:
				continue
			elif m_i == 1 or n_i == m_i + 1:
				ans = ans * n_i % p
			else:
				m_i = min(m_i, n_i - m_i)
				tmp = 1
				for j in range(m_i):
					tmp = tmp * (n_i - j) % p
					tmp = tmp * inv_mod(j + 1, p) % p
				ans = ans * tmp % p
		return ans
t = int(input())
for case in range(t):
	n = int(input())
	Ans = C(3 * n, n, 99991) - 2 * C(3 * n, n - 1, 99991) + C(3 * n, n - 2, 99991)
	print(Ans % 99991)


def ext_gcd(aneek, sayantan):
	ash = aneek // sayantan
	champa = aneek - sayantan * ash
	shreejit = [1, 0]
	while champa != 0:
		aneek = sayantan
		sayantan = champa
		shreejit.append(shreejit[0] - ash * shreejit[1])
		shreejit = shreejit[1:]
		ash = aneek // sayantan
		champa = aneek - sayantan * ash
	return shreejit[1]

def inv_mod(xavi, prime):
	barca = ext_gcd(xavi, prime)
	if barca < 0:
		barca += prime
	return barca

def C(n, m, p):
	if m == 0 or n == m:
		return 1
	elif m == 1 or n - m == 1:
		return n
	else:
		ans = 1
		ind = 1
		q1 = m
		q2 = n
		while ind != 0:
			m_i = q1 % p
			n_i = q2 % p
			q1 = q1 // p
			q2 = q2 // p
			ind = q1
			if m_i > n_i:
				return 0
			elif m_i == 0 or n_i == m_i:
				continue
			elif m_i == 1 or n_i == m_i + 1:
				ans = ans * n_i % p
			else:
				m_i = min(m_i, n_i - m_i)
				tmp = 1
				for j in range(m_i):
					tmp = tmp * (n_i - j) % p
					tmp = tmp * inv_mod(j + 1, p) % p
				ans = ans * tmp % p
		return ans
t = int(input())
for case in range(t):
	n = int(input())
	Ans = C(3 * n, n, 99991) - 2 * C(3 * n, n - 1, 99991) + C(3 * n, n - 2, 99991)
	print(Ans % 99991)


(MOD, fact) = (99991, [1] * 400001)
for i in range(2, 400001):
	fact[i] = fact[i - 1] * i % MOD

def L(X, Y):
	if X == Y == 0:
		return 1
	(x, y) = (X % MOD, Y % MOD)
	if x < y:
		return 0
	(a, b, c) = (fact[x], fact[y], fact[x - y])
	return L(X // MOD, Y // MOD) * (a * pow(b, MOD - 2, MOD) % MOD * pow(c, MOD - 2, MOD)) % MOD
for T in range(int(input())):
	ans = (lambda N: L(3 * N, N) + 2 * MOD - 2 * L(3 * N, N - 1) + L(3 * N, N - 2))(int(input()))
	print((MOD + ans % MOD) % MOD)


def ext_gcd(aneek, sayantan):
	ash = aneek // sayantan
	champa = aneek - sayantan * ash
	shreejit = [1, 0]
	while champa != 0:
		aneek = sayantan
		sayantan = champa
		shreejit.append(shreejit[0] - ash * shreejit[1])
		shreejit = shreejit[1:]
		ash = aneek // sayantan
		champa = aneek - sayantan * ash
	return shreejit[1]

def inv_mod(xavi, prime):
	barca = ext_gcd(xavi, prime)
	if barca < 0:
		barca += prime
	return barca

def C(n, m, p):
	if m == 0 or n == m:
		return 1
	elif m == 1 or n - m == 1:
		return n
	else:
		ans = 1
		ind = 1
		q1 = m
		q2 = n
		while ind != 0:
			m_i = q1 % p
			n_i = q2 % p
			q1 = q1 // p
			q2 = q2 // p
			ind = q1
			if m_i > n_i:
				return 0
			elif m_i == 0 or n_i == m_i:
				continue
			elif m_i == 1 or n_i == m_i + 1:
				ans = ans * n_i % p
			else:
				m_i = min(m_i, n_i - m_i)
				tmp = 1
				for j in range(m_i):
					tmp = tmp * (n_i - j) % p
					tmp = tmp * inv_mod(j + 1, p) % p
				ans = ans * tmp % p
		return ans
t = int(input())
for case in range(t):
	n = int(input())
	Ans = C(3 * n, n, 99991) - 2 * C(3 * n, n - 1, 99991) + C(3 * n, n - 2, 99991)
	print(Ans % 99991)