#include #define debug(x) cerr << #x << ": " << x << '\n'; using namespace std; using ll = long long; using P = pair; const int INF = (int)1e9; const int MOD = (int)1e9 + 7; #ifndef MOD_INT #define MOD_INT template struct ModInt{ int x; ModInt() : x(0){} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod){} ModInt &operator+=(const ModInt &p){ if((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p){ if((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p){ x = (int) (1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p){ *this *= p.inverse(); return *this; } ModInt operator-() const {return ModInt(-x);} ModInt operator+(const ModInt &p) const {return ModInt(*this) += p;} ModInt operator-(const ModInt &p) const {return ModInt(*this) -= p;} ModInt operator*(const ModInt &p) const {return ModInt(*this) *= p;} ModInt operator/(const ModInt &p) const {return ModInt(*this) /= p;} bool operator==(const ModInt &p) const {return x == p.x;} bool operator!=(const ModInt &p) const {return x != p.x;} ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p){ return os << p.x; } friend istream &operator>>(istream &is, ModInt &a){ int64_t t; is >> t; a = ModInt< mod >(t); return (is); } static int get_mod(){return mod;} }; using mint = ModInt; #endif #ifndef MOD_INT #define MOD_INT template struct ModInt{ int x; ModInt() : x(0){} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod){} ModInt &operator+=(const ModInt &p){ if((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p){ if((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p){ x = (int) (1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p){ *this *= p.inverse(); return *this; } ModInt operator-() const {return ModInt(-x);} ModInt operator+(const ModInt &p) const {return ModInt(*this) += p;} ModInt operator-(const ModInt &p) const {return ModInt(*this) -= p;} ModInt operator*(const ModInt &p) const {return ModInt(*this) *= p;} ModInt operator/(const ModInt &p) const {return ModInt(*this) /= p;} bool operator==(const ModInt &p) const {return x == p.x;} bool operator!=(const ModInt &p) const {return x != p.x;} ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p){ return os << p.x; } friend istream &operator>>(istream &is, ModInt &a){ int64_t t; is >> t; a = ModInt< mod >(t); return (is); } static int get_mod(){return mod;} }; using mint = ModInt; #endif class Factorial{ private: int max; vector fact, ifact; void resize(int n){ if(n <= max) return; fact.resize(n+1); ifact.resize(n+1); for(int i = max + 1; i <= n; i++) fact[i] = fact[i-1] * i; ifact[n] = fact[n].inverse(); for(int i = n; i >= max + 1; i--) ifact[i-1] = ifact[i] * i; max = n; } public: Factorial(int N = 0):max(N), fact(N+1), ifact(N+1){ fact[0] = 1; for(int i = 1; i <= N; i++) fact[i] = fact[i-1] * i; ifact[N] = fact[N].inverse(); for(int i = N; i >= 1; i--) ifact[i-1] = ifact[i] * i; } mint operator()(int n){ if(n > max) this -> resize(n); return fact[n]; } mint inverse(int n){ if(n > max) this -> resize(n); return ifact[n]; } mint combination(int n, int k){ if(k < 0 or n < k) return 0; if(n > max) this -> resize(n); return fact[n]*ifact[k]*ifact[n-k]; } mint permutation(int n, int k){ if(k < 0 or n < k) return 0; if(n > max) this -> resize(n); return fact[n]*ifact[n-k]; } mint hcombination(int n, int k){ if(n == 0 and k == 0) return 1; return this -> combination(n+k-1, k); } }; int main(void){ int T; cin >> T; Factorial fact; vector vec; for(int i = 0; i < T; i++){ char c, buf; int n, k; cin >> c >> buf >> n >> buf >> k >> buf; if(c == 'C'){ vec.push_back(fact.combination(n, k)); }else if(c == 'P'){ vec.push_back(fact.permutation(n, k)); }else if(c == 'H') { vec.push_back(fact.hcombination(n, k)); } } for(int i = 0; i < T; i++){ cout << vec[i] << '\n'; } return 0; }