from fractions import gcd from heapq import* import math from collections import defaultdict, Counter import sys sys.setrecursionlimit(10 ** 7) MOD = 10 ** 9 + 7 def modpow(a, n, mod): res = 1 while n > 0: if n & 1: res = res * a % mod a = a * a % mod n >>= 1 return res def modinv(a, mod): return modpow(a, mod - 2, mod) def nck(a, b): if a < b: return 0 ret = 1 for i in range(b): ret *= (a-i) ret %= MOD ret = ret * modinv(i+1, MOD) % MOD return ret def nhk(a, b): if a < b: return 1 ret = 1 for i in range(b): ret *= (a-i) ret %= MOD ret = ret * modinv(i+1, MOD) % MOD return ret def pnk(a, b): if a < b: return 0 ret = 1 for i in range(b): ret *= (a-i) ret %= MOD return ret def main(): n = int(input()) for i in range(n): s = list(input()) l, r = "", "" lf, rf = False, False for i in range(len(s)): if s[i] == "(": lf = True elif s[i] == ",": l = int(l) rf = True lf = False elif s[i] == ")": r = int(r) elif lf: l += s[i] elif rf: r += s[i] if s[0] == "C": print(nck(l, r)) elif s[0] == "P": print(pnk(l, r)) else: print(nhk(l + r - 1, r)) if __name__ == '__main__': main()