#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; void solve() { char c; cin >> c; string s; cin >> s; int id = 1; const auto ri = [&]() -> int { int res{}; for(; s[id] != ',' && s[id] != ')'; id++) { res *= 10; res += s[id] - '0'; } return res; }; int N = ri(); id++; int K = ri(); 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() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int tt = 1; std::cin >> tt; while (tt--) { solve(); } }