#include using namespace std; typedef long long ll; const ll mod = 1e9 + 7; ll modpow(ll a, ll b, ll p = 1e9+7){ if(b == 0) return 1; if(b % 2 == 0){ ll d = modpow(a, b/2, p); return (d*d) % p; }else{ return (a%p * modpow(a, b-1, p)) % p; } } #define N 2000005 ll po[N]; ll inv[N]; int main(){ po[0] = 1; for(int i = 1; i < N; i++) po[i] = (po[i-1] * i) % mod; inv[N-1] = modpow(po[N-1], mod-2, mod); for(int i = N-2; i >= 0; i--) inv[i] = (inv[i+1]*(i+1)) % mod; int t; scanf("%d\n", &t); char c; int n, k; for(int i = 0; i < t; i++){ ll ans = 1; scanf("%c(%d,%d)\n", &c, &n, &k); if(c != 'H' && n < k){ printf("0\n"); continue; } if(c == 'H' && n == 0 && k == 0){ printf("1\n"); continue; } switch(c){ case 'C': ans = (ans * po[n]) % mod; ans = (ans * inv[n-k]) % mod; ans = (ans * inv[k]) % mod; break; case 'P': ans = (ans * po[n]) % mod; ans = (ans * inv[n-k]) % mod; break; case 'H': ans = (ans * po[n+k-1]) % mod; ans = (ans * inv[k]) % mod; ans = (ans * inv[n+k-1-k]) % mod; break; } printf("%d\n", ans); } return 0; }