#line 1 "test/modint_util.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/117" #line 2 "utility/int_alias.cpp" #include #include using i32 = std::int32_t; using u32 = std::uint32_t; using i64 = std::int64_t; using u64 = std::uint64_t; using i128 = __int128_t; using u128 = __uint128_t; using isize = std::ptrdiff_t; using usize = std::size_t; #line 2 "math/rem_euclid.cpp" template constexpr T rem_euclid(T value, const T& mod) { return (value %= mod) >= 0 ? value : mod; } #line 4 "math/static_modint.cpp" #include #include namespace static_modint_internal { constexpr u32 totient(u32 x) noexcept { u32 ret = x; for (u32 i = 2; i * i <= x; ++i) { if (x % i == 0) { ret /= i; ret *= i - 1; while (x % i == 0) x /= i; } } if (x > 1) { ret /= x; ret *= x - 1; } return ret; } } template * = nullptr> class StaticModint { using Mint = StaticModint; static inline constexpr u32 PHI = static_modint_internal::totient(MOD); u32 v; public: static constexpr u32 mod() noexcept { return MOD; } template and std::is_integral_v>* = nullptr> static constexpr T normalize(const T x) noexcept { return rem_euclid(x, MOD); } template and std::is_integral_v>* = nullptr> static constexpr T normalize(const T x) noexcept { return x % MOD; } constexpr StaticModint() noexcept: v(0) { } template explicit constexpr StaticModint(const T x) noexcept: v(normalize(x)) { } template static constexpr Mint raw(const T x) noexcept { Mint ret; ret.v = x; return ret; } constexpr u32 get() const noexcept { return v; } constexpr Mint neg() const noexcept { return raw(v == 0 ? 0 : MOD - v); } constexpr Mint operator - () const noexcept { return neg(); } constexpr Mint inv() const noexcept { return pow(PHI - 1); } constexpr Mint operator ~ () const noexcept { return inv(); } constexpr Mint pow(u64 exp) const noexcept { Mint ret(1), mult(*this); for (; exp > 0; exp >>= 1) { if (exp & 1) ret *= mult; mult *= mult; } return ret; } constexpr Mint operator + (const Mint& rhs) const noexcept { return Mint(*this) += rhs; } constexpr Mint& operator += (const Mint& rhs) noexcept { if ((v += rhs.v) >= MOD) v -= MOD; return *this; } constexpr Mint operator - (const Mint& rhs) const noexcept { return Mint(*this) -= rhs; } constexpr Mint& operator -= (const Mint& rhs) noexcept { if (v < rhs.v) v += MOD; v -= rhs.v; return *this; } constexpr Mint operator * (const Mint& rhs) const noexcept { return Mint(*this) *= rhs; } constexpr Mint& operator *= (const Mint& rhs) noexcept { v = (u64) v * rhs.v % MOD; return *this; } constexpr Mint operator / (const Mint& rhs) const noexcept { return Mint(*this) /= rhs; } constexpr Mint& operator /= (const Mint& rhs) noexcept { return *this *= rhs.inv(); } constexpr bool operator == (const Mint& rhs) const noexcept { return v == rhs.v; } constexpr bool operator != (const Mint& rhs) const noexcept { return v != rhs.v; } friend std::ostream& operator << (std::ostream& stream, const Mint& rhs) { return stream << rhs.v; } }; #line 3 "utility/rep.cpp" #include class rep { struct Iter { usize itr; constexpr Iter(const usize pos) noexcept: itr(pos) { } constexpr void operator ++ () noexcept { ++itr; } constexpr bool operator != (const Iter& other) const noexcept { return itr != other.itr; } constexpr usize operator * () const noexcept { return itr; } }; const Iter first, last; public: explicit constexpr rep(const usize first, const usize last) noexcept: first(first), last(std::max(first, last)) { } constexpr Iter begin() const noexcept { return first; } constexpr Iter end() const noexcept { return last; } }; #line 3 "bit/ceil_log2.cpp" constexpr u64 ceil_log2(const u64 x) { u64 e = 0; while (((u64) 1 << e) < x) ++e; return e; } #line 4 "utility/auto_realloc.cpp" #include #include template class AutoRealloc { using R = typename decltype(std::declval().operator()(std::declval()))::value_type; F func; std::vector data; public: explicit AutoRealloc(F&& func): func(std::forward(func)), data() { } explicit AutoRealloc(F&& func, const usize capacity): func(std::forward(func)) { reserve(capacity); } void reserve(const usize size) { if (data.size() < size) { const usize pow2 = ((usize) 1 << ceil_log2(size)); data = func(pow2); } } R operator [] (const usize i) { reserve(i + 1); return data[i]; } }; #line 6 "math/modint_util.cpp" #include template struct ModintUtil { static inline auto fact = AutoRealloc([](const usize n) { std::vector ret(n); ret[0] = M(1); for (const usize i: rep(1, n)) { ret[i] = ret[i - 1] * M(i); } return ret; }); static inline auto inv = AutoRealloc([](const usize n) { std::vector ret(n); if (n == 1) return ret; ret[1] = M(1); for (const usize i: rep(2, n)) { ret[i] = -M(M::mod() / i) * ret[M::mod() % i]; } return ret; }); static inline auto inv_fact = AutoRealloc([](const usize n) { std::vector ret(n); ret[0] = M(1); for (const usize i: rep(1, n)) { ret[i] = ret[i - 1] * inv[i]; } return ret; }); static M binom(const usize n, const usize k) { assert(k <= n); return fact[n] * inv_fact[n - k] * inv_fact[k]; } static M factpow(const usize n, const usize k) { assert(k <= n); return fact[n] * inv_fact[n - k]; } static M homo(const usize n, const usize k) { if (n == 0 && k == 0) return M(1); return binom(n + k - 1, k); } }; #line 5 "test/modint_util.test.cpp" #include using Fp = StaticModint<1000000007>; using FpUtil = ModintUtil; int main() { usize T; std::cin >> T; while (T--) { char type, dust; usize N, K; std::cin >> type >> dust >> N >> dust >> K >> dust; if (type == 'C') { if (N < K) std::cout << 0 << '\n'; else std::cout << FpUtil::binom(N, K) << '\n'; } if (type == 'P') { if (N < K) std::cout << 0 << '\n'; else std::cout << FpUtil::factpow(N, K) << '\n'; } if (type == 'H') { if (N == 0 && K > 0) std::cout << 0 << '\n'; else std::cout << FpUtil::homo(N, K) << '\n'; } } return 0; }