/* -*- coding: utf-8 -*- * * 117.cc: No.117 組み合わせの数 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 1000000; typedef long long ll; const ll MOD = 1000000007; /* typedef */ /* global variables */ ll fracs[MAX_N + 1], invfs[MAX_N + 1]; /* subroutines */ ll powmod(ll a, int n) { // a^n % MOD ll pm = 1; while (n > 0) { if (n & 1) pm = (pm * a) % MOD; a = (a * a) % MOD; n >>= 1; } return pm; } inline ll npk(int n, int k) { // nPk % MOD if (n < k) return 0; return fracs[n] * invfs[n - k] % MOD; } inline ll nck(int n, int k) { // nCk % MOD if (n < k) return 0; return (fracs[n] * invfs[n - k] % MOD) * invfs[k] % MOD; } inline ll nhk(int n, int k) { // nHk % MOD return nck(n + k - 1, k); } /* main */ int main() { fracs[0] = invfs[0] = 1; for (int i = 1; i <= MAX_N; i++) { fracs[i] = (fracs[i - 1] * i) % MOD; invfs[i] = powmod(fracs[i], MOD - 2); } int tn; cin >> tn; while (tn--) { string s; cin >> s; int n = 0, k = 0, pos = 2; while (s[pos] != ',') n = n * 10 + s[pos++] - '0'; pos++; while (s[pos] != ')') k = k * 10 + s[pos++] - '0'; //printf("%c(%d,%d)\n", s[0], n, k); ll ans; switch (s[0]) { case 'C': ans = nck(n, k); break; case 'P': ans = npk(n, k); break; case 'H': ans = nhk(n, k); break; } printf("%lld\n", ans); } return 0; }