import re MOD = 10 ** 9 + 7 sz = 2 * 10 ** 6 def inverse(n): return pow(n,MOD-2,MOD) fac = [0] * (sz) fac[0] = 1 for i in range(1, sz): fac[i] = fac[i-1] * i % MOD facinv = [0] * (sz) facinv[sz - 1] = inverse(fac[sz - 1]) for i in range(sz - 2, -1, -1): facinv[i] = facinv[i+1] * (i+1) % MOD def bi(n,r): if n < r: return 0 return fac[n] * facinv[r] * facinv[n-r] % MOD def per(n,r): if n < r: return 0 return fac[n] * facinv[n-r] % MOD t = int(input()) for _ in range(t): s = input() n,r = (map(int,re.findall(r"\d+", s))) if s[0] == "C": print(bi(n,r)) elif s[0] == "P": print(per(n,r)) else: print(bi(n+r-1,r) if n * n + r * r > 0 else 1)