#include using namespace std; long long MOD = 1000000007; long long const sz = 2 * 1000000; long long fac[sz], facinv[sz]; long long inverse(long long x){ long long ret = 1; long long k = MOD - 2; while (k > 0){ if (k & 1) ret = ret * x % MOD; x = x * x % MOD; k /= 2; } return ret; } void init(){ fac[0] = 1; for (int i = 1; i < sz; i++) fac[i] = fac[i - 1] * i % MOD; facinv[sz - 1] = inverse(fac[sz - 1]); for (int i = sz - 2; i > -1; i--) facinv[i] = facinv[i + 1] * (i + 1) % MOD; } long long com(long long n, long long r){ if (n < r) return 0; return fac[n] * facinv[n - r] % MOD * facinv[r] % MOD; } long long per(long long n, long long r){ if (n < r) return 0; return fac[n] * facinv[n - r] % MOD; } long long hom(long long n, long long r){ if (n == 0 && r == 0){ return 1; } return com(n + r - 1, r); } int main(){ init(); int t; cin >> t; for (int _ = 0; _ < t; _++){ char a; char c; int n,r; cin >> c >> a >> n >> a >> r >> a; if (c == 'C') cout << com(n,r) << endl; else if (c == 'P') cout << per(n,r) << endl; else cout << hom(n,r) << endl; } }