mod = 10**9 + 7 fact = [1] * (2 * 10**6 + 1) inv = [1] * (2 * 10**6 + 1) finv = [1] * (2 * 10**6 + 1) for i in range(2, 2 * 10**6 + 1): fact[i] = fact[i - 1] * i % mod inv[i] = mod - inv[mod % i] * (mod // i) % mod finv[i] = finv[i - 1] * inv[i] % mod def c(x, y): if x < y: return 0 return fact[x] * finv[y] % mod * finv[x - y] % mod def p(x, y): if x < y: return 0 return fact[x] * finv[x - y] % mod def h(x, y): return fact[x + y - 1] * finv[x - 1] % mod * finv[y] % mod for _ in range(int(input())): s = input() n = len(s) s1, s2 = s[0], s[2:n - 1] x, y = map(int, s2.split(',')) if s1 == 'P': print(p(x, y)) elif s1 == 'C': print(c(x, y)) elif s1 == 'H': print(h(x, y))