// --- base.hpp --- #include using namespace std; #if __has_include() #include using namespace atcoder; #define HAS_ACL 1 #else #define HAS_ACL 0 #endif // --- math/Eratosthenes.hpp --- /// @brief エラトステネスの篩 /// @return n 以下の値が素数であるか /// @pre n >= 0 /// @note O(n log log n) /// @see https://judge.yosupo.jp/submission/412844 vector eratosthenes(int64_t n) { vector res(n + 1, true); res[0] = false; if (n >= 1) res[1] = false; for (int64_t i = 4; i <= n; i += 2) res[i] = false; for (int64_t i = 3; i <= n / i; i += 2) { if (!res[i]) continue; for (int64_t j = i * i; j <= n; j += i * 2) res[j] = false; } return res; } /// @brief 素数リスト /// @return n 以下の素数一覧 /// @pre n >= 0 /// @note O(n log log n) /// @see https://judge.yosupo.jp/submission/412844 vector prime_list(int64_t n) { vector P = eratosthenes(n); vector res; if (n >= 2) res.push_back(2); for (int64_t i = 3; i <= n; i += 2) { if (P[i]) res.push_back(i); } return res; } // --- math/BaseMath.hpp --- /// @brief a の b 乗 /// @pre b >= 0 /// @note O(log b) int64_t power(int64_t a, int64_t b) { assert(b >= 0); int64_t res = 1; while (b > 0) { if (b & 1) res *= a; b >>= 1; if (b) a *= a; } return res; } /// @brief 階乗 /// @pre n >= 0 /// @note O(n) int64_t factorial(int64_t n) { assert(n >= 0); int64_t res = 1; for (int64_t i = 2; i <= n; i++) res *= i; return res; } /// @brief 愚直なnPr /// @pre n >= 0, r >= 0; /// @note O(r) int64_t nPr(int64_t n, int64_t r) { if (r < 0 || n < r) return 0; int64_t res = 1; for (int64_t i = 0; i < r; i++) res *= n - i; return res; } /// @brief 愚直なnCr /// @note O(min(r, n - r)) int64_t nCr(int64_t n, int64_t r) { if (r < 0 || n < r) return 0; r = min(r, n - r); int64_t res = 1; for (int64_t i = 0; i < r; i++) res = res * (n - i) / (i + 1); return res; } /// @brief mod上の a の b 乗 /// @pre b >= 0, mod > 0 /// @note O(log b) int64_t power(int64_t a, int64_t b, int64_t mod) { assert(b >= 0 && mod > 0); int64_t res = 1 % mod; a %= mod; if (a < 0) a += mod; if (mod < INT_MAX) { while (b > 0) { if (b & 1) res = res * a % mod; b >>= 1; if (b) a = a * a % mod; } } else { while (b > 0) { if (b & 1) res = (__int128_t)res * a % mod; b >>= 1; if (b) a = (__int128_t)a * a % mod; } } return res; } /// @brief mod上の階乗 /// @pre n >= 0, mod > 0 /// @note O(n) int64_t factorial(int64_t n, int64_t mod) { assert(n >= 0 && mod > 0); if (n >= mod) return 0; int64_t res = 1 % mod; if (mod < INT_MAX) { for (int64_t i = 2; i <= n; i++) { res = res * i % mod; } } else { for (int64_t i = 2; i <= n; i++) { res = (__int128_t)res * i % mod; } } return res; } /// @brief mod上の愚直なnPr /// @pre n >= 0, mod > 0 /// @note O(r) int64_t nPr(int64_t n, int64_t r, int64_t mod) { assert(mod > 0); if (r < 0 || n < r) return 0; int64_t res = 1 % mod; if (res == 0 || r >= mod) return 0; if (mod < INT_MAX) { if (n < INT_MAX) { for (int64_t i = 0; i < r; i++) { res = res * (n - i) % mod; } } else { for (int64_t i = 0; i < r; i++) { res = res * ((n - i) % mod) % mod; } } } else { for (int64_t i = 0; i < r; i++) { res = (__int128_t)res * (n - i) % mod; } } return res; } /// @brief mod上の愚直なnCr /// @pre mod は素数 /// @pre r < mod /// @note O(min(r, n - r) + log mod) int64_t nCr(int64_t n, int64_t r, int64_t mod) { if (r < 0 || n < r) return 0; assert(mod >= 2); r = min(r, n - r); assert(r < mod); int64_t a = 1 % mod, b = 1; if (mod < INT_MAX) { if (n < INT_MAX) { for (int64_t i = 0; i < r; i++) { a = a * (n - i) % mod; b = b * (i + 1) % mod; } } else { for (int64_t i = 0; i < r; i++) { a = a * ((n - i) % mod) % mod; b = b * (i + 1) % mod; } } if (a == 0) return 0; return a * power(b, mod - 2, mod) % mod; } for (int64_t i = 0; i < r; i++) { a = (__int128_t)a * (n - i) % mod; b = (__int128_t)b * (i + 1) % mod; } if (a == 0) return 0; return (__int128_t)a * power(b, mod - 2, mod) % mod; } /// @brief 10進数の右から k 桁目を返す /// @pre x >= 0, k >= 0 /// @note O(k) int64_t digit(int64_t x, int k) { assert(x >= 0 && k >= 0); while (k--) x /= 10; return x % 10; } // --- math/ModMath.hpp --- /// @brief mod上の計算 /// @pre modは素数 /// @note 大体3e7の構築が1秒です 最初に全部構築したほうが早い struct ModMath { int64_t mod; vector inv_, fact_, ifact_; /// @brief 初期化 /// @param n 事前計算する最大値 /// @param m 法 /// @pre 0 <= n < m /// @pre m は素数 /// @note O(n) ModMath(int n = 0, int64_t m = 998244353) { init(n, m); } /// @brief 初期化 /// @param n 事前計算する最大値 /// @param m 法 /// @pre 0 <= n < m /// @pre m は素数 /// @note O(n) void init(int n, int64_t m = 998244353) { n = max(2, n); mod = m; assert(2 <= mod && mod < INT_MAX && 0 <= n && n < mod); inv_.assign(n + 1, 1); fact_.assign(n + 1, 1); ifact_.assign(n + 1, 1); for (int64_t i = 2; i <= n; i++) { inv_[i] = mod - (mod / i) * inv_[mod % i] % mod; fact_[i] = fact_[i - 1] * i % mod; ifact_[i] = ifact_[i - 1] * inv_[i] % mod; } } /// @brief 事前計算範囲の拡張 /// @param n 事前計算する最大値 /// @pre n < mod /// @note 拡張時 O(n - 現在の最大値) 非拡張時 O(1) void extend(int n) { assert(0 <= n && n < mod); n = max(2, n); int old = fact_.size(); if (n < old) return; inv_.resize(n + 1); fact_.resize(n + 1); ifact_.resize(n + 1); for (int64_t i = old; i <= n; i++) { inv_[i] = mod - (mod / i) * inv_[mod % i] % mod; fact_[i] = fact_[i - 1] * i % mod; ifact_[i] = ifact_[i - 1] * inv_[i] % mod; } } /// @brief 逆元 /// @pre n >= 1 /// @note O(1)(非拡張時) int64_t inv(int n) { assert(n >= 1); extend(n); return inv_[n]; } /// @brief 階乗 /// @pre n >= 0 /// @note O(1)(非拡張時) int64_t fact(int n) { assert(n >= 0); extend(n); return fact_[n]; } /// @brief 階乗の逆元 /// @pre n >= 0 /// @note O(1)(非拡張時) int64_t ifact(int n) { assert(n >= 0); extend(n); return ifact_[n]; } /// @brief nPr /// @pre n >= 0 /// @note O(1)(非拡張時) int64_t nPr(int n, int r) { assert(n >= 0); if (r < 0 || n < r) return 0; extend(n); return fact_[n] * ifact_[n - r] % mod; } /// @brief nCr /// @pre n >= 0 /// @note O(1)(非拡張時) int64_t nCr(int n, int r) { assert(n >= 0); if (r < 0 || n < r) return 0; extend(n); return fact_[n] * ifact_[r] % mod * ifact_[n - r] % mod; } /// @brief nHr /// @pre n >= 0, r >= 0 /// @note O(1)(非拡張時) int64_t nHr(int n, int r) { assert(n >= 0 && r >= 0); if (n == 0) return r == 0; assert((int64_t)n + r - 1 < mod); return nCr(n + r - 1, r); } /// @brief 多項係数 /// @param v 各項の個数 /// @pre すべての項は0以上であること /// @note O(v.size()) (非拡張時) template int64_t multinomial(const vector &v) { int64_t n = 0; for (auto x : v) { assert(x >= 0); n += x; } assert(n < mod); extend(n); int64_t res = fact_[n]; for (auto x : v) res = res * ifact_[x] % mod; return res; } }; // --- solution --- int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ModMath MM(1000000, 1000000007); int T; cin >> T; while (T--) { string S; cin >> S; char C = S[0]; int i = 2, N = 0, K = 0; while (S[i] != ',') { N = N * 10 + S[i] - '0'; i++; } i++; while (S[i] != ')') { K = K * 10 + S[i] - '0'; i++; } if (C == 'C') cout << MM.nCr(N, K) << '\n'; if (C == 'P') cout << MM.nPr(N, K) << '\n'; if (C == 'H') cout << MM.nHr(N, K) << '\n'; } }