N = 2 * 10 ** 6 + 1 MOD = 10 ** 9 + 7 fact = [1]*(N+1) rfact = [1]*(N+1) r = 1 for i in range(1, N+1): fact[i] = r = r * i % MOD rfact[N] = r = pow(fact[N], MOD-2, MOD) for i in range(N, 0, -1): rfact[i-1] = r = r * i % MOD def comb(n, k): return fact[n] * rfact[k] * rfact[n-k] % MOD def perm(n, k): return fact[n] * rfact[n-k] % MOD for _ in range(int(input())): S = input() op = S[0] S = S[2:-1] a,b = map(int,S.split(",")) if op == "C": if a < b: print(0) else: print(comb(a,b)) elif op == "P": if a < b: print(0) else: print(perm(a,b)) else: print(comb(a+b-1,a-1))