class Combination: def __init__(self, N, p): self.N = N self.p = p self.f = [None] * (N + 1) self.finv = [None] * (N + 1) self.inv = [None] * (N + 1) self.f[0] = 1 self.f[1] = 1 self.finv[0] = 1 self.finv[1] = 1 self.inv[1] = 1 for i in range(2, N + 1): self.f[i] = self.f[i - 1] * i % p self.inv[i] = p - self.inv[p % i] * (p // i) % p self.finv[i] = self.finv[i - 1] * self.inv[i] % p def P(self, n, k): if n < k: return 0 if n < 0 or k < 0: return 0 return self.f[n] * self.finv[n - k] % self.p def C(self, n, k): if n < k: return 0 if n < 0 or k < 0: return 0 return self.f[n] * (self.finv[k] * self.finv[n - k] % self.p) % self.p # 重複組合せ # n種類のものからk個選ぶ def H(self, n, k): return self.C(k + n - 1, k) # https://yukicoder.me/problems/no/117 L = 2000000 p = 10 ** 9 + 7 comb = Combination(L, p) # autopep8: off import re # autopep8: on T = int(input()) regex = r'([CPH])\((\d+),(\d+)\)' for _ in range(T): res = re.match(regex, input()) t = res[1] n = int(res[2]) k = int(res[3]) if t == 'C': print(comb.C(n, k)) elif t == 'P': print(comb.P(n, k)) else: print(comb.H(n, k))