import java.util.*; public class Main { static final int MOD = 1000000007; public static void main (String[] args) { Scanner sc = new Scanner(System.in); long[] kaijo = new long[2000001]; int t = sc.nextInt(); Combination cmb = new Combination(2000001); kaijo[0] = 1; for (int i = 1; i < kaijo.length; i++) { kaijo[i] = kaijo[i - 1] * i % MOD; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < t; i++) { String line = sc.next(); char type = line.charAt(0); String[] units = line.substring(2, line.length() - 1).split(",", 2); int a = Integer.parseInt(units[0]); int b = Integer.parseInt(units[1]); long ans; if (type == 'P') { ans = cmb.getComb(a, b) * kaijo[b] % MOD; } else if (type == 'C') { ans = cmb.getComb(a, b); } else { ans = cmb.getComb(a + b - 1, b); } sb.append(ans).append("\n"); } System.out.print(sb); } } class Combination { static final int MOD = 1000000007; long[] fac; long[] finv; long[] inv; public Combination (int size) { fac = new long [size]; finv = new long [size]; inv = new long [size]; fac[0] = 1; fac[1] = 1; finv[0] = 1; finv[1] = 1; inv[1] = 1; for (int i = 2; i < size; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } public long getComb(int n, int k) { if (n < k || n < 0 || k < 0) { return 0; } return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } }