import re p = re.compile(r'\(|\)|,') mod = 10**9+7 N_MAX = 10**6 fact = [0]*(2*N_MAX+1) fact_inv = [0]*(2*N_MAX+1) fact[0] = fact_inv[0] = 1 for i in xrange(1,2*N_MAX+1): fact[i] = (i*fact[i-1]) % mod fact_inv[i] = pow(fact[i],mod-2,mod) % mod T = input() for i in xrange(T): q = p.split(raw_input()) N = int(q[1]) K = int(q[2]) if q[0] == "C": print 0 if N < K else (fact[N]*fact_inv[K]*fact_inv[N-K]) % mod elif q[0] == "P": print 0 if N < K else (fact[N]*fact_inv[N-K]) % mod else: print 1 if N == K == 0 else (fact[N-1+K]*fact_inv[N-1]*fact_inv[K]) % mod