#include 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; } struct Combination { const int n; std::vector fact, inv_f; Combination(int _n) : n(_n), fact(n + 1), inv_f(n + 1) { fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = ModInt(i) * fact[i - 1]; inv_f[n] = fact[n].inv(); for (int i = n - 1; 0 <= i; --i) inv_f[i] = ModInt(i + 1) * inv_f[i + 1]; } ModInt Factorial(const ModInt &n) const { return fact[n.v]; } ModInt InverseFactorial(const ModInt &n) const { return inv_f[n.v]; } ModInt Permuation(const ModInt &n, const ModInt &k) const { if (k < 0 || n < k) return ModInt(0); else return fact[n.v] * inv_f[n.v - k.v]; } ModInt BinomialCoefficient(const ModInt &n, const ModInt &k) const { if (n < 0 || k < 0 || n < k) return ModInt(0); else return fact[n.v] * inv_f[k.v] * inv_f[n.v - k.v]; } ModInt MultisetCoefficient(const ModInt &n, const ModInt &k) const { if (n < 0 || k < 0) return ModInt(0); else return k.v == 0 ? 1 : BinomialCoefficient(n + k - 1, k); } }; int main() { Combination cm(1e6); int T; char c; ModInt n, k; scanf("%d\n", &T); while (T--) { scanf("%c(%d,%d)\n", &c, &n.v, &k.v); 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; }