module main; import std; struct ModInt(long MOD) { private: long val; public: // コンストラクタ // useMod:MODで剰余を取る処理を行うかどうか this(long x, bool useMod = true) { if (useMod) { val = x % MOD; if (val < 0) val += MOD; } else val = x; } @property long value() const { return val; } @property ModInt value(long x) { val = x % MOD; if (val < 0) val += MOD; return this; } // 代入演算子 ModInt opAssign(T)(T x) if (isIntegral!T) { val = x % MOD; if (val < 0) val += MOD; return this; } // 演算代入演算子 ModInt opOpAssign(string op, T)(T rhs) pure nothrow if (is(T : ModInt) && (op == "+" || op == "-" || op == "*" || op == "/")) { static if (op == "+") { val += rhs.val; while (val >= MOD) val -= MOD; } else static if (op == "-") { val -= rhs.val; while (val < 0) val += MOD; } else static if (op == "*") { val = val * rhs.val % MOD; while (val < 0) val += MOD; } else static if (op == "/") { val = val * rhs.inv().val % MOD; while (val < 0) val += MOD; } return this; } ModInt opOpAssign(string op, T)(T rhs) pure nothrow if (isIntegral!T && (op == "+" || op == "-" || op == "*" || op == "/")) { static if (op == "+") { val += rhs; while (val >= MOD) val -= MOD; } else static if (op == "-") { val -= rhs; while (val < 0) val += MOD; } else static if (op == "*") { val = val * rhs % MOD; while (val < 0) val += MOD; } else static if (op == "/") { val = val * Mint(rhs).inv().val % MOD; while (val < 0) val += MOD; } return this; } // 二項演算子 ModInt opBinary(string op, T)(T rhs) pure nothrow if (is(T : ModInt) && (op == "+" || op == "-" || op == "*" || op == "/")) { auto r = this; return r.opOpAssign!op(rhs); } ModInt opBinary(string op, T)(T rhs) pure nothrow if (isIntegral!T && (op == "+" || op == "-" || op == "*" || op == "/")) { auto r = this; return r.opOpAssign!op(rhs); } ModInt opBinaryRight(string op, T)(T lhs) pure nothrow if (isIntegral!T && (op == "+" || op == "-" || op == "*" || op == "/")) { ModInt r = lhs; return r.opOpAssign!op(this); } // 単項演算子 ModInt opUnary(string op)() pure nothrow if (op == "++" || op == "--") { static if (op == "++") { ++val; if (val == MOD) val = 0; } else static if (op == "--") { if (val == 0) val = MOD; --val; } return this; } ModInt opUnary(string op)() pure const nothrow if (op == "-") { if (val == 0) return Mint(0, false); else return Mint(MOD - val, false); } // 等号演算子 bool opEquals(ref const ModInt rhs) @safe pure const nothrow { return val == rhs.val; } bool opEquals(T)(const T rhs) @safe pure const nothrow if (isIntegral!T) { return val == rhs; } // 累乗 ModInt pow(T)(T n) pure const if (isIntegral!T) { ModInt x = n >= 0 ? this : inv(), r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } // MODに関する逆元 ModInt inv() pure const { long a = val, b = MOD, u = 1, v = 0; while (b) { long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return ModInt(u); } string toString() pure nothrow const { return val.to!string; } // 連想配列のためのハッシュ hash_t toHash() const @safe pure nothrow { // MMIX by Donald Knuth return cast(hash_t)(6_364_136_223_846_793_005L * val + 1_442_695_040_888_963_407L); } } immutable MOD = 10L ^^ 9 + 7; alias Mint = ModInt!MOD; immutable N = 2_000_000; // MODを法とする階乗とその逆元 Mint[] fact, inv, factInv; void init() { fact = new Mint[](N + 1), inv = new Mint[](N + 1), factInv = new Mint[](N + 1); fact[0] = fact[1] = 1L; inv[1] = 1L; factInv[0] = factInv[1] = 1L; foreach (i; 2L .. N) { fact[i] = i * fact[i - 1]; inv[i] = -inv[MOD % i] * (MOD / i); factInv[i] = inv[i] * factInv[i - 1]; } } // 二項係数 Mint nCr(long n, long r) { if (n < r || r < 0) return Mint(0, false); return fact[n] * factInv[r] * factInv[n - r]; } // 順列 Mint nPr(long n, long r) { if (n < r || r < 0) return Mint(0, false); return fact[n] * factInv[n - r]; } // 重複組み合わせ Mint nHr(long n, long r) { return nCr(n + r - 1, r); } void main() { init(); // キューの処理 int T = readln.chomp.to!int; foreach (_; 0 .. T) { char c; long n, r; readln.chomp.formattedRead("%c(%d,%d)", c, n, r); if (c == 'C') writeln(nCr(n, r)); else if (c == 'P') writeln(nPr(n, r)); else writeln(nHr(n, r)); } }