import java.io.*; import java.util.*; public class Main_yukicoder117 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Printer pr = new Printer(System.out); int t = sc.nextInt(); preCombi2(2_000_000); for (int i = 0; i < t; i++) { String[] s = sc.next().split("[\\,\\(\\)]"); // pr.printf("%s %s %s\n", s[0], s[1], s[2]); switch (s[0]) { case "C": pr.println(C(Integer.parseInt(s[1]), Integer.parseInt(s[2]))); break; case "P": pr.println(P(Integer.parseInt(s[1]), Integer.parseInt(s[2]))); break; case "H": pr.println(H(Integer.parseInt(s[1]), Integer.parseInt(s[2]))); break; } } pr.close(); sc.close(); } private static int MOD = 1_000_000_007; private static long[] fact; private static long[] ifact; private static void preCombi2(int size) { // cache size need size * 2 * 16. fact = new long[size + 1]; fact[0] = 1; for (int i = 1; i <= size; i++) { fact[i] = fact[i - 1] * i % MOD; } ifact = new long[size + 1]; ifact[0] = 1; int loop = MOD - 2; long x = fact[size]; ifact[size] = 1; while (loop > 0) { if (loop % 2 == 1) { ifact[size] = ifact[size] * x % MOD; } x = x * x % MOD; loop /= 2; } for (int i = size - 1; i >= 0; i--) { ifact[i] = ifact[i + 1] * (i + 1) % MOD; } } private static int C(int n, int r) { if (r > n) { return 0; } return (int)(((fact[n] * ifact[n - r]) % MOD) * ifact[r] % MOD); } private static int P(int n, int r) { if (r > n) { return 0; } return (int)((fact[n] * ifact[n -r]) % MOD); } private static int H(int n, int r) { if (n == 0 && r == 0) { return 1; } return C(n + r - 1, r); } @SuppressWarnings("unused") private static class Scanner { BufferedReader br; Iterator it; Scanner (InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() throws RuntimeException { try { if (it == null || !it.hasNext()) { it = Arrays.asList(br.readLine().split(" ")).iterator(); } return it.next(); } catch (IOException e) { throw new IllegalStateException(); } } int nextInt() throws RuntimeException { return Integer.parseInt(next()); } long nextLong() throws RuntimeException { return Long.parseLong(next()); } float nextFloat() throws RuntimeException { return Float.parseFloat(next()); } double nextDouble() throws RuntimeException { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { // throw new IllegalStateException(); } } } private static class Printer extends PrintWriter { Printer(PrintStream out) { super(out); } } }