import java.util.*; public class Main { public static long MOD = (long)Math.pow(10, 9) + 7; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); long[] kai = new long[2000001]; kai[0] = 1; for(int i = 1; i < 2000001; i++) { kai[i] = (kai[i - 1] * (long)i) % MOD; } for(int i = 0; i < t; i++) { String s = sc.next(); char c1 = s.charAt(0); String sub = s.substring(2, s.length() - 1); String[] num = sub.split(",", 0); long n1 = Long.parseLong(num[0]); long n2 = Long.parseLong(num[1]); long ans = 0; if(c1 == 'C') { if(n1 >= n2) { ans = kai[(int)n1]; ans = (ans * func(kai[(int)n2], MOD - 2)) % MOD; ans = (ans * func(kai[(int)n1 -(int) n2], MOD - 2)) % MOD; } } else if(c1 == 'P') { if(n1 >= n2) { ans = kai[(int)n1]; ans = (ans * func(kai[(int)n1 -(int) n2], MOD - 2)) % MOD; } } else { if(n1 >= 1) { ans = kai[(int)n1 + (int)n2 - 1]; ans = (ans * func(kai[(int)n2], MOD - 2)) % MOD; ans = (ans * func(kai[(int)n1 - 1], MOD - 2)) % MOD; } else { ans = 1; } } System.out.println(ans); } } public static long func(long a, long x) { if(x == 0) return 1; if(x % 2 == 0) { long t = func(a, x / 2); return (t * t) % MOD; } else { long t = func(a, x / 2); t = (t * t) % MOD; return (t * a) % MOD; } } }