#include // Replace needed headers for speed up // @require ./modular_arithmetics.cc 👇👇 using Int = int; // using Int = long long; struct ModInt { static constexpr Int mod = 1e9 + 7; Int v; ModInt(Int _v = 0) : v(set(_v)) {} ModInt(const ModInt &r) : v(set(r.v)) {} inline static Int set(const Int x) { return (x < 0 ? (x % mod) + mod : x % mod); } inline void set() { v = set(v); } bool operator<(ModInt r) const { return v < r.v; } bool operator>(ModInt r) const { return r.v < v; } bool operator==(ModInt r) const { return v == r.v; } bool operator!= (ModInt r) const { return v != r.v; } ModInt operator-() const { return ModInt(v ? mod - v : v); } ModInt &operator+=(ModInt r) { (v += r.v) %= mod; return *this; } ModInt &operator-=(ModInt r) { (v -= r.v - mod) %= mod; return *this; } ModInt &operator*=(ModInt r) { v = (__uint128_t(v) * r.v) % mod; return *this; } ModInt &operator/=(ModInt r) { *this *= r.inv(); return *this; } ModInt &operator=(const ModInt &r) { if (this != &r) v = set(r.v); return *this; } ModInt inv() const { Int u = 1, tv = v, s = 0, t = mod; while (t) { Int q = tv / t; std::swap(tv -= q * t, t); std::swap(u -= q * s, s); } return ModInt(u < 0 ? u + mod : u); } ModInt pow(Int e) { ModInt a = *this, x(1); for ( ; 0 < e; e >>= 1) { if (e & 1) x *= a; a *= a; } return x; } inline ModInt pow(ModInt &e) { return pow(e.v); } }; ModInt operator+(ModInt l, ModInt r) { return l += r; } ModInt operator-(ModInt l, ModInt r) { return l -= r; } ModInt operator*(ModInt l, ModInt r) { return l *= r; } ModInt operator/(ModInt l, ModInt r) { return l /= r; } std::ostream &operator<<(std::ostream &os, const ModInt &r) { return os << r.v; } std::istream &operator>>(std::istream &is, ModInt &r) { is >> r.v; r.set();return is; } std::vector Inverse(const Int n = ModInt::mod - 1) { constexpr Int mod = ModInt::mod; std::vector inv(n + 1); inv[1].v = 1; for (Int a = 2; a <= n; ++a) inv[a] = inv[mod % a] * ModInt(mod - mod / a); return inv; } // -------------8<------- start of library -------8<------------------------ struct Combination { const int n; std::vector fact, inv_f; // MultisetCoefficient を使用する場合は n = 2 * _n とする Combination(int _n) : n(_n), fact(n + 1), inv_f(n + 1) { fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = i * fact[i - 1]; inv_f[n] = fact[n].inv(); for (int i = n; 1 <= i; --i) inv_f[i - 1] = i * inv_f[i]; } ModInt Factorial(const int &n) const { return fact[n]; } ModInt InverseFactorial(const int &n) const { return inv_f[n]; } ModInt Permuation(const int &n, const int &k) const { if (k < 0 || n < k) return ModInt(0); else return fact[n] * inv_f[n - k]; } ModInt BinomialCoefficient(const int &n, const int &k) const { if (n < 0 || k < 0 || n < k) return ModInt(0); else return fact[n] * inv_f[k] * inv_f[n - k]; } ModInt MultisetCoefficient(const int &n, const int &k) const { if (n < 0 || k < 0) return ModInt(0); else return k == 0 ? 1 : BinomialCoefficient(n + k - 1, k); } }; // -------------8<------- end of library ---------8------------------------- int main() { Combination cm(2 * 1e6); int T, n, k; char c; scanf("%d\n", &T); while (T--) { scanf("%c(%d,%d)\n", &c, &n, &k); if (c == 'C') printf("%d\n", cm.BinomialCoefficient(n, k).v); else if (c == 'P') printf("%d\n", cm.Permuation(n, k).v); else if (c == 'H') printf("%d\n", cm.MultisetCoefficient(n, k).v); } return 0; }