// i = 2 が解けません Ω\ζ°)チーン //#define NDEBUG #include #include #include #include #include #include #include namespace n91 { using i32 = std::int32_t; using i64 = std::int64_t; using u32 = std::uint32_t; using u64 = std::uint64_t; using isize = std::ptrdiff_t; using usize = std::size_t; struct rep { struct itr { usize i; constexpr itr(const usize i) noexcept : i(i) {} void operator++() noexcept { ++i; } constexpr usize operator*() const noexcept { return i; } constexpr bool operator!=(const itr x) const noexcept { return i != x.i; } }; const itr f, l; constexpr rep(const usize f, const usize l) noexcept : f(std::min(f, l)), l(l) {} constexpr auto begin() const noexcept { return f; } constexpr auto end() const noexcept { return l; } }; struct revrep { struct itr { usize i; constexpr itr(const usize i) noexcept : i(i) {} void operator++() noexcept { --i; } constexpr usize operator*() const noexcept { return i; } constexpr bool operator!=(const itr x) const noexcept { return i != x.i; } }; const itr f, l; constexpr revrep(const usize f, const usize l) noexcept : f(l - 1), l(std::min(f, l) - 1) {} constexpr auto begin() const noexcept { return f; } constexpr auto end() const noexcept { return l; } }; template auto md_vec(const usize n, const T& value) { return std::vector(n, value); } template auto md_vec(const usize n, Args... args) { return std::vector(n, md_vec(args...)); } template constexpr T difference(const T& a, const T& b) noexcept { return a < b ? b - a : a - b; } template void chmin(T& a, const T& b) noexcept { if (b < a) a = b; } template void chmax(T& a, const T& b) noexcept { if (a < b) a = b; } template class rec_lambda { F f; public: rec_lambda(F&& f) : f(std::move(f)) {} template auto operator()(Args &&... args) const { return f(*this, std::forward(args)...); } }; template auto make_rec(F&& f) { return rec_lambda(std::move(f)); } template T scan() { T ret; std::cin >> ret; return ret; } constexpr char eoln = '\n'; template T ceildiv(const T& l, const T& r) { return l / r + (l % r != 0 ? 1 : 0); } } // namespace n91 #include namespace n91 { template class mint_utils { using usize = std::size_t; std::vector fact_, inv_fact_; public: mint_utils() : fact_(), inv_fact_() {} explicit mint_utils(const usize n) : fact_(n + 1), inv_fact_(n + 1) { fact_[0] = 1; for (usize i = 0; i != n; i += 1) { fact_[i + 1] = fact_[i] * (i + 1); } inv_fact_[n] = T(1) / fact_[n]; for (usize i = n; i != 0; i -= 1) { inv_fact_[i - 1] = inv_fact_[i] * i; } } T fact(const usize n) const { return fact_[n]; } T inv_fact(const usize n) const { return inv_fact_[n]; } T binom(const usize n, const usize r) const { return fact_[n] * inv_fact_[r] * inv_fact_[n - r]; } T homo(const usize n, const usize r) const { return fact_[n + r - 1] * inv_fact_[n] * inv_fact_[r - 1]; } }; } // namespace n91 #include #include #include #include #include namespace n91 { void main_() { using mint = atcoder::static_modint<998244353>; //* std::ios::sync_with_stdio(false); std::cin.tie(nullptr); //*/ const usize n = scan(); const usize m = scan(); std::vector b(n), c(n); for (const usize i : rep(0, n)) { std::cin >> b[i] >> c[i]; } if (m == 2) { for (const usize i : rep(0, n)) { if (i == 0) { std::cout << 1 << eoln; } else if (i == 1) { if (b[0] == b[1]) { std::cout << 0 << eoln; } else { std::cout << 2 << eoln; } } else { std::cout << 0 << eoln; } } return; } const auto parity = [m](const usize b) -> bool { bool ret = false; std::vector used(m, false); for (const usize i : rep(0, m)) { if (used[i]) { continue; } usize count = 0; usize v = i; do { used[v] = true; count += 1; v = (b * v) % m; } while (v != i); if (count % 2 == 0) { ret = !ret; } } return ret; }; std::vector parities(m); { const usize pr = atcoder::internal::primitive_root_constexpr(m); std::vector log(m); { usize v = 1; for (const usize i : rep(0, m - 1)) { log[v] = i; v = v * pr % m; } } std::map memo; // gcd, parity for (const usize i : rep(1, m)) { const usize g = std::gcd(m - 1, log[i]); if (!memo.count(g)) { memo[g] = parity(i); } parities[i] = memo[g]; } } for (const usize i : rep(1, m)) { assert(parities[i] == parity(i)); } u64 one = 0, zero = 0; for (const usize i : rep(1, m)) { if (parities[i]) { one += 1; } else { zero += 1; } } one *= m; zero *= m; const mint_utils util(n + 1); std::vector odd, even; { mint one_fact = 1, zero_fact = 1; std::vector one_odd(n + 1), one_even(n + 1), zero_a(n + 1); for (const usize i : rep(0, n + 1)) { if (i % 2 == 0) { one_even[i] = one_fact / util.fact(i); } else { one_odd[i] = one_fact / util.fact(i); } zero_a[i] = zero_fact / util.fact(i); one_fact *= mint(one) - i; zero_fact *= mint(zero) - i; } odd = atcoder::convolution(one_odd, zero_a); even = atcoder::convolution(one_even, zero_a); } bool current_p = false; for (const usize i : rep(0, n)) { if (parities[c[i]]) { current_p = !current_p; } if (i == 0) { std::cout << 1 << eoln; continue; } if (current_p) { std::cout << (odd[i + 1] * util.fact(i + 1)).val() << eoln; } else { std::cout << (even[i + 1] * util.fact(i + 1)).val() << eoln; } } } } // namespace n91 int main() { n91::main_(); return 0; }