import sys sys.setrecursionlimit(10 ** 6) from bisect import * from collections import * from heapq import * int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def MI1(): return map(int1, sys.stdin.readline().split()) def MF(): return map(float, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LI1(): return list(map(int1, sys.stdin.readline().split())) def LF(): return list(map(float, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] dij = [(0, 1), (1, 0), (0, -1), (-1, 0)] def nCr(com_n, com_r): if com_n < com_r: return 0 return fac[com_n] * ifac[com_r] * ifac[com_n - com_r] % md def nPr(com_n, com_r): if com_n < com_r: return 0 return fac[com_n] * ifac[com_n - com_r] % md def nHr(com_n, com_r): if (com_n, com_r)==(0,0):return 1 return nCr(com_n + com_r - 1, com_r) md = 10 ** 9 + 7 n_max = 10 ** 6 * 2 fac = [1] for i in range(1, n_max + 1): fac.append(fac[-1] * i % md) ifac = [1] * (n_max + 1) ifac[n_max] = pow(fac[n_max], md - 2, md) for i in range(n_max - 1, 1, -1): ifac[i] = ifac[i + 1] * (i + 1) % md def main(): t = II() for _ in range(t): s = input() op = s[0] n, r = map(int, s[2:-1].split(",")) if op == "C": print(nCr(n, r)) if op == "P": print(nPr(n, r)) if op == "H": print(nHr(n, r)) main()