#include using namespace std; using i64 = int_fast64_t; using ui64 = uint_fast64_t; #define REP(i, n) for (i64 (i) = 0; (i) < (n); ++(i)) #define FOR(i, a, b) for (i64 (i) = (a); (i) < (b); ++(i)) const int MOD = (int)1e9 + 7; const int MAX_N = 2020202; long long fac[MAX_N], finv[MAX_N], inv[MAX_N]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX_N; ++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; } } long long nCr(long long n, long long r) { if (r == 0) return 1; if (r < 0) return 0; if (n < 0) return (r % 2 ? -1 : 1) * nCr(-n + r - 1, r); if (n == 0 || n < r) return 0; return (fac[n] * ((finv[n - r] * finv[r]) % MOD)) % MOD; } long long nPr(long long n, long long r) { if (r < 0 || n < r) return 0; else return (fac[n]*finv[n - r]) % MOD; } long long nHr(long long n, long long r) { if (n == 0 && r == 0) return 1; else return nCr(n + r - 1, r); } signed main() { COMinit(); int N; cin >> N; REP(i, N) { string S; cin >> S; char t; int a, b; sscanf(S.c_str(), "%c(%d,%d)", &t, &a, &b); if (t == 'C') { cout << nCr(a, b) << endl; } else if (t == 'P') { cout << nPr(a, b) << endl; } else { cout << nHr(a, b) << endl; } } }