#ifndef LOCAL #include using namespace std; #define debug(...) (void(0)) #else #include "algo/debug.h" #endif #include using mint = atcoder::modint1000000007; template struct Binom { private: std::vector fact, ifac; void make_table(size_t N) { const size_t a = fact.size(); if(a > N) return; fact.resize(N + 1); for(size_t i = a; i <= N; i++) fact[i] = fact[i - 1] * i; ifac.resize(N + 1); ifac[N] = Tp{1} / fact[N]; for(size_t i = N; i >= a; i--) ifac[i - 1] = ifac[i] * i; } public: Binom(size_t N = 0) : fact(1, 1), ifac(1, 1) { make_table(N); } Tp factrial(size_t x) { make_table(x); return fact[x]; } Tp invfact(size_t x) { make_table(x); return ifac[x]; } Tp perm(size_t N, size_t K) { if(N < K) return Tp{0}; make_table(N); return fact[N] * ifac[N - K]; } Tp comb(size_t N, size_t K) { if(N < K) return Tp{0}; make_table(N); return fact[N] * ifac[K] * ifac[N - K]; } Tp homo(size_t N, size_t K) { if(N == 0) return K == 0 ? Tp{1} : Tp{0}; return comb(N - 1 + K, K); } }; Binom C; char s[2]; void solve() { int N, K; scanf("%1s(%d, %d)", s, &N, &K); char c = s[0]; if(c == 'C') cout << C.comb(N, K).val() << '\n'; else if(c == 'P') cout << C.perm(N, K).val() << '\n'; else cout << C.homo(N, K).val() << '\n'; } int main() { int tt = 1; scanf("%d", &tt); while (tt--) { solve(); } }