This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "tools/binomial_product.hpp"template <std::ranges::input_range R>
requires tools::modint<std::tuple_element_t<0, std::ranges::range_value_t<R>>>
&& std::same_as<std::tuple_element_t<1, std::ranges::range_value_t<R>>, std::tuple_element_t<0, std::ranges::range_value_t<R>>>
&& std::integral<std::tuple_element_t<2, std::ranges::range_value_t<R>>>
&& std::integral<std::tuple_element_t<3, std::ranges::range_value_t<R>>>
tools::fps<std::tuple_element_t<0, std::ranges::range_value_t<R>>> binomial_product(R&& f, int n);
Given $(a_i, b_i, c_i, d_i) \in \mathbb{Z}/M\mathbb{Z} \times \mathbb{Z}/M\mathbb{Z} \times \mathbb{Z}_{\geqslant 0} \times \mathbb{Z}_{\geqslant 0}$ for $i = 0, 1, \ldots, m - 1$, it returns the first $n$ terms of $\prod_{i = 0}^{m - 1} (a_i + b_i x^{c_i})^{d_i}$.
#ifndef TOOLS_BINOMIAL_PRODUCT_HPP
#define TOOLS_BINOMIAL_PRODUCT_HPP
#include <algorithm>
#include <cassert>
#include <concepts>
#include <ranges>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
#include "tools/ceil.hpp"
#include "tools/fact_mod_cache.hpp"
#include "tools/fps.hpp"
#include "tools/modint.hpp"
#include "tools/pow_mod_cache.hpp"
namespace tools {
template <std::ranges::input_range R>
requires tools::modint<std::tuple_element_t<0, std::ranges::range_value_t<R>>>
&& std::same_as<std::tuple_element_t<1, std::ranges::range_value_t<R>>, std::tuple_element_t<0, std::ranges::range_value_t<R>>>
&& std::integral<std::tuple_element_t<2, std::ranges::range_value_t<R>>>
&& std::integral<std::tuple_element_t<3, std::ranges::range_value_t<R>>>
auto binomial_product(R&& f, const int n) {
using M = std::tuple_element_t<0, std::ranges::range_value_t<R>>;
using F = tools::fps<M>;
assert(n >= 0);
assert(std::ranges::all_of(std::views::iota(2, std::max(2, n)), [](const auto i) { return M::mod() % i != 0; }));
M multiplier(1);
int offset = 0;
std::vector<std::tuple<M, int, int>> factors;
for (const auto& [a, b, c, d] : f) {
assert(c >= 0);
assert(d >= 0);
if (d == 0) {
continue;
}
if (c == 0) {
multiplier *= (a + b).pow(d);
continue;
}
if (a.val() == 0) {
if (d >= tools::ceil(n - offset, c)) {
return F(n);
}
multiplier *= b.pow(d);
offset += c * d;
continue;
}
multiplier *= a.pow(d);
factors.emplace_back(b / a, c, d);
}
assert((factors | std::views::transform([](const auto& factor) { return std::get<1>(factor); }) | std::ranges::to<std::set>()).size() == factors.size());
tools::fact_mod_cache<M> cache;
F res(n - offset);
for (const auto& [b, c, d] : factors) {
tools::pow_mod_cache<M> pow_b(b);
for (int i = 1; c < tools::ceil(n - offset, i); ++i) {
res[c * i] += M(i % 2 == 0 ? -d : d) * pow_b[i] * cache.inv(i);
}
}
res.exp_inplace();
res *= multiplier;
res <<= offset;
return res;
}
}
#endif
#line 1 "tools/binomial_product.hpp"
#include <algorithm>
#include <cassert>
#include <concepts>
#include <ranges>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
#line 1 "tools/ceil.hpp"
#line 5 "tools/ceil.hpp"
#include <type_traits>
#line 1 "tools/non_bool_integral.hpp"
#line 1 "tools/integral.hpp"
#line 1 "tools/is_integral.hpp"
#line 5 "tools/is_integral.hpp"
namespace tools {
template <typename T>
struct is_integral : std::is_integral<T> {};
template <typename T>
inline constexpr bool is_integral_v = tools::is_integral<T>::value;
}
#line 5 "tools/integral.hpp"
namespace tools {
template <typename T>
concept integral = tools::is_integral_v<T>;
}
#line 7 "tools/non_bool_integral.hpp"
namespace tools {
template <typename T>
concept non_bool_integral = tools::integral<T> && !std::same_as<std::remove_cv_t<T>, bool>;
}
#line 1 "tools/is_unsigned.hpp"
#line 5 "tools/is_unsigned.hpp"
namespace tools {
template <typename T>
struct is_unsigned : std::is_unsigned<T> {};
template <typename T>
inline constexpr bool is_unsigned_v = tools::is_unsigned<T>::value;
}
#line 8 "tools/ceil.hpp"
namespace tools {
template <tools::non_bool_integral M, tools::non_bool_integral N>
constexpr std::common_type_t<M, N> ceil(const M x, const N y) noexcept {
assert(y != 0);
if (y >= 0) {
if (x > 0) {
return (x - 1) / y + 1;
} else {
if constexpr (tools::is_unsigned_v<std::common_type_t<M, N>>) {
return 0;
} else {
return x / y;
}
}
} else {
if (x >= 0) {
if constexpr (tools::is_unsigned_v<std::common_type_t<M, N>>) {
return 0;
} else {
return x / y;
}
} else {
return (x + 1) / y + 1;
}
}
}
}
#line 1 "tools/fact_mod_cache.hpp"
#line 6 "tools/fact_mod_cache.hpp"
#include <cmath>
#include <iterator>
#line 1 "tools/is_prime.hpp"
#include <array>
#line 1 "tools/prod_mod.hpp"
#line 1 "tools/uint128_t.hpp"
#line 1 "tools/detail/int128_t_and_uint128_t.hpp"
#line 6 "tools/detail/int128_t_and_uint128_t.hpp"
#include <cstddef>
#include <cstdint>
#include <functional>
#include <iostream>
#include <limits>
#include <string>
#include <string_view>
#line 1 "tools/abs.hpp"
#line 7 "tools/abs.hpp"
namespace tools {
namespace detail::abs {
template <typename T>
struct impl {
constexpr decltype(auto) operator()(const T x) const noexcept(noexcept(std::abs(x))) {
return std::abs(x);
}
};
}
template <typename T>
constexpr decltype(auto) abs(T&& x) noexcept(noexcept(tools::detail::abs::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x)))) {
return tools::detail::abs::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x));
}
}
#line 1 "tools/bit_ceil.hpp"
#include <bit>
#line 1 "tools/is_signed.hpp"
#line 5 "tools/is_signed.hpp"
namespace tools {
template <typename T>
struct is_signed : std::is_signed<T> {};
template <typename T>
inline constexpr bool is_signed_v = tools::is_signed<T>::value;
}
#line 1 "tools/make_unsigned.hpp"
#line 5 "tools/make_unsigned.hpp"
namespace tools {
template <typename T>
struct make_unsigned : std::make_unsigned<T> {};
template <typename T>
using make_unsigned_t = typename tools::make_unsigned<T>::type;
}
#line 12 "tools/bit_ceil.hpp"
namespace tools {
namespace detail::bit_ceil {
template <tools::non_bool_integral T>
struct impl {
constexpr T operator()(const T x) const noexcept(noexcept(impl<tools::make_unsigned_t<T>>{}(x))) requires tools::is_signed_v<T> {
assert(x >= 0);
return impl<tools::make_unsigned_t<T>>{}(x);
}
constexpr T operator()(const T x) const noexcept(noexcept(std::bit_ceil(x))) requires tools::is_unsigned_v<T> {
return std::bit_ceil(x);
}
};
}
template <typename T>
constexpr decltype(auto) bit_ceil(T&& x) noexcept(noexcept(tools::detail::bit_ceil::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x)))) {
return tools::detail::bit_ceil::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x));
}
}
#line 1 "tools/bit_floor.hpp"
#line 12 "tools/bit_floor.hpp"
namespace tools {
namespace detail::bit_floor {
template <tools::non_bool_integral T>
struct impl {
constexpr T operator()(const T x) const noexcept(noexcept(impl<tools::make_unsigned_t<T>>{}(x))) requires tools::is_signed_v<T> {
assert(x >= 0);
return impl<tools::make_unsigned_t<T>>{}(x);
}
constexpr T operator()(const T x) const noexcept(noexcept(std::bit_floor(x))) requires tools::is_unsigned_v<T> {
return std::bit_floor(x);
}
};
}
template <typename T>
constexpr decltype(auto) bit_floor(T&& x) noexcept(noexcept(tools::detail::bit_floor::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x)))) {
return tools::detail::bit_floor::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x));
}
}
#line 1 "tools/bit_width.hpp"
#line 12 "tools/bit_width.hpp"
namespace tools {
namespace detail::bit_width {
template <tools::non_bool_integral T>
struct impl {
constexpr int operator()(const T x) const noexcept(noexcept(impl<tools::make_unsigned_t<T>>{}(x))) requires tools::is_signed_v<T> {
assert(x >= 0);
return impl<tools::make_unsigned_t<T>>{}(x);
}
constexpr int operator()(const T x) const noexcept(noexcept(std::bit_width(x))) requires tools::is_unsigned_v<T> {
return std::bit_width(x);
}
};
}
template <typename T>
constexpr decltype(auto) bit_width(T&& x) noexcept(noexcept(tools::detail::bit_width::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x)))) {
return tools::detail::bit_width::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x));
}
}
#line 1 "tools/countr_zero.hpp"
#line 14 "tools/countr_zero.hpp"
namespace tools {
namespace detail::countr_zero {
template <tools::non_bool_integral T>
struct impl {
constexpr int operator()(const T x) const noexcept(noexcept(impl<tools::make_unsigned_t<T>>{}(x))) requires tools::is_signed_v<T> {
assert(x >= 0);
return std::min(impl<tools::make_unsigned_t<T>>{}(x), std::numeric_limits<T>::digits);
}
constexpr int operator()(const T x) const noexcept(noexcept(std::countr_zero(x))) requires tools::is_unsigned_v<T> {
return std::countr_zero(x);
}
};
}
template <typename T>
constexpr decltype(auto) countr_zero(T&& x) noexcept(noexcept(tools::detail::countr_zero::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x)))) {
return tools::detail::countr_zero::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x));
}
}
#line 1 "tools/gcd.hpp"
#include <numeric>
#line 7 "tools/gcd.hpp"
namespace tools {
namespace detail::gcd {
template <typename M, typename N>
struct impl {
constexpr decltype(auto) operator()(const M m, const N n) const noexcept(noexcept(std::gcd(m, n))) {
return std::gcd(m, n);
}
};
}
template <typename M, typename N>
constexpr decltype(auto) gcd(M&& m, N&& n) noexcept(noexcept(tools::detail::gcd::impl<std::remove_cvref_t<M>, std::remove_cvref_t<N>>{}(std::forward<M>(m), std::forward<N>(n)))) {
return tools::detail::gcd::impl<std::remove_cvref_t<M>, std::remove_cvref_t<N>>{}(std::forward<M>(m), std::forward<N>(n));
}
}
#line 1 "tools/has_single_bit.hpp"
#line 12 "tools/has_single_bit.hpp"
namespace tools {
namespace detail::has_single_bit {
template <tools::non_bool_integral T>
struct impl {
constexpr bool operator()(const T x) const noexcept(noexcept(impl<tools::make_unsigned_t<T>>{}(x))) requires tools::is_signed_v<T> {
assert(x >= 0);
return impl<tools::make_unsigned_t<T>>{}(x);
}
constexpr bool operator()(const T x) const noexcept(noexcept(std::has_single_bit(x))) requires tools::is_unsigned_v<T> {
return std::has_single_bit(x);
}
};
}
template <typename T>
constexpr decltype(auto) has_single_bit(T&& x) noexcept(noexcept(tools::detail::has_single_bit::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x)))) {
return tools::detail::has_single_bit::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x));
}
}
#line 1 "tools/hash_combine.hpp"
#line 6 "tools/hash_combine.hpp"
// Source: https://github.com/google/cityhash/blob/f5dc54147fcce12cefd16548c8e760d68ac04226/src/city.h
// License: MIT
// Author: Google Inc.
// Copyright (c) 2011 Google, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
namespace tools {
template <typename T>
void hash_combine(std::size_t& seed, const T& v) {
static const std::hash<T> hasher;
static constexpr std::size_t k_mul = 0x9ddfea08eb382d69ULL;
std::size_t a = (hasher(v) ^ seed) * k_mul;
a ^= (a >> 47);
std::size_t b = (seed ^ a) * k_mul;
b ^= (b >> 47);
seed = b * k_mul;
}
}
#line 1 "tools/make_signed.hpp"
#line 5 "tools/make_signed.hpp"
namespace tools {
template <typename T>
struct make_signed : std::make_signed<T> {};
template <typename T>
using make_signed_t = typename tools::make_signed<T>::type;
}
#line 1 "tools/now.hpp"
#include <chrono>
namespace tools {
inline long long now() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
}
#line 1 "tools/popcount.hpp"
#line 12 "tools/popcount.hpp"
namespace tools {
namespace detail::popcount {
template <tools::non_bool_integral T>
struct impl {
constexpr int operator()(const T x) const noexcept(noexcept(impl<tools::make_unsigned_t<T>>{}(x))) requires tools::is_signed_v<T> {
assert(x >= 0);
return impl<tools::make_unsigned_t<T>>{}(x);
}
constexpr int operator()(const T x) const noexcept(noexcept(std::popcount(x))) requires tools::is_unsigned_v<T> {
return std::popcount(x);
}
};
}
template <typename T>
constexpr decltype(auto) popcount(T&& x) noexcept(noexcept(tools::detail::popcount::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x)))) {
return tools::detail::popcount::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x));
}
}
#line 31 "tools/detail/int128_t_and_uint128_t.hpp"
namespace tools {
using uint128_t = unsigned __int128;
using int128_t = __int128;
template <>
struct is_integral<tools::int128_t> : std::true_type {};
template <>
struct is_integral<tools::uint128_t> : std::true_type {};
template <>
struct is_integral<const tools::int128_t> : std::true_type {};
template <>
struct is_integral<const tools::uint128_t> : std::true_type {};
template <>
struct is_integral<volatile tools::int128_t> : std::true_type {};
template <>
struct is_integral<volatile tools::uint128_t> : std::true_type {};
template <>
struct is_integral<const volatile tools::int128_t> : std::true_type {};
template <>
struct is_integral<const volatile tools::uint128_t> : std::true_type {};
template <>
struct is_signed<tools::int128_t> : std::true_type {};
template <>
struct is_signed<tools::uint128_t> : std::false_type {};
template <>
struct is_signed<const tools::int128_t> : std::true_type {};
template <>
struct is_signed<const tools::uint128_t> : std::false_type {};
template <>
struct is_signed<volatile tools::int128_t> : std::true_type {};
template <>
struct is_signed<volatile tools::uint128_t> : std::false_type {};
template <>
struct is_signed<const volatile tools::int128_t> : std::true_type {};
template <>
struct is_signed<const volatile tools::uint128_t> : std::false_type {};
template <>
struct is_unsigned<tools::int128_t> : std::false_type {};
template <>
struct is_unsigned<tools::uint128_t> : std::true_type {};
template <>
struct is_unsigned<const tools::int128_t> : std::false_type {};
template <>
struct is_unsigned<const tools::uint128_t> : std::true_type {};
template <>
struct is_unsigned<volatile tools::int128_t> : std::false_type {};
template <>
struct is_unsigned<volatile tools::uint128_t> : std::true_type {};
template <>
struct is_unsigned<const volatile tools::int128_t> : std::false_type {};
template <>
struct is_unsigned<const volatile tools::uint128_t> : std::true_type {};
template <>
struct make_signed<tools::int128_t> {
using type = tools::int128_t;
};
template <>
struct make_signed<tools::uint128_t> {
using type = tools::int128_t;
};
template <>
struct make_signed<const tools::int128_t> {
using type = const tools::int128_t;
};
template <>
struct make_signed<const tools::uint128_t> {
using type = const tools::int128_t;
};
template <>
struct make_signed<volatile tools::int128_t> {
using type = volatile tools::int128_t;
};
template <>
struct make_signed<volatile tools::uint128_t> {
using type = volatile tools::int128_t;
};
template <>
struct make_signed<const volatile tools::int128_t> {
using type = const volatile tools::int128_t;
};
template <>
struct make_signed<const volatile tools::uint128_t> {
using type = const volatile tools::int128_t;
};
template <>
struct make_unsigned<tools::int128_t> {
using type = tools::uint128_t;
};
template <>
struct make_unsigned<tools::uint128_t> {
using type = tools::uint128_t;
};
template <>
struct make_unsigned<const tools::int128_t> {
using type = const tools::uint128_t;
};
template <>
struct make_unsigned<const tools::uint128_t> {
using type = const tools::uint128_t;
};
template <>
struct make_unsigned<volatile tools::int128_t> {
using type = volatile tools::uint128_t;
};
template <>
struct make_unsigned<volatile tools::uint128_t> {
using type = volatile tools::uint128_t;
};
template <>
struct make_unsigned<const volatile tools::int128_t> {
using type = const volatile tools::uint128_t;
};
template <>
struct make_unsigned<const volatile tools::uint128_t> {
using type = const volatile tools::uint128_t;
};
namespace detail::int128_t {
constexpr tools::uint128_t parse_unsigned(const std::string_view s) noexcept {
assert(!s.empty());
tools::uint128_t x = 0;
std::size_t i = s[0] == '+';
if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
for (i += 2; i < s.size(); ++i) {
assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
x <<= 4;
if ('0' <= s[i] && s[i] <= '9') {
x |= s[i] - '0';
} else if ('a' <= s[i] && s[i] <= 'f') {
x |= s[i] - 'a' + 10;
} else {
x |= s[i] - 'A' + 10;
}
}
} else {
for (; i < s.size(); ++i) {
assert('0' <= s[i] && s[i] <= '9');
x *= 10;
x += s[i] - '0';
}
}
return x;
}
constexpr tools::int128_t parse_signed(const std::string_view s) noexcept {
assert(!s.empty());
tools::int128_t x = 0;
if (s[0] == '-') {
std::size_t i = 1;
if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
for (i += 2; i < s.size(); ++i) {
assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
x *= 16;
if ('0' <= s[i] && s[i] <= '9') {
x -= s[i] - '0';
} else if ('a' <= s[i] && s[i] <= 'f') {
x -= s[i] - 'a' + 10;
} else {
x -= s[i] - 'A' + 10;
}
}
} else {
for (; i < s.size(); ++i) {
assert('0' <= s[i] && s[i] <= '9');
x *= 10;
x -= s[i] - '0';
}
}
} else {
std::size_t i = s[0] == '+';
if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
for (i += 2; i < s.size(); ++i) {
assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
x <<= 4;
if ('0' <= s[i] && s[i] <= '9') {
x |= s[i] - '0';
} else if ('a' <= s[i] && s[i] <= 'f') {
x |= s[i] - 'a' + 10;
} else {
x |= s[i] - 'A' + 10;
}
}
} else {
for (; i < s.size(); ++i) {
assert('0' <= s[i] && s[i] <= '9');
x *= 10;
x += s[i] - '0';
}
}
}
return x;
}
}
}
#define UINT128_C(c) tools::detail::int128_t::parse_unsigned(#c)
#define INT128_C(c) tools::detail::int128_t::parse_signed(#c)
inline std::istream& operator>>(std::istream& is, tools::uint128_t& x) {
std::string s;
is >> s;
x = tools::detail::int128_t::parse_unsigned(s);
return is;
}
inline std::istream& operator>>(std::istream& is, tools::int128_t& x) {
std::string s;
is >> s;
x = tools::detail::int128_t::parse_signed(s);
return is;
}
inline std::ostream& operator<<(std::ostream& os, tools::uint128_t x) {
std::string s;
if (x > 0) {
while (x > 0) {
s.push_back('0' + x % 10);
x /= 10;
}
} else {
s.push_back('0');
}
std::ranges::reverse(s);
return os << s;
}
inline std::ostream& operator<<(std::ostream& os, tools::int128_t x) {
std::string s;
if (x > 0) {
while (x > 0) {
s.push_back('0' + x % 10);
x /= 10;
}
} else if (x < 0) {
while (x < 0) {
s.push_back('0' + (-(x % 10)));
x /= 10;
}
s.push_back('-');
} else {
s.push_back('0');
}
std::ranges::reverse(s);
return os << s;
}
#if defined(__GLIBCXX__) && defined(__STRICT_ANSI__)
namespace std {
template <>
struct hash<tools::uint128_t> {
std::size_t operator()(const tools::uint128_t& x) const {
static const std::size_t seed = tools::now();
std::size_t hash = seed;
tools::hash_combine(hash, static_cast<std::uint64_t>(x >> 64));
tools::hash_combine(hash, static_cast<std::uint64_t>(x & ((UINT128_C(1) << 64) - 1)));
return hash;
}
};
template <>
struct hash<tools::int128_t> {
std::size_t operator()(const tools::int128_t& x) const {
static std::hash<tools::uint128_t> hasher;
return hasher(static_cast<tools::uint128_t>(x));
}
};
}
#endif
namespace tools {
template <>
struct detail::abs::impl<tools::int128_t> {
constexpr tools::int128_t operator()(const tools::int128_t& x) const noexcept {
return x >= 0 ? x : -x;
}
};
#if defined(__GLIBCXX__) && defined(__STRICT_ANSI__)
template <>
struct detail::bit_ceil::impl<tools::uint128_t> {
constexpr tools::uint128_t operator()(tools::uint128_t x) const noexcept {
if (x <= 1) return 1;
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
x |= x >> 64;
return ++x;
}
};
template <>
struct detail::bit_floor::impl<tools::uint128_t> {
constexpr tools::uint128_t operator()(tools::uint128_t x) const noexcept {
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
x |= x >> 64;
return x & ~(x >> 1);
}
};
template <>
struct detail::bit_width::impl<tools::uint128_t> {
constexpr int operator()(tools::uint128_t x) const noexcept {
int w = 0;
if (x & UINT128_C(0xffffffffffffffff0000000000000000)) {
x >>= 64;
w += 64;
}
if (x & UINT128_C(0xffffffff00000000)) {
x >>= 32;
w += 32;
}
if (x & UINT128_C(0xffff0000)) {
x >>= 16;
w += 16;
}
if (x & UINT128_C(0xff00)) {
x >>= 8;
w += 8;
}
if (x & UINT128_C(0xf0)) {
x >>= 4;
w += 4;
}
if (x & UINT128_C(0xc)) {
x >>= 2;
w += 2;
}
if (x & UINT128_C(0x2)) {
x >>= 1;
w += 1;
}
w += x;
return w;
}
};
template <>
class detail::countr_zero::impl<tools::uint128_t> {
using type = tools::uint128_t;
static constexpr int shift = 120;
static constexpr type magic = UINT128_C(0x01061438916347932a5cd9d3ead7b77f);
static constexpr int ntz_table[255] = {
128, 0, 1, -1, 2, -1, 8, -1, 3, -1, 15, -1, 9, -1, 22, -1,
4, -1, 29, -1, 16, -1, 36, -1, 10, -1, 43, -1, 23, -1, 50, -1,
5, -1, 33, -1, 30, -1, 57, -1, 17, -1, 64, -1, 37, -1, 71, -1,
11, -1, 60, -1, 44, -1, 78, -1, 24, -1, 85, -1, 51, -1, 92, -1,
-1, 6, -1, 20, -1, 34, -1, 48, 31, -1, -1, 69, 58, -1, -1, 90,
18, -1, 67, -1, 65, -1, 99, -1, 38, -1, 101, -1, 72, -1, 106, -1,
-1, 12, -1, 40, -1, 61, -1, 82, 45, -1, -1, 103, 79, -1, 113, -1,
-1, 25, -1, 74, 86, -1, -1, 116, -1, 52, -1, 108, -1, 93, -1, 120,
127, -1, -1, 7, -1, 14, -1, 21, -1, 28, -1, 35, -1, 42, -1, 49,
-1, 32, -1, 56, -1, 63, -1, 70, -1, 59, -1, 77, -1, 84, -1, 91,
-1, 19, -1, 47, -1, 68, -1, 89, -1, 66, -1, 98, -1, 100, -1, 105,
-1, 39, -1, 81, -1, 102, -1, 112, -1, 73, -1, 115, -1, 107, -1, 119,
126, -1, 13, -1, 27, -1, 41, -1, -1, 55, 62, -1, -1, 76, 83, -1,
-1, 46, -1, 88, -1, 97, -1, 104, -1, 80, -1, 111, -1, 114, -1, 118,
125, -1, 26, -1, 54, -1, 75, -1, -1, 87, 96, -1, -1, 110, -1, 117,
124, -1, 53, -1, -1, 95, 109, -1, 123, -1, 94, -1, 122, -1, 121
};
public:
constexpr int operator()(const type& x) const noexcept {
return ntz_table[static_cast<type>(magic * static_cast<type>(x & -x)) >> shift];
}
};
namespace detail::gcd {
template <>
struct impl<tools::uint128_t, tools::uint128_t> {
constexpr tools::uint128_t operator()(tools::uint128_t m, tools::uint128_t n) const noexcept {
while (n != 0) {
m %= n;
std::swap(m, n);
}
return m;
};
};
template <typename T>
concept non_bool_integral_at_most_128bit = tools::non_bool_integral<T> && std::numeric_limits<T>::digits <= 128;
template <typename T>
concept non_bool_integral_at_most_64bit = tools::non_bool_integral<T> && std::numeric_limits<T>::digits <= 64;
template <typename M, typename N> requires (
(non_bool_integral_at_most_128bit<M> && non_bool_integral_at_most_128bit<N>)
&& !(non_bool_integral_at_most_64bit<M> && non_bool_integral_at_most_64bit<N>)
&& !(std::same_as<M, tools::uint128_t> && std::same_as<N, tools::uint128_t>)
)
struct impl<M, N> {
constexpr std::common_type_t<M, N> operator()(const M m, const N n) const noexcept {
return std::common_type_t<M, N>(
tools::gcd(
m >= 0 ? tools::uint128_t(m) : tools::uint128_t(-(m + 1)) + 1,
n >= 0 ? tools::uint128_t(n) : tools::uint128_t(-(n + 1)) + 1
)
);
}
};
}
template <>
struct detail::has_single_bit::impl<tools::uint128_t> {
constexpr bool operator()(tools::uint128_t x) const noexcept {
return x != 0 && (x & (x - 1)) == 0;
}
};
template <>
struct detail::popcount::impl<tools::uint128_t> {
constexpr int operator()(tools::uint128_t x) const noexcept {
x = (x & UINT128_C(0x55555555555555555555555555555555)) + (x >> 1 & UINT128_C(0x55555555555555555555555555555555));
x = (x & UINT128_C(0x33333333333333333333333333333333)) + (x >> 2 & UINT128_C(0x33333333333333333333333333333333));
x = (x & UINT128_C(0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f)) + (x >> 4 & UINT128_C(0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f));
x = (x & UINT128_C(0x00ff00ff00ff00ff00ff00ff00ff00ff)) + (x >> 8 & UINT128_C(0x00ff00ff00ff00ff00ff00ff00ff00ff));
x = (x & UINT128_C(0x0000ffff0000ffff0000ffff0000ffff)) + (x >> 16 & UINT128_C(0x0000ffff0000ffff0000ffff0000ffff));
x = (x & UINT128_C(0x00000000ffffffff00000000ffffffff)) + (x >> 32 & UINT128_C(0x00000000ffffffff00000000ffffffff));
x = (x & UINT128_C(0x0000000000000000ffffffffffffffff)) + (x >> 64 & UINT128_C(0x0000000000000000ffffffffffffffff));
return x;
}
};
#endif
}
#line 5 "tools/uint128_t.hpp"
#line 5 "tools/prod_mod.hpp"
namespace tools {
template <typename T1, typename T2, typename T3>
constexpr T3 prod_mod(const T1 x, const T2 y, const T3 m) {
using u128 = tools::uint128_t;
u128 prod_mod = u128(x >= 0 ? x : -x) * u128(y >= 0 ? y : -y) % u128(m);
if ((x >= 0) ^ (y >= 0)) prod_mod = u128(m) - prod_mod;
return prod_mod;
}
}
#line 1 "tools/pow_mod.hpp"
#line 1 "tools/mod.hpp"
#line 7 "tools/mod.hpp"
namespace tools {
template <tools::non_bool_integral M, tools::non_bool_integral N>
constexpr std::common_type_t<M, N> mod(const M a, const N b) noexcept {
assert(b != 0);
using UM = tools::make_unsigned_t<M>;
using UN = tools::make_unsigned_t<N>;
const UM ua = a >= 0 ? a : static_cast<UM>(-(a + 1)) + 1;
const UN ub = b >= 0 ? b : static_cast<UN>(-(b + 1)) + 1;
auto r = ua % ub;
if (a < 0 && r > 0) {
r = ub - r;
}
return r;
}
}
#line 6 "tools/pow_mod.hpp"
namespace tools {
template <typename T1, typename T2, typename T3>
constexpr T3 pow_mod(const T1 x, T2 n, const T3 m) {
if (m == 1) return 0;
T3 r = 1;
T3 y = tools::mod(x, m);
while (n > 0) {
if ((n & 1) > 0) {
r = tools::prod_mod(r, y, m);
}
y = tools::prod_mod(y, y, m);
n /= 2;
}
return r;
}
}
#line 7 "tools/is_prime.hpp"
namespace tools {
constexpr bool is_prime(const unsigned long long n) {
constexpr std::array<unsigned long long, 7> bases = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
if (n <= 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
auto d = n - 1;
for (; d % 2 == 0; d /= 2);
for (const auto a : bases) {
if (a % n == 0) return true;
auto power = d;
auto target = tools::pow_mod(a, power, n);
bool is_composite = true;
if (target == 1) is_composite = false;
for (; is_composite && power != n - 1; power *= 2, target = tools::prod_mod(target, target, n)) {
if (target == n - 1) is_composite = false;
}
if (is_composite) {
return false;
}
}
return true;
}
}
#line 1 "tools/modint_compatible.hpp"
#line 6 "tools/modint_compatible.hpp"
namespace tools {
template <typename T>
concept modint_compatible = std::regular<std::remove_cv_t<T>>
&& std::equality_comparable<std::remove_cv_t<T>>
&& std::constructible_from<std::remove_cv_t<T>, bool>
&& std::constructible_from<std::remove_cv_t<T>, char>
&& std::constructible_from<std::remove_cv_t<T>, int>
&& std::constructible_from<std::remove_cv_t<T>, long long>
&& std::constructible_from<std::remove_cv_t<T>, unsigned int>
&& std::constructible_from<std::remove_cv_t<T>, unsigned long long>
&& requires(std::remove_cv_t<T> a, std::remove_cv_t<T> b, int v_int, long long v_ll) {
{ std::remove_cv_t<T>::mod() } -> std::convertible_to<int>;
{ std::remove_cv_t<T>::raw(v_int) } -> std::same_as<std::remove_cv_t<T>>;
{ a.val() } -> std::convertible_to<int>;
{ ++a } -> std::same_as<std::remove_cv_t<T>&>;
{ --a } -> std::same_as<std::remove_cv_t<T>&>;
{ a++ } -> std::same_as<std::remove_cv_t<T>>;
{ a-- } -> std::same_as<std::remove_cv_t<T>>;
{ a += b } -> std::same_as<std::remove_cv_t<T>&>;
{ a -= b } -> std::same_as<std::remove_cv_t<T>&>;
{ a *= b } -> std::same_as<std::remove_cv_t<T>&>;
{ a /= b } -> std::same_as<std::remove_cv_t<T>&>;
{ +a } -> std::same_as<std::remove_cv_t<T>>;
{ -a } -> std::same_as<std::remove_cv_t<T>>;
{ a.pow(v_ll) } -> std::same_as<std::remove_cv_t<T>>;
{ a.inv() } -> std::same_as<std::remove_cv_t<T>>;
{ a + b } -> std::same_as<std::remove_cv_t<T>>;
{ a - b } -> std::same_as<std::remove_cv_t<T>>;
{ a * b } -> std::same_as<std::remove_cv_t<T>>;
{ a / b } -> std::same_as<std::remove_cv_t<T>>;
};
}
#line 11 "tools/fact_mod_cache.hpp"
namespace tools {
template <tools::modint_compatible M>
class fact_mod_cache {
std::vector<M> m_inv;
std::vector<M> m_fact;
std::vector<M> m_fact_inv;
public:
fact_mod_cache() : m_inv({M::raw(0), M::raw(1)}), m_fact({M::raw(1), M::raw(1)}), m_fact_inv({M::raw(1), M::raw(1)}) {
assert(tools::is_prime(M::mod()));
}
explicit fact_mod_cache(const long long max) : fact_mod_cache() {
this->fact(std::min<long long>(max, M::mod() - 1));
this->fact_inv(std::min<long long>(max, M::mod() - 1));
}
M inv(const long long n) {
assert(n % M::mod() != 0);
const long long size = std::ssize(this->m_inv);
this->m_inv.resize(std::clamp<long long>(std::abs(n) + 1, size, M::mod()));
for (long long i = size; i < std::ssize(this->m_inv); ++i) {
this->m_inv[i] = -this->m_inv[M::mod() % i] * M::raw(M::mod() / i);
}
M result = this->m_inv[std::abs(n) % M::mod()];
if (n < 0) result = -result;
return result;
}
M fact(const long long n) {
assert(n >= 0);
const long long size = std::ssize(this->m_fact);
this->m_fact.resize(std::clamp<long long>(n + 1, size, M::mod()));
for (long long i = size; i < std::ssize(this->m_fact); ++i) {
this->m_fact[i] = this->m_fact[i - 1] * M::raw(i);
}
return n < M::mod() ? this->m_fact[n] : M::raw(0);
}
M fact_inv(const long long n) {
assert(0 <= n && n < M::mod());
const long long size = std::ssize(this->m_fact_inv);
this->m_fact_inv.resize(std::max<long long>(size, n + 1));
this->inv(this->m_fact_inv.size() - 1);
for (long long i = size; i < std::ssize(this->m_fact_inv); ++i) {
this->m_fact_inv[i] = this->m_fact_inv[i - 1] * this->m_inv[i];
}
return this->m_fact_inv[n];
}
M binomial(long long n, long long r) {
if (r < 0) return M::raw(0);
if (0 <= n && n < r) return M::raw(0);
if (n < 0) return M(1 - ((r & 1) << 1)) * this->binomial(-n + r - 1, r);
this->fact(std::min<long long>(n, M::mod() - 1));
this->fact_inv(std::min<long long>(n, M::mod() - 1));
const auto c = [&](const long long nn, const long long rr) {
return 0 <= rr && rr <= nn ? this->m_fact[nn] * this->m_fact_inv[nn - rr] * this->m_fact_inv[rr] : M::raw(0);
};
M result(1);
while (n > 0 || r > 0) {
result *= c(n % M::mod(), r % M::mod());
n /= M::mod();
r /= M::mod();
}
return result;
}
M combination(const long long n, const long long r) {
if (!(0 <= r && r <= n)) return M::raw(0);
return this->binomial(n, r);
}
M permutation(const long long n, const long long r) {
if (!(0 <= r && r <= n)) return M::raw(0);
return this->binomial(n, r) * this->fact(r);
}
M combination_with_repetition(const long long n, const long long r) {
if (n < 0 || r < 0) return M::raw(0);
return this->binomial(n + r - 1, r);
}
};
}
#line 1 "tools/fps.hpp"
#line 7 "tools/fps.hpp"
#include <initializer_list>
#line 1 "lib/ac-library/atcoder/modint.hpp"
#line 7 "lib/ac-library/atcoder/modint.hpp"
#ifdef _MSC_VER
#include <intrin.h>
#endif
#line 1 "lib/ac-library/atcoder/internal_math.hpp"
#line 5 "lib/ac-library/atcoder/internal_math.hpp"
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
x %= m;
if (x < 0) x += m;
return x;
}
// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
unsigned int _m;
unsigned long long im;
// @param m `1 <= m`
explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}
// @return m
unsigned int umod() const { return _m; }
// @param a `0 <= a < m`
// @param b `0 <= b < m`
// @return `a * b % m`
unsigned int mul(unsigned int a, unsigned int b) const {
// [1] m = 1
// a = b = im = 0, so okay
// [2] m >= 2
// im = ceil(2^64 / m)
// -> im * m = 2^64 + r (0 <= r < m)
// let z = a*b = c*m + d (0 <= c, d < m)
// a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
// c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
// ((ab * im) >> 64) == c or c + 1
unsigned long long z = a;
z *= b;
#ifdef _MSC_VER
unsigned long long x;
_umul128(z, im, &x);
#else
unsigned long long x =
(unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
unsigned long long y = x * _m;
return (unsigned int)(z - y + (z < y ? _m : 0));
}
};
// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
if (m == 1) return 0;
unsigned int _m = (unsigned int)(m);
unsigned long long r = 1;
unsigned long long y = safe_mod(x, m);
while (n) {
if (n & 1) r = (r * y) % _m;
y = (y * y) % _m;
n >>= 1;
}
return r;
}
// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
if (n <= 1) return false;
if (n == 2 || n == 7 || n == 61) return true;
if (n % 2 == 0) return false;
long long d = n - 1;
while (d % 2 == 0) d /= 2;
constexpr long long bases[3] = {2, 7, 61};
for (long long a : bases) {
long long t = d;
long long y = pow_mod_constexpr(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1) {
y = y * y % n;
t <<= 1;
}
if (y != n - 1 && t % 2 == 0) {
return false;
}
}
return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);
// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};
// Contracts:
// [1] s - m0 * a = 0 (mod b)
// [2] t - m1 * a = 0 (mod b)
// [3] s * |m1| + t * |m0| <= b
long long s = b, t = a;
long long m0 = 0, m1 = 1;
while (t) {
long long u = s / t;
s -= t * u;
m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b
// [3]:
// (s - t * u) * |m1| + t * |m0 - m1 * u|
// <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
// = s * |m1| + t * |m0| <= b
auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
// by [3]: |m0| <= b/g
// by g != b: |m0| < b/g
if (m0 < 0) m0 += b / s;
return {s, m0};
}
// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
if (m == 2) return 1;
if (m == 167772161) return 3;
if (m == 469762049) return 3;
if (m == 754974721) return 11;
if (m == 998244353) return 3;
int divs[20] = {};
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while (x % 2 == 0) x /= 2;
for (int i = 3; (long long)(i)*i <= x; i += 2) {
if (x % i == 0) {
divs[cnt++] = i;
while (x % i == 0) {
x /= i;
}
}
}
if (x > 1) {
divs[cnt++] = x;
}
for (int g = 2;; g++) {
bool ok = true;
for (int i = 0; i < cnt; i++) {
if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if (ok) return g;
}
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);
// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
unsigned long long m,
unsigned long long a,
unsigned long long b) {
unsigned long long ans = 0;
while (true) {
if (a >= m) {
ans += n * (n - 1) / 2 * (a / m);
a %= m;
}
if (b >= m) {
ans += n * (b / m);
b %= m;
}
unsigned long long y_max = a * n + b;
if (y_max < m) break;
// y_max < m * (n + 1)
// floor(y_max / m) <= n
n = (unsigned long long)(y_max / m);
b = (unsigned long long)(y_max % m);
std::swap(m, a);
}
return ans;
}
} // namespace internal
} // namespace atcoder
#line 1 "lib/ac-library/atcoder/internal_type_traits.hpp"
#line 7 "lib/ac-library/atcoder/internal_type_traits.hpp"
namespace atcoder {
namespace internal {
#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;
template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;
#else
template <class T> using is_integral = typename std::is_integral<T>;
template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;
#endif
template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
template <class T> using to_unsigned_t = typename to_unsigned<T>::type;
} // namespace internal
} // namespace atcoder
#line 14 "lib/ac-library/atcoder/modint.hpp"
namespace atcoder {
namespace internal {
struct modint_base {};
struct static_modint_base : modint_base {};
template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;
} // namespace internal
template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
using mint = static_modint;
public:
static constexpr int mod() { return m; }
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
static_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
static_modint(T v) {
long long x = (long long)(v % (long long)(umod()));
if (x < 0) x += umod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
static_modint(T v) {
_v = (unsigned int)(v % umod());
}
int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v -= rhs._v;
if (_v >= umod()) _v += umod();
return *this;
}
mint& operator*=(const mint& rhs) {
unsigned long long z = _v;
z *= rhs._v;
_v = (unsigned int)(z % umod());
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
if (prime) {
assert(_v);
return pow(umod() - 2);
} else {
auto eg = internal::inv_gcd(_v, m);
assert(eg.first == 1);
return eg.second;
}
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}
private:
unsigned int _v;
static constexpr unsigned int umod() { return m; }
static constexpr bool prime = internal::is_prime<m>;
};
template <int id> struct dynamic_modint : internal::modint_base {
using mint = dynamic_modint;
public:
static int mod() { return (int)(bt.umod()); }
static void set_mod(int m) {
assert(1 <= m);
bt = internal::barrett(m);
}
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
dynamic_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
dynamic_modint(T v) {
long long x = (long long)(v % (long long)(mod()));
if (x < 0) x += mod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
dynamic_modint(T v) {
_v = (unsigned int)(v % mod());
}
int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v += mod() - rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator*=(const mint& rhs) {
_v = bt.mul(_v, rhs._v);
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
auto eg = internal::inv_gcd(_v, mod());
assert(eg.first == 1);
return eg.second;
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}
private:
unsigned int _v;
static internal::barrett bt;
static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);
using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;
namespace internal {
template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;
template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;
template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};
template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;
} // namespace internal
} // namespace atcoder
#line 1 "lib/ac-library/atcoder/convolution.hpp"
#line 9 "lib/ac-library/atcoder/convolution.hpp"
#line 1 "lib/ac-library/atcoder/internal_bit.hpp"
#ifdef _MSC_VER
#include <intrin.h>
#endif
#if __cplusplus >= 202002L
#line 10 "lib/ac-library/atcoder/internal_bit.hpp"
#endif
namespace atcoder {
namespace internal {
#if __cplusplus >= 202002L
using std::bit_ceil;
#else
// @return same with std::bit::bit_ceil
unsigned int bit_ceil(unsigned int n) {
unsigned int x = 1;
while (x < (unsigned int)(n)) x *= 2;
return x;
}
#endif
// @param n `1 <= n`
// @return same with std::bit::countr_zero
int countr_zero(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
// @param n `1 <= n`
// @return same with std::bit::countr_zero
constexpr int countr_zero_constexpr(unsigned int n) {
int x = 0;
while (!(n & (1 << x))) x++;
return x;
}
} // namespace internal
} // namespace atcoder
#line 12 "lib/ac-library/atcoder/convolution.hpp"
namespace atcoder {
namespace internal {
template <class mint,
int g = internal::primitive_root<mint::mod()>,
internal::is_static_modint_t<mint>* = nullptr>
struct fft_info {
static constexpr int rank2 = countr_zero_constexpr(mint::mod() - 1);
std::array<mint, rank2 + 1> root; // root[i]^(2^i) == 1
std::array<mint, rank2 + 1> iroot; // root[i] * iroot[i] == 1
std::array<mint, std::max(0, rank2 - 2 + 1)> rate2;
std::array<mint, std::max(0, rank2 - 2 + 1)> irate2;
std::array<mint, std::max(0, rank2 - 3 + 1)> rate3;
std::array<mint, std::max(0, rank2 - 3 + 1)> irate3;
fft_info() {
root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2);
iroot[rank2] = root[rank2].inv();
for (int i = rank2 - 1; i >= 0; i--) {
root[i] = root[i + 1] * root[i + 1];
iroot[i] = iroot[i + 1] * iroot[i + 1];
}
{
mint prod = 1, iprod = 1;
for (int i = 0; i <= rank2 - 2; i++) {
rate2[i] = root[i + 2] * prod;
irate2[i] = iroot[i + 2] * iprod;
prod *= iroot[i + 2];
iprod *= root[i + 2];
}
}
{
mint prod = 1, iprod = 1;
for (int i = 0; i <= rank2 - 3; i++) {
rate3[i] = root[i + 3] * prod;
irate3[i] = iroot[i + 3] * iprod;
prod *= iroot[i + 3];
iprod *= root[i + 3];
}
}
}
};
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly(std::vector<mint>& a) {
int n = int(a.size());
int h = internal::countr_zero((unsigned int)n);
static const fft_info<mint> info;
int len = 0; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed
while (len < h) {
if (h - len == 1) {
int p = 1 << (h - len - 1);
mint rot = 1;
for (int s = 0; s < (1 << len); s++) {
int offset = s << (h - len);
for (int i = 0; i < p; i++) {
auto l = a[i + offset];
auto r = a[i + offset + p] * rot;
a[i + offset] = l + r;
a[i + offset + p] = l - r;
}
if (s + 1 != (1 << len))
rot *= info.rate2[countr_zero(~(unsigned int)(s))];
}
len++;
} else {
// 4-base
int p = 1 << (h - len - 2);
mint rot = 1, imag = info.root[2];
for (int s = 0; s < (1 << len); s++) {
mint rot2 = rot * rot;
mint rot3 = rot2 * rot;
int offset = s << (h - len);
for (int i = 0; i < p; i++) {
auto mod2 = 1ULL * mint::mod() * mint::mod();
auto a0 = 1ULL * a[i + offset].val();
auto a1 = 1ULL * a[i + offset + p].val() * rot.val();
auto a2 = 1ULL * a[i + offset + 2 * p].val() * rot2.val();
auto a3 = 1ULL * a[i + offset + 3 * p].val() * rot3.val();
auto a1na3imag =
1ULL * mint(a1 + mod2 - a3).val() * imag.val();
auto na2 = mod2 - a2;
a[i + offset] = a0 + a2 + a1 + a3;
a[i + offset + 1 * p] = a0 + a2 + (2 * mod2 - (a1 + a3));
a[i + offset + 2 * p] = a0 + na2 + a1na3imag;
a[i + offset + 3 * p] = a0 + na2 + (mod2 - a1na3imag);
}
if (s + 1 != (1 << len))
rot *= info.rate3[countr_zero(~(unsigned int)(s))];
}
len += 2;
}
}
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly_inv(std::vector<mint>& a) {
int n = int(a.size());
int h = internal::countr_zero((unsigned int)n);
static const fft_info<mint> info;
int len = h; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed
while (len) {
if (len == 1) {
int p = 1 << (h - len);
mint irot = 1;
for (int s = 0; s < (1 << (len - 1)); s++) {
int offset = s << (h - len + 1);
for (int i = 0; i < p; i++) {
auto l = a[i + offset];
auto r = a[i + offset + p];
a[i + offset] = l + r;
a[i + offset + p] =
(unsigned long long)((unsigned int)(l.val() - r.val()) + mint::mod()) *
irot.val();
;
}
if (s + 1 != (1 << (len - 1)))
irot *= info.irate2[countr_zero(~(unsigned int)(s))];
}
len--;
} else {
// 4-base
int p = 1 << (h - len);
mint irot = 1, iimag = info.iroot[2];
for (int s = 0; s < (1 << (len - 2)); s++) {
mint irot2 = irot * irot;
mint irot3 = irot2 * irot;
int offset = s << (h - len + 2);
for (int i = 0; i < p; i++) {
auto a0 = 1ULL * a[i + offset + 0 * p].val();
auto a1 = 1ULL * a[i + offset + 1 * p].val();
auto a2 = 1ULL * a[i + offset + 2 * p].val();
auto a3 = 1ULL * a[i + offset + 3 * p].val();
auto a2na3iimag =
1ULL *
mint((mint::mod() + a2 - a3) * iimag.val()).val();
a[i + offset] = a0 + a1 + a2 + a3;
a[i + offset + 1 * p] =
(a0 + (mint::mod() - a1) + a2na3iimag) * irot.val();
a[i + offset + 2 * p] =
(a0 + a1 + (mint::mod() - a2) + (mint::mod() - a3)) *
irot2.val();
a[i + offset + 3 * p] =
(a0 + (mint::mod() - a1) + (mint::mod() - a2na3iimag)) *
irot3.val();
}
if (s + 1 != (1 << (len - 2)))
irot *= info.irate3[countr_zero(~(unsigned int)(s))];
}
len -= 2;
}
}
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution_naive(const std::vector<mint>& a,
const std::vector<mint>& b) {
int n = int(a.size()), m = int(b.size());
std::vector<mint> ans(n + m - 1);
if (n < m) {
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
ans[i + j] += a[i] * b[j];
}
}
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans[i + j] += a[i] * b[j];
}
}
}
return ans;
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution_fft(std::vector<mint> a, std::vector<mint> b) {
int n = int(a.size()), m = int(b.size());
int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
a.resize(z);
internal::butterfly(a);
b.resize(z);
internal::butterfly(b);
for (int i = 0; i < z; i++) {
a[i] *= b[i];
}
internal::butterfly_inv(a);
a.resize(n + m - 1);
mint iz = mint(z).inv();
for (int i = 0; i < n + m - 1; i++) a[i] *= iz;
return a;
}
} // namespace internal
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution(std::vector<mint>&& a, std::vector<mint>&& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
[[maybe_unused]] int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
assert((mint::mod() - 1) % z == 0);
if (std::min(n, m) <= 60) return convolution_naive(std::move(a), std::move(b));
return internal::convolution_fft(std::move(a), std::move(b));
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution(const std::vector<mint>& a,
const std::vector<mint>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
[[maybe_unused]] int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
assert((mint::mod() - 1) % z == 0);
if (std::min(n, m) <= 60) return convolution_naive(a, b);
return internal::convolution_fft(a, b);
}
template <unsigned int mod = 998244353,
class T,
std::enable_if_t<internal::is_integral<T>::value>* = nullptr>
std::vector<T> convolution(const std::vector<T>& a, const std::vector<T>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
using mint = static_modint<mod>;
[[maybe_unused]] int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
assert((mint::mod() - 1) % z == 0);
std::vector<mint> a2(n), b2(m);
for (int i = 0; i < n; i++) {
a2[i] = mint(a[i]);
}
for (int i = 0; i < m; i++) {
b2[i] = mint(b[i]);
}
auto c2 = convolution(std::move(a2), std::move(b2));
std::vector<T> c(n + m - 1);
for (int i = 0; i < n + m - 1; i++) {
c[i] = c2[i].val();
}
return c;
}
std::vector<long long> convolution_ll(const std::vector<long long>& a,
const std::vector<long long>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
static constexpr unsigned long long MOD1 = 754974721; // 2^24
static constexpr unsigned long long MOD2 = 167772161; // 2^25
static constexpr unsigned long long MOD3 = 469762049; // 2^26
static constexpr unsigned long long M2M3 = MOD2 * MOD3;
static constexpr unsigned long long M1M3 = MOD1 * MOD3;
static constexpr unsigned long long M1M2 = MOD1 * MOD2;
static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3;
static constexpr unsigned long long i1 =
internal::inv_gcd(MOD2 * MOD3, MOD1).second;
static constexpr unsigned long long i2 =
internal::inv_gcd(MOD1 * MOD3, MOD2).second;
static constexpr unsigned long long i3 =
internal::inv_gcd(MOD1 * MOD2, MOD3).second;
static constexpr int MAX_AB_BIT = 24;
static_assert(MOD1 % (1ull << MAX_AB_BIT) == 1, "MOD1 isn't enough to support an array length of 2^24.");
static_assert(MOD2 % (1ull << MAX_AB_BIT) == 1, "MOD2 isn't enough to support an array length of 2^24.");
static_assert(MOD3 % (1ull << MAX_AB_BIT) == 1, "MOD3 isn't enough to support an array length of 2^24.");
assert(n + m - 1 <= (1 << MAX_AB_BIT));
auto c1 = convolution<MOD1>(a, b);
auto c2 = convolution<MOD2>(a, b);
auto c3 = convolution<MOD3>(a, b);
std::vector<long long> c(n + m - 1);
for (int i = 0; i < n + m - 1; i++) {
unsigned long long x = 0;
x += (c1[i] * i1) % MOD1 * M2M3;
x += (c2[i] * i2) % MOD2 * M1M3;
x += (c3[i] * i3) % MOD3 * M1M2;
// B = 2^63, -B <= x, r(real value) < B
// (x, x - M, x - 2M, or x - 3M) = r (mod 2B)
// r = c1[i] (mod MOD1)
// focus on MOD1
// r = x, x - M', x - 2M', x - 3M' (M' = M % 2^64) (mod 2B)
// r = x,
// x - M' + (0 or 2B),
// x - 2M' + (0, 2B or 4B),
// x - 3M' + (0, 2B, 4B or 6B) (without mod!)
// (r - x) = 0, (0)
// - M' + (0 or 2B), (1)
// -2M' + (0 or 2B or 4B), (2)
// -3M' + (0 or 2B or 4B or 6B) (3) (mod MOD1)
// we checked that
// ((1) mod MOD1) mod 5 = 2
// ((2) mod MOD1) mod 5 = 3
// ((3) mod MOD1) mod 5 = 4
long long diff =
c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1));
if (diff < 0) diff += MOD1;
static constexpr unsigned long long offset[5] = {
0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3};
x -= offset[diff % 5];
c[i] = x;
}
return c;
}
} // namespace atcoder
#line 1 "tools/ceil_log2.hpp"
#line 6 "tools/ceil_log2.hpp"
namespace tools {
template <typename T>
constexpr T ceil_log2(T x) noexcept {
assert(x > 0);
return tools::bit_width(x - 1);
}
}
#line 1 "tools/convolution.hpp"
#line 7 "tools/convolution.hpp"
#include <complex>
#line 1 "tools/available_for_multiple_range_adaptors.hpp"
#line 1 "tools/specialization_of.hpp"
#line 5 "tools/specialization_of.hpp"
namespace tools {
namespace detail {
namespace specialization_of {
template <typename, template <typename...> typename>
struct trait : std::false_type {};
template <template <typename...> typename U, typename... Args>
struct trait<U<Args...>, U> : std::true_type {};
}
}
template <typename T, template <typename...> typename U>
concept specialization_of = tools::detail::specialization_of::trait<T, U>::value;
}
#line 8 "tools/available_for_multiple_range_adaptors.hpp"
namespace tools {
template <typename T>
concept available_for_multiple_range_adaptors = std::ranges::forward_range<T>
&& std::ranges::viewable_range<T>
&& std::copyable<std::views::all_t<T>>;
}
#line 1 "tools/complex.hpp"
#line 6 "tools/complex.hpp"
namespace tools {
template <typename T>
concept complex = tools::specialization_of<T, std::complex>;
}
#line 1 "tools/garner3.hpp"
#line 7 "tools/garner3.hpp"
namespace tools {
template <typename M, typename M1, typename M2, typename M3>
M garner3(const M1& a, const M2& b, const M3& c, const M m) {
using ull = unsigned long long;
static const M2 m1_inv_mod_m2 = M2::raw(M1::mod()).inv();
static const M3 m1_m2_inv_mod_m3 = (M3::raw(M1::mod()) * M3::raw(M2::mod())).inv();
static const auto plus_mod = [](ull x, const ull y, const ull mod) {
assert(x < mod);
assert(y < mod);
x += y;
if (x >= mod) x -= mod;
return x;
};
assert(m >= 1);
assert(M1::mod() < M2::mod());
assert(M2::mod() < M3::mod());
assert(tools::is_prime(M1::mod()));
assert(tools::is_prime(M2::mod()));
assert(tools::is_prime(M3::mod()));
// t1 = (b - a) / M1; (mod M2)
// t2 = (c - a - t1 * M1) / M1 / M2; (mod M3)
// return a + t1 * M1 + t2 * M1 * M2; (mod m)
const M2 t1 = (b - M2::raw(a.val())) * m1_inv_mod_m2;
const M3 t2 = (c - M3::raw(a.val()) - M3::raw(t1.val()) * M3::raw(M1::mod())) * m1_m2_inv_mod_m3;
ull r = tools::prod_mod(t2.val(), ull(M1::mod()) * ull(M2::mod()), m);
assert(r < ull(m));
r = plus_mod(r, ull(t1.val()) * ull(M1::mod()) % m, m);
assert(r < ull(m));
r = plus_mod(r, a.val() % m, m);
assert(r < ull(m));
return r;
}
}
#line 1 "tools/groups.hpp"
#line 1 "tools/arithmetic.hpp"
#line 6 "tools/arithmetic.hpp"
namespace tools {
template <typename T>
concept arithmetic = tools::integral<T> || std::floating_point<T>;
}
#line 7 "tools/groups.hpp"
namespace tools {
namespace groups {
template <typename G>
struct bit_xor {
using T = G;
static T op(const T& x, const T& y) {
return x ^ y;
}
static T e() {
return T(0);
}
static T inv(const T& x) {
return x;
}
};
template <typename G>
struct multiplies {
using T = G;
static T op(const T& x, const T& y) {
return x * y;
}
static T e() {
return T(1);
}
static T inv(const T& x) {
return e() / x;
}
};
template <typename G>
struct plus {
using T = G;
static T op(const T& x, const T& y) {
return x + y;
}
static T e() {
return T(0);
}
static T inv(const T& x) {
return -x;
}
};
}
}
#line 1 "tools/modint.hpp"
#line 7 "tools/modint.hpp"
namespace tools {
template <typename T>
concept modint = tools::modint_compatible<T>
&& requires(std::remove_cv_t<T> a) {
{ std::remove_cv_t<T>::mod() } -> std::same_as<int>;
{ a.val() } -> std::same_as<int>;
};
}
#line 1 "tools/monoids.hpp"
#line 14 "tools/monoids.hpp"
namespace tools {
namespace monoids {
template <typename M>
struct bit_and {
using T = M;
static T op(const T& x, const T& y) {
return x & y;
}
static T e() {
return std::numeric_limits<T>::max();
}
};
template <typename M>
struct bit_or {
using T = M;
static T op(const T& x, const T& y) {
return x | y;
}
static T e() {
return T(0);
}
};
template <typename M>
requires requires (M x, M y) {
{tools::gcd(x, y)} -> std::convertible_to<M>;
}
struct gcd {
using T = M;
static T op(const T& x, const T& y) {
return tools::gcd(x, y);
}
static T e() {
return T(0);
}
};
template <typename M, M ...dummy>
struct max;
template <tools::arithmetic M>
struct max<M> {
using T = M;
static T op(const T& x, const T& y) {
return std::max(x, y);
}
static T e() {
if constexpr (tools::integral<M>) {
return std::numeric_limits<M>::min();
} else {
return -std::numeric_limits<M>::infinity();
}
}
};
template <std::totally_ordered M, M E>
struct max<M, E> {
using T = M;
static T op(const T& x, const T& y) {
assert(E <= x);
assert(E <= y);
return std::max(x, y);
}
static T e() {
return E;
}
};
template <typename M, M ...dummy>
struct min;
template <tools::arithmetic M>
struct min<M> {
using T = M;
static T op(const T& x, const T& y) {
return std::min(x, y);
}
static T e() {
if constexpr (tools::integral<M>) {
return std::numeric_limits<M>::max();
} else {
return std::numeric_limits<M>::infinity();
}
}
};
template <std::totally_ordered M, M E>
struct min<M, E> {
using T = M;
static T op(const T& x, const T& y) {
assert(x <= E);
assert(y <= E);
return std::min(x, y);
}
static T e() {
return E;
}
};
template <typename M>
struct multiplies {
using T = M;
static T op(const T& x, const T& y) {
return x * y;
}
static T e() {
return T(1);
}
};
template <>
struct multiplies<bool> {
using T = bool;
static T op(const bool x, const bool y) {
return x && y;
}
static T e() {
return true;
}
};
template <typename M, M E>
struct update {
using T = M;
static T op(const T& x, const T& y) {
return x == E ? y : x;
}
static T e() {
return E;
}
};
}
}
#line 1 "tools/pow2.hpp"
#line 7 "tools/pow2.hpp"
namespace tools {
template <tools::integral T>
constexpr T pow2(const T x) noexcept {
assert(0 <= x && x < std::numeric_limits<T>::digits);
return T(1) << x;
}
}
#line 1 "tools/ring.hpp"
#line 1 "tools/commutative_group.hpp"
#line 1 "tools/commutative_monoid.hpp"
#line 1 "tools/monoid.hpp"
#line 5 "tools/monoid.hpp"
namespace tools {
template <typename M>
concept monoid = requires(typename M::T x, typename M::T y) {
{ M::op(x, y) } -> std::same_as<typename M::T>;
{ M::e() } -> std::same_as<typename M::T>;
};
}
#line 5 "tools/commutative_monoid.hpp"
namespace tools {
template <typename M>
concept commutative_monoid = tools::monoid<M>;
}
#line 1 "tools/group.hpp"
#line 6 "tools/group.hpp"
namespace tools {
template <typename G>
concept group = tools::monoid<G> && requires(typename G::T x) {
{ G::inv(x) } -> std::same_as<typename G::T>;
};
}
#line 6 "tools/commutative_group.hpp"
namespace tools {
template <typename G>
concept commutative_group = tools::group<G> && tools::commutative_monoid<G>;
}
#line 1 "tools/semiring.hpp"
#line 6 "tools/semiring.hpp"
namespace tools {
template <typename R>
concept semiring = tools::commutative_monoid<typename R::add> && tools::monoid<typename R::mul> && std::same_as<typename R::add::T, typename R::mul::T>;
}
#line 6 "tools/ring.hpp"
namespace tools {
template <typename R>
concept ring = tools::semiring<R> && tools::commutative_group<typename R::add>;
}
#line 1 "tools/rings.hpp"
#line 1 "tools/semirings.hpp"
#line 8 "tools/semirings.hpp"
namespace tools {
namespace semirings {
template <tools::commutative_monoid A, tools::monoid M>
struct of {
using add = A;
using mul = M;
};
template <typename R>
using min_plus = tools::semirings::of<tools::monoids::min<R>, tools::groups::plus<R>>;
template <typename R>
using max_plus = tools::semirings::of<tools::monoids::max<R>, tools::groups::plus<R>>;
template <typename R>
using min_max = tools::semirings::of<tools::monoids::min<R>, tools::monoids::max<R>>;
template <typename R>
using max_min = tools::semirings::of<tools::monoids::max<R>, tools::monoids::min<R>>;
}
}
#line 9 "tools/rings.hpp"
namespace tools {
namespace rings {
template <tools::commutative_group A, tools::monoid M>
using of = tools::semirings::of<A, M>;
template <typename R>
using plus_multiplies = tools::rings::of<tools::groups::plus<R>, tools::monoids::multiplies<R>>;
template <typename R>
using xor_and = tools::rings::of<tools::groups::bit_xor<R>, tools::monoids::bit_and<R>>;
}
}
#line 28 "tools/convolution.hpp"
namespace tools {
namespace detail {
namespace convolution {
template <tools::ring R, std::ranges::forward_range R1, std::ranges::forward_range R2>
requires std::same_as<std::ranges::range_value_t<R1>, std::ranges::range_value_t<R2>>
&& std::assignable_from<typename R::add::T&, std::ranges::range_value_t<R1>>
auto naive(R1&& a, R2&& b) {
assert(!std::ranges::empty(a));
assert(!std::ranges::empty(b));
using Add = typename R::add;
using Mul = typename R::mul;
using T = typename Add::T;
const auto n = std::ranges::distance(a);
const auto m = std::ranges::distance(b);
std::vector<T> c(n + m - 1, Add::e());
if (n < m) {
auto it1 = c.begin();
for (const auto& b_j : b) {
auto it2 = it1;
for (const auto& a_i : a) {
*it2 = Add::op(*it2, Mul::op(a_i, b_j));
++it2;
}
++it1;
}
} else {
auto it1 = c.begin();
for (const auto& a_i : a) {
auto it2 = it1;
for (const auto& b_j : b) {
*it2 = Add::op(*it2, Mul::op(a_i, b_j));
++it2;
}
++it1;
}
}
return c;
}
template <std::ranges::input_range R1, std::ranges::input_range R2>
requires std::same_as<std::ranges::range_value_t<R1>, std::ranges::range_value_t<R2>>
&& (std::floating_point<std::ranges::range_value_t<R1>> || tools::complex<std::ranges::range_value_t<R1>>)
auto fft(R1&& pa, R2&& pb) {
using T = std::ranges::range_value_t<R1>;
using C = std::conditional_t<std::floating_point<T>, std::complex<T>, T>;
using R = typename C::value_type;
assert(!std::ranges::empty(pa));
assert(!std::ranges::empty(pb));
std::vector<C> a, b;
if constexpr (std::same_as<T, R>) {
for (auto&& a_i : pa) {
a.emplace_back(std::forward<decltype(a_i)>(a_i), 0);
}
for (auto&& b_i : pb) {
b.emplace_back(std::forward<decltype(b_i)>(b_i), 0);
}
} else {
a = std::forward<R1>(pa) | std::ranges::to<std::vector<C>>();
b = std::forward<R2>(pb) | std::ranges::to<std::vector<C>>();
}
const auto n = a.size() + b.size() - 1;
const auto z = tools::pow2(tools::ceil_log2(n));
a.resize(z);
b.resize(z);
std::vector<C> pow_root;
pow_root.reserve(z);
pow_root.emplace_back(1, 0);
if (z > 1) pow_root.push_back(std::polar<R>(1, R(2) * std::acos(R(-1)) / z));
for (std::size_t p = 2; p < z; p *= 2) {
pow_root.push_back(pow_root[p / 2] * pow_root[p / 2]);
for (std::size_t i = p + 1; i < p * 2; ++i) {
pow_root.push_back(pow_root[p] * pow_root[i - p]);
}
}
const auto butterfly = [&](std::vector<C>& f) {
std::vector<C> prev(z);
for (std::size_t p = z / 2; p >= 1; p /= 2) {
prev.swap(f);
for (std::size_t qp = 0; qp < z; qp += p) {
for (std::size_t r = 0; r < p; ++r) {
f[qp + r] = prev[qp * 2 % z + r] + pow_root[qp] * prev[qp * 2 % z + p + r];
}
}
}
};
butterfly(a);
butterfly(b);
for (std::size_t i = 0; i < z; ++i) {
a[i] *= b[i];
}
std::reverse(std::next(pow_root.begin()), pow_root.end());
butterfly(a);
if constexpr (std::same_as<T, R>) {
std::vector<T> c;
c.reserve(n);
for (std::size_t i = 0; i < n; ++i) {
c.push_back(a[i].real() / z);
}
return c;
} else {
a.resize(n);
for (auto& a_i : a) {
a_i /= z;
}
return a;
}
}
template <std::ranges::input_range R1, std::ranges::input_range R2>
requires (std::same_as<std::ranges::range_value_t<R1>, std::ranges::range_value_t<R2>>
&& atcoder::internal::is_static_modint<std::ranges::range_value_t<R1>>::value
&& std::ranges::range_value_t<R1>::mod() <= 2000000000
&& tools::is_prime(std::ranges::range_value_t<R1>::mod()))
auto ntt(R1&& pa, R2&& pb) {
using M = std::ranges::range_value_t<R1>;
assert(!std::ranges::empty(pa));
assert(!std::ranges::empty(pb));
auto a = std::forward<R1>(pa) | std::ranges::to<std::vector<M>>();
auto b = std::forward<R2>(pb) | std::ranges::to<std::vector<M>>();
const auto n = a.size();
const auto m = b.size();
const auto z = tools::pow2(tools::ceil_log2(n + m - 1));
assert((M::mod() - 1) % z == 0);
if (n == m && 4 * n == z + 4) {
const auto afbf = a.front() * b.front();
const auto abbb = a.back() * b.back();
a.resize(z / 2);
atcoder::internal::butterfly(a);
b.resize(z / 2);
atcoder::internal::butterfly(b);
for (std::size_t i = 0; i < z / 2; ++i) {
a[i] *= b[i];
}
atcoder::internal::butterfly_inv(a);
const auto iz = M(z / 2).inv();
a.resize(n + m - 1);
a.front() = afbf;
for (auto it = std::next(a.begin()), end = std::prev(a.end()); it != end; ++it) {
*it *= iz;
}
a.back() = abbb;
} else {
a.resize(z);
atcoder::internal::butterfly(a);
b.resize(z);
atcoder::internal::butterfly(b);
for (std::size_t i = 0; i < z; ++i) {
a[i] *= b[i];
}
atcoder::internal::butterfly_inv(a);
const auto iz = M(z).inv();
a.resize(n + m - 1);
for (auto& a_i : a) {
a_i *= iz;
}
}
return a;
}
template <std::ranges::input_range R1, std::ranges::input_range R2>
requires std::same_as<std::ranges::range_value_t<R1>, std::ranges::range_value_t<R2>>
&& tools::modint<std::ranges::range_value_t<R1>>
auto ntt_and_garner(R1&& a, R2&& b) {
using M = std::ranges::range_value_t<R1>;
if constexpr (tools::available_for_multiple_range_adaptors<R1> && tools::available_for_multiple_range_adaptors<R2>) {
using M1 = atcoder::static_modint<1107296257>; // 33 * 2^25 + 1
using M2 = atcoder::static_modint<1711276033>; // 51 * 2^25 + 1
using M3 = atcoder::static_modint<1811939329>; // 27 * 2^26 + 1
assert(!std::ranges::empty(a));
assert(!std::ranges::empty(b));
#ifndef NDEBUG
const auto n = std::ranges::distance(a);
const auto m = std::ranges::distance(b);
const auto z = tools::pow2(tools::ceil_log2(n + m - 1));
#endif
assert((M1::mod() - 1) % z == 0);
assert((M2::mod() - 1) % z == 0);
assert((M3::mod() - 1) % z == 0);
// No need for the following assertion because the condition always holds.
// assert(std::min(n, m) * tools::square(M::mod() - 1) < M1::mod() * M2::mod() * M3::mod());
return std::views::zip_transform(
[](const auto c1_i, const auto c2_i, const auto c3_i) {
return M::raw(tools::garner3(c1_i, c2_i, c3_i, M::mod()));
},
tools::detail::convolution::ntt(
a | std::views::transform([](const auto a_i) { return M1(a_i.val()); }),
b | std::views::transform([](const auto b_i) { return M1(b_i.val()); })
),
tools::detail::convolution::ntt(
a | std::views::transform([](const auto a_i) { return M2(a_i.val()); }),
b | std::views::transform([](const auto b_i) { return M2(b_i.val()); })
),
tools::detail::convolution::ntt(
a | std::views::transform([](const auto a_i) { return M3(a_i.val()); }),
b | std::views::transform([](const auto b_i) { return M3(b_i.val()); })
)
) | std::ranges::to<std::vector<M>>();
} else {
const auto va = std::forward<R1>(a) | std::ranges::to<std::vector<M>>();
const auto vb = std::forward<R2>(b) | std::ranges::to<std::vector<M>>();
return tools::detail::convolution::ntt_and_garner(va, vb);
}
}
template <std::ranges::input_range R1, std::ranges::input_range R2>
requires std::same_as<std::ranges::range_value_t<R1>, std::ranges::range_value_t<R2>>
&& std::integral<std::ranges::range_value_t<R1>>
auto ntt_and_garner_for_ll(R1&& a, R2&& b) {
using Z = std::ranges::range_value_t<R1>;
using ll = long long;
return atcoder::convolution_ll(
std::forward<R1>(a) | std::ranges::to<std::vector<ll>>(),
std::forward<R2>(b) | std::ranges::to<std::vector<ll>>()
) | std::ranges::to<std::vector<Z>>();
}
}
}
template <tools::ring R, std::ranges::input_range R1, std::ranges::input_range R2>
requires std::same_as<std::ranges::range_value_t<R1>, std::ranges::range_value_t<R2>>
&& std::assignable_from<typename R::add::T&, std::ranges::range_value_t<R1>>
auto convolution(R1&& a, R2&& b) {
if constexpr (std::ranges::forward_range<R1> && std::ranges::forward_range<R2>) {
using Add = typename R::add;
using Mul = typename R::mul;
using T = typename Add::T;
if (std::ranges::empty(a) || std::ranges::empty(b)) {
return std::vector<T>{};
}
const auto n = std::ranges::distance(a);
const auto m = std::ranges::distance(b);
if (std::min(n, m) <= 60) {
return tools::detail::convolution::naive<R>(std::forward<R1>(a), std::forward<R2>(b));
}
if constexpr (std::same_as<Add, tools::groups::plus<T>> && (std::same_as<Mul, tools::monoids::multiplies<T>> || std::same_as<Mul, tools::groups::multiplies<T>>)) {
if constexpr (std::floating_point<T> || tools::complex<T>) {
return tools::detail::convolution::fft(std::forward<R1>(a), std::forward<R2>(b));
} else if constexpr (std::integral<T>) {
return tools::detail::convolution::ntt_and_garner_for_ll(std::forward<R1>(a), std::forward<R2>(b));
} else if constexpr (tools::modint<T>) {
if constexpr (atcoder::internal::is_static_modint<T>::value && T::mod() <= 2000000000 && tools::is_prime(T::mod())) {
if ((T::mod() - 1) % tools::pow2(tools::ceil_log2(n + m - 1)) == 0) {
return tools::detail::convolution::ntt(std::forward<R1>(a), std::forward<R2>(b));
} else {
return tools::detail::convolution::ntt_and_garner(std::forward<R1>(a), std::forward<R2>(b));
}
} else {
return tools::detail::convolution::ntt_and_garner(std::forward<R1>(a), std::forward<R2>(b));
}
} else {
return tools::detail::convolution::naive<R>(std::forward<R1>(a), std::forward<R2>(b));
}
} else {
return tools::detail::convolution::naive<R>(std::forward<R1>(a), std::forward<R2>(b));
}
} else {
return tools::convolution(
std::forward<R1>(a) | std::ranges::to<std::vector<std::ranges::range_value_t<R1>>>(),
std::forward<R2>(b) | std::ranges::to<std::vector<std::ranges::range_value_t<R2>>>()
);
}
}
template <std::ranges::input_range R1, std::ranges::input_range R2>
requires std::same_as<std::ranges::range_value_t<R1>, std::ranges::range_value_t<R2>>
auto convolution(R1&& a, R2&& b) {
return tools::convolution<tools::rings::plus_multiplies<std::ranges::range_value_t<R1>>>(std::forward<R1>(a), std::forward<R2>(b));
}
}
#line 1 "tools/exp.hpp"
#line 7 "tools/exp.hpp"
namespace tools {
namespace detail::exp {
template <typename T>
struct impl {
constexpr decltype(auto) operator()(const T x) const noexcept(noexcept(std::exp(x))) {
return std::exp(x);
}
};
}
template <typename T>
constexpr decltype(auto) exp(T&& x) noexcept(noexcept(tools::detail::exp::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x)))) {
return tools::detail::exp::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x));
}
}
#line 1 "tools/less_by_first.hpp"
#line 5 "tools/less_by_first.hpp"
namespace tools {
struct less_by_first {
template <typename T1, typename T2>
bool operator()(const std::pair<T1, T2>& x, const std::pair<T1, T2>& y) const {
return x.first < y.first;
}
};
}
#line 1 "tools/log.hpp"
#line 7 "tools/log.hpp"
namespace tools {
namespace detail::log {
template <typename T>
struct impl {
constexpr decltype(auto) operator()(const T x) const noexcept(noexcept(std::log(x))) {
return std::log(x);
}
};
}
template <typename T>
constexpr decltype(auto) log(T&& x) noexcept(noexcept(tools::detail::log::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x)))) {
return tools::detail::log::impl<std::remove_cvref_t<T>>{}(std::forward<T>(x));
}
}
#line 1 "tools/pow.hpp"
#line 1 "tools/multiplicative_structure.hpp"
#line 1 "tools/prime_static_modint.hpp"
#line 6 "tools/prime_static_modint.hpp"
namespace tools {
template <typename T>
concept prime_static_modint = atcoder::internal::is_static_modint<T>::value && tools::is_prime(T::mod());
}
#line 13 "tools/multiplicative_structure.hpp"
namespace tools {
template <typename T>
using multiplicative_structure = std::conditional_t<
tools::complex<T> || std::floating_point<T> || tools::prime_static_modint<T> || atcoder::internal::is_dynamic_modint<T>::value,
tools::groups::multiplies<T>,
std::conditional_t<
tools::integral<T> || atcoder::internal::is_static_modint<T>::value,
tools::monoids::multiplies<T>,
std::conditional_t<
requires(T a, T b) { { a / b } -> std::same_as<T>; },
tools::groups::multiplies<T>,
tools::monoids::multiplies<T>
>
>
>;
}
#line 1 "tools/square.hpp"
#line 5 "tools/square.hpp"
namespace tools {
template <tools::monoid M>
constexpr typename M::T square(const typename M::T& x) noexcept(noexcept(M::op(x, x))) {
return M::op(x, x);
}
template <typename T>
requires (!tools::monoid<T>)
constexpr T square(const T& x) noexcept(noexcept(x * x)) {
return x * x;
}
}
#line 14 "tools/pow.hpp"
namespace tools {
namespace detail::pow {
template <typename X, typename E>
struct impl {
template <typename G = X>
requires std::same_as<G, X> && tools::group<G>
constexpr typename G::T operator()(const typename G::T& b, const E n) const
noexcept(noexcept(G::e()) && noexcept(G::op(b, b)) && noexcept(G::inv(b)))
requires tools::integral<E> {
if (n < 0) return G::inv((*this)(b, -n));
if (n == 0) return G::e();
if (n % 2 == 0) return tools::square<G>((*this)(b, n / 2));
return G::op((*this)(b, n - 1), b);
}
template <typename M = X>
requires (std::same_as<M, X> && !tools::group<M> && tools::monoid<M>)
constexpr typename M::T operator()(const typename M::T& b, const E n) const
noexcept(noexcept(M::e()) && noexcept(M::op(b, b)))
requires tools::integral<E> {
assert(n >= 0);
if (n == 0) return M::e();
if (n % 2 == 0) return tools::square<M>((*this)(b, n / 2));
return M::op((*this)(b, n - 1), b);
}
constexpr X operator()(const X& b, const E n) const
noexcept(noexcept(impl<tools::multiplicative_structure<X>, E>{}(b, n)))
requires (!tools::monoid<X> && tools::integral<E>) {
return impl<tools::multiplicative_structure<X>, E>{}(b, n);
}
constexpr decltype(auto) operator()(const X& b, const E n) const
noexcept(noexcept(std::pow(b, n)))
requires (!tools::monoid<X> && !tools::integral<E>) {
return std::pow(b, n);
}
};
}
template <typename X = void>
constexpr decltype(auto) pow(auto&& b, auto&& n) noexcept(noexcept(tools::detail::pow::impl<std::conditional_t<std::same_as<X, void>, std::remove_cvref_t<decltype(b)>, X>, std::remove_cvref_t<decltype(n)>>{}(std::forward<decltype(b)>(b), std::forward<decltype(n)>(n)))) {
return tools::detail::pow::impl<std::conditional_t<std::same_as<X, void>, std::remove_cvref_t<decltype(b)>, X>, std::remove_cvref_t<decltype(n)>>{}(std::forward<decltype(b)>(b), std::forward<decltype(n)>(n));
}
}
#line 24 "tools/fps.hpp"
// Source: https://opt-cp.com/fps-implementation/
// License: CC0
// Author: opt
namespace tools {
template <tools::modint M>
class fps;
template <tools::modint M>
struct detail::exp::impl<tools::fps<M>> {
tools::fps<M> operator()(auto&&) const;
};
template <tools::modint M>
struct detail::log::impl<tools::fps<M>> {
tools::fps<M> operator()(auto&&) const;
};
template <tools::modint M, tools::integral E>
struct detail::pow::impl<tools::fps<M>, E> {
tools::fps<M> operator()(auto&&, E) const;
};
template <tools::modint M>
class fps {
using F = tools::fps<M>;
std::vector<M> m_vector;
// maximum 2^k s.t. x = 1 (mod 2^k)
static constexpr int pow2_k(const unsigned int x) {
return (x - 1) & -(x - 1);
}
// d <= lpf(M)
static bool is_leq_lpf_of_M(const int d) {
if (M::mod() == 1) return true;
for (int i = 2; i < d; ++i) {
if (M::mod() % i == 0) return false;
}
return true;
}
public:
using reference = M&;
using const_reference = const M&;
using iterator = typename std::vector<M>::iterator;
using const_iterator = typename std::vector<M>::const_iterator;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using value_type = M;
using allocator_type = typename std::vector<M>::allocator_type;
using pointer = M*;
using const_pointer = const M*;
using reverse_iterator = typename std::vector<M>::reverse_iterator;
using const_reverse_iterator = typename std::vector<M>::const_reverse_iterator;
fps() = default;
explicit fps(const size_type n) : m_vector(n) {}
fps(const size_type n, const_reference value) : m_vector(n, value) {}
template <class InputIter> fps(const InputIter first, const InputIter last) : m_vector(first, last) {}
fps(const std::initializer_list<M> il) : m_vector(il) {}
iterator begin() noexcept { return this->m_vector.begin(); }
const_iterator begin() const noexcept { return this->m_vector.begin(); }
iterator end() noexcept { return this->m_vector.end(); }
const_iterator end() const noexcept { return this->m_vector.end(); }
const_iterator cbegin() const noexcept { return this->m_vector.cbegin(); }
const_iterator cend() const noexcept { return this->m_vector.cend(); }
reverse_iterator rbegin() noexcept { return this->m_vector.rbegin(); }
const_reverse_iterator rbegin() const noexcept { return this->m_vector.rbegin(); }
const_reverse_iterator crbegin() const noexcept { return this->m_vector.crbegin(); }
reverse_iterator rend() noexcept { return this->m_vector.rend(); }
const_reverse_iterator rend() const noexcept { return this->m_vector.rend(); }
const_reverse_iterator crend() const noexcept { return this->m_vector.crend(); }
size_type size() const noexcept { return this->m_vector.size(); }
size_type max_size() const noexcept { return this->m_vector.max_size(); }
void resize(const size_type sz) { this->m_vector.resize(sz); }
void resize(const size_type sz, const M& c) { this->m_vector.resize(sz, c); }
size_type capacity() const noexcept { return this->m_vector.capacity(); }
bool empty() const noexcept { return this->m_vector.empty(); }
void reserve(const size_type n) { this->m_vector.reserve(n); }
void shrink_to_fit() { this->m_vector.shrink_to_fit(); }
reference operator[](const size_type n) { return this->m_vector[n]; }
const_reference operator[](const size_type n) const { return this->m_vector[n]; }
reference at(const size_type n) { return this->m_vector.at(n); }
const_reference at(const size_type n) const { return this->m_vector.at(n); }
pointer data() noexcept { return this->m_vector.data(); }
const_pointer data() const noexcept { return this->m_vector.data(); }
reference front() { return this->m_vector.front(); }
const_reference front() const { return this->m_vector.front(); }
reference back() { return this->m_vector.back(); }
const_reference back() const { return this->m_vector.back(); }
template <class InputIterator> void assign(const InputIterator first, const InputIterator last) { this->m_vector.assign(first, last); }
void assign(const size_type n, const M& u) { this->m_vector.assign(n, u); }
void assign(const std::initializer_list<M> il) { this->m_vector.assign(il); }
void push_back(const M& x) { this->m_vector.push_back(x); }
void push_back(M&& x) { this->m_vector.push_back(std::forward<M>(x)); }
template <class... Args> reference emplace_back(Args&&... args) { return this->m_vector.emplace_back(std::forward<Args>(args)...); }
void pop_back() { this->m_vector.pop_back(); }
iterator insert(const const_iterator position, const M& x) { return this->m_vector.insert(position, x); }
iterator insert(const const_iterator position, M&& x) { return this->m_vector.insert(position, std::forward<M>(x)); }
iterator insert(const const_iterator position, const size_type n, const M& x) { return this->m_vector.insert(position, n, x); }
template <class InputIterator> iterator insert(const const_iterator position, const InputIterator first, const InputIterator last) { return this->m_vector.insert(position, first, last); }
iterator insert(const const_iterator position, const std::initializer_list<M> il) { return this->m_vector.insert(position, il); }
template <class... Args> iterator emplace(const const_iterator position, Args&&... args) { return this->m_vector.emplace(position, std::forward<Args>(args)...); }
iterator erase(const const_iterator position) { return this->m_vector.erase(position); }
iterator erase(const const_iterator first, const const_iterator last) { return this->m_vector.erase(first, last); }
void swap(F& x) noexcept { this->m_vector.swap(x.m_vector); }
void clear() { this->m_vector.clear(); }
allocator_type get_allocator() const noexcept { return this->m_vector.get_allocator(); }
friend bool operator==(const F& x, const F& y) { return x.m_vector == y.m_vector; }
friend bool operator!=(const F& x, const F& y) { return x.m_vector != y.m_vector; }
friend void swap(F& x, F& y) noexcept { x.m_vector.swap(y.m_vector); }
F operator+() const {
return *this;
}
F operator-() const {
F res(*this);
for (auto& e : res) {
e = -e;
}
return res;
}
F& operator++() {
if (!this->empty()) ++(*this)[0];
return *this;
}
F operator++(int) {
const auto self = *this;
++*this;
return self;
}
F& operator--() {
if (!this->empty()) --(*this)[0];
return *this;
}
F operator--(int) {
const auto self = *this;
--*this;
return self;
}
F& operator*=(const M& g) {
for (auto& e : *this) {
e *= g;
}
return *this;
}
F& operator/=(const M& g) {
assert(std::gcd(g.val(), M::mod()) == 1);
*this *= g.inv();
return *this;
}
F& operator+=(const F& g) {
const int n = this->size();
const int m = g.size();
for (int i = 0; i < std::min(n, m); ++i) {
(*this)[i] += g[i];
}
return *this;
}
F& operator-=(const F& g) {
const int n = this->size();
const int m = g.size();
for (int i = 0; i < std::min(n, m); ++i) {
(*this)[i] -= g[i];
}
return *this;
}
F& operator<<=(const int d) {
if (d < 0) *this >>= -d;
const int n = this->size();
this->resize(std::max(0, n - d));
this->insert(this->begin(), std::min(n, d), M::raw(0));
return *this;
}
F& operator>>=(const int d) {
if (d < 0) *this <<= -d;
const int n = this->size();
this->erase(this->begin(), this->begin() + std::min(n, d));
this->resize(n);
return *this;
}
F& multiply_inplace(const F& g, const int d) {
assert(d >= 0);
this->m_vector = tools::convolution(*this | std::views::take(d), g | std::views::take(d));
this->m_vector.resize(d);
return *this;
}
F& multiply_inplace(const F& g) { return this->multiply_inplace(g, this->size()); }
F& operator*=(const F& g) { return this->multiply_inplace(g); }
F multiply(const F& g, const int d) const { return F(*this).multiply_inplace(g, d); }
F multiply(const F& g) const { return this->multiply(g, this->size()); }
private:
F inv_regular(const int d) const {
assert(d > 0);
assert(M::mod() > 1);
assert(!this->empty());
assert(std::gcd((*this)[0].val(), M::mod()) == 1);
const int n = this->size();
F res{(*this)[0].inv()};
for (int m = 1; m < d; m *= 2) {
F f(this->begin(), this->begin() + std::min(n, 2 * m));
f *= -1;
F r(res);
r.multiply_inplace(r, 2 * m);
r.multiply_inplace(f);
r += res;
r += res;
res = std::move(r);
}
res.resize(d);
return res;
}
template <typename M_ = M>
F inv_faster(const int d) const {
static_assert(atcoder::internal::is_static_modint<M>::value);
static_assert(2 <= M::mod() && M::mod() <= 2000000000);
static_assert(tools::is_prime(M::mod()));
assert(d > 0);
assert(!this->empty());
assert(tools::pow2(tools::ceil_log2(d)) <= pow2_k(M::mod()));
assert(std::gcd((*this)[0].val(), M::mod()) == 1);
const int n = this->size();
F res{(*this)[0].inv()};
for (int m = 1; m < d; m *= 2) {
F f(this->begin(), this->begin() + std::min(n, 2 * m));
F r(res);
f.resize(2 * m);
atcoder::internal::butterfly(f.m_vector);
r.resize(2 * m);
atcoder::internal::butterfly(r.m_vector);
for (int i = 0; i < 2 * m; ++i) {
f[i] *= r[i];
}
atcoder::internal::butterfly_inv(f.m_vector);
f.erase(f.begin(), f.begin() + m);
f.resize(2 * m);
atcoder::internal::butterfly(f.m_vector);
for (int i = 0; i < 2 * m; ++i) {
f[i] *= r[i];
}
atcoder::internal::butterfly_inv(f.m_vector);
M iz = M(2 * m).inv();
iz *= -iz;
for (int i = 0; i < m; ++i) {
f[i] *= iz;
}
res.insert(res.end(), f.begin(), f.begin() + m);
}
res.resize(d);
return res;
}
public:
F inv(const int d) const {
assert(d >= 0);
if (d == 0) return F();
if (M::mod() == 1) return F(d);
assert(!this->empty());
assert(std::gcd((*this)[0].val(), M::mod()) == 1);
if constexpr (atcoder::internal::is_static_modint<M>::value && M::mod() <= 2000000000 && tools::is_prime(M::mod())) {
if (tools::pow2(tools::ceil_log2(d)) <= pow2_k(M::mod())) {
return this->inv_faster(d);
} else {
return this->inv_regular(d);
}
} else {
return this->inv_regular(d);
}
}
F inv() const { return this->inv(this->size()); }
F& divide_inplace(const F& g, const int d) {
assert(d >= 0);
this->m_vector = tools::convolution(*this | std::views::take(d), g.inv(d));
this->m_vector.resize(d);
return *this;
}
F& divide_inplace(const F& g) { return this->divide_inplace(g, this->size()); }
F& operator/=(const F& g) { return this->divide_inplace(g); }
F divide(const F& g, const int d) const { return F(*this).divide_inplace(g, d); }
F divide(const F& g) const { return this->divide(g, this->size()); }
// sparse
template <class InputIterator>
F& multiply_inplace(InputIterator g_begin, const InputIterator g_end) {
assert(std::is_sorted(g_begin, g_end, tools::less_by_first()));
const int n = this->size();
if (g_begin == g_end) {
std::fill(this->begin(), this->end(), M::raw(0));
return *this;
}
auto [d, c] = *g_begin;
if (d == 0) {
++g_begin;
} else {
c = M::raw(0);
}
for (int i = n - 1; i >= 0; --i) {
(*this)[i] *= c;
for (auto it = g_begin; it != g_end; ++it) {
const auto& [j, b] = *it;
if (j > i) break;
(*this)[i] += (*this)[i - j] * b;
}
}
return *this;
}
F& multiply_inplace(const std::initializer_list<std::pair<int, M>> il) { return this->multiply_inplace(il.begin(), il.end()); }
template <class InputIterator>
F multiply(const InputIterator g_begin, const InputIterator g_end) const { return F(*this).multiply_inplace(g_begin, g_end); }
F multiply(const std::initializer_list<std::pair<int, M>> il) const { return this->multiply(il.begin(), il.end()); }
template <class InputIterator>
F& divide_inplace(InputIterator g_begin, const InputIterator g_end) {
assert(g_begin != g_end);
assert(std::is_sorted(g_begin, g_end, tools::less_by_first()));
const int n = this->size();
if (n == 0) return *this;
if (M::mod() == 1) return *this;
const auto [d, c] = *g_begin;
assert(d == 0 && std::gcd(c.val(), M::mod()) == 1);
const M ic = c.inv();
++g_begin;
for (int i = 0; i < n; ++i) {
for (auto it = g_begin; it != g_end; ++it) {
const auto& [j, b] = *it;
if (j > i) break;
(*this)[i] -= (*this)[i - j] * b;
}
(*this)[i] *= ic;
}
return *this;
}
F& divide_inplace(const std::initializer_list<std::pair<int, M>> il) { return this->divide_inplace(il.begin(), il.end()); }
template <class InputIterator>
F divide(const InputIterator g_begin, const InputIterator g_end) const { return F(*this).divide_inplace(g_begin, g_end); }
F divide(const std::initializer_list<std::pair<int, M>> il) const { return this->divide(il.begin(), il.end()); }
// multiply and divide (1 + cz^d)
F& multiply_inplace(const int d, const M c) {
assert(d > 0);
const int n = this->size();
if (c == M(1)) {
for (int i = n - d - 1; i >= 0; --i) {
(*this)[i + d] += (*this)[i];
}
} else if (c == M(-1)) {
for (int i = n - d - 1; i >= 0; --i) {
(*this)[i + d] -= (*this)[i];
}
} else {
for (int i = n - d - 1; i >= 0; --i) {
(*this)[i + d] += (*this)[i] * c;
}
}
return *this;
}
F multiply(const int d, const M c) const { return F(*this).multiply_inplace(d, c); }
F& divide_inplace(const int d, const M c) {
assert(d > 0);
const int n = this->size();
if (c == M(1)) {
for (int i = 0; i < n - d; ++i) {
(*this)[i + d] -= (*this)[i];
}
} else if (c == M(-1)) {
for (int i = 0; i < n - d; ++i) {
(*this)[i + d] += (*this)[i];
}
} else {
for (int i = 0; i < n - d; ++i) {
(*this)[i + d] -= (*this)[i] * c;
}
}
return *this;
}
F divide(const int d, const M c) const { return F(*this).divide_inplace(d, c); }
F& integral_inplace() {
const int n = this->size();
assert(is_leq_lpf_of_M(n));
if (n == 0) return *this;
if (n == 1) return *this = F{0};
this->insert(this->begin(), 0);
this->pop_back();
std::vector<M> inv(n);
inv[1] = M(1);
int p = M::mod();
for (int i = 2; i < n; ++i) {
inv[i] = -inv[p % i] * (p / i);
}
for (int i = 2; i < n; ++i) {
(*this)[i] *= inv[i];
}
return *this;
}
F integral() const { return F(*this).integral_inplace(); }
F& derivative_inplace() {
const int n = this->size();
if (n == 0) return *this;
for (int i = 2; i < n; ++i) {
(*this)[i] *= i;
}
this->erase(this->begin());
this->push_back(0);
return *this;
}
F derivative() const { return F(*this).derivative_inplace(); }
F& log_inplace(const int d) {
assert(d >= 0);
assert(is_leq_lpf_of_M(d));
this->resize(d);
if (d == 0) return *this;
assert((*this)[0] == M(1));
const F f_inv = this->inv();
this->derivative_inplace();
this->multiply_inplace(f_inv);
this->integral_inplace();
return *this;
}
F& log_inplace() { return this->log_inplace(this->size()); }
F log(const int d) const { return F(*this).log_inplace(d); }
F log() const { return this->log(this->size()); }
private:
F& exp_inplace_regular(const int d) {
assert(d >= 0);
assert(is_leq_lpf_of_M(d));
assert(this->empty() || (*this)[0] == M::raw(0));
const int n = this->size();
F g{1};
for (int m = 1; m < d; m *= 2) {
F r(g);
r.resize(2 * m);
r.log_inplace();
r *= -1;
r += F(this->begin(), this->begin() + std::min(n, 2 * m));
++r[0];
r.multiply_inplace(g);
g = std::move(r);
}
g.resize(d);
*this = std::move(g);
return *this;
}
template <typename M_ = M>
F& exp_inplace_faster(const int d) {
static_assert(atcoder::internal::is_static_modint<M>::value);
static_assert(2 <= M::mod() && M::mod() <= 2000000000);
static_assert(tools::is_prime(M::mod()));
assert(d > 0);
assert(is_leq_lpf_of_M(d));
assert(tools::pow2(tools::ceil_log2(d)) <= pow2_k(M::mod()));
assert(this->empty() || (*this)[0] == M::raw(0));
F g{1}, g_fft{1, 1};
this->resize(d);
(*this)[0] = 1;
F h_drv(this->derivative());
for (int m = 2; m < d; m *= 2) {
// prepare
F f_fft(this->begin(), this->begin() + m);
f_fft.resize(2 * m);
atcoder::internal::butterfly(f_fft.m_vector);
// Step 2.a'
{
F g_(m);
for (int i = 0; i < m; ++i) {
g_[i] = f_fft[i] * g_fft[i];
}
atcoder::internal::butterfly_inv(g_.m_vector);
g_.erase(g_.begin(), g_.begin() + m / 2);
g_.resize(m);
atcoder::internal::butterfly(g_.m_vector);
for (int i = 0; i < m; ++i) {
g_[i] *= g_fft[i];
}
atcoder::internal::butterfly_inv(g_.m_vector);
g_.resize(m / 2);
g_ /= M(-m) * m;
g.insert(g.end(), g_.begin(), g_.begin() + m / 2);
}
// Step 2.b'--d'
F t(this->begin(), this->begin() + m);
t.derivative_inplace();
{
// Step 2.b'
F r{h_drv.begin(), h_drv.begin() + m - 1};
// Step 2.c'
r.resize(m);
atcoder::internal::butterfly(r.m_vector);
for (int i = 0; i < m; ++i) {
r[i] *= f_fft[i];
}
atcoder::internal::butterfly_inv(r.m_vector);
r /= -m;
// Step 2.d'
t += r;
t.insert(t.begin(), t.back());
t.pop_back();
}
// Step 2.e'
if (2 * m < d) {
t.resize(2 * m);
atcoder::internal::butterfly(t.m_vector);
g_fft = g;
g_fft.resize(2*m);
atcoder::internal::butterfly(g_fft.m_vector);
for (int i = 0; i < 2 * m; ++i) {
t[i] *= g_fft[i];
}
atcoder::internal::butterfly_inv(t.m_vector);
t.resize(m);
t /= 2 * m;
} else { // この場合分けをしても数パーセントしか速くならない
F g1(g.begin() + m / 2, g.end());
F s1(t.begin() + m / 2, t.end());
t.resize(m/2);
g1.resize(m);
atcoder::internal::butterfly(g1.m_vector);
t.resize(m);
atcoder::internal::butterfly(t.m_vector);
s1.resize(m);
atcoder::internal::butterfly(s1.m_vector);
for (int i = 0; i < m; ++i) {
s1[i] = g_fft[i] * s1[i] + g1[i] * t[i];
}
for (int i = 0; i < m; ++i) {
t[i] *= g_fft[i];
}
atcoder::internal::butterfly_inv(t.m_vector);
atcoder::internal::butterfly_inv(s1.m_vector);
for (int i = 0; i < m / 2; ++i) {
t[i + m / 2] += s1[i];
}
t /= m;
}
// Step 2.f'
F v(this->begin() + m, this->begin() + std::min<int>(d, 2 * m));
v.resize(m);
t.insert(t.begin(), m - 1, 0);
t.push_back(0);
t.integral_inplace();
for (int i = 0; i < m; ++i) {
v[i] -= t[m + i];
}
// Step 2.g'
v.resize(2 * m);
atcoder::internal::butterfly(v.m_vector);
for (int i = 0; i < 2 * m; ++i) {
v[i] *= f_fft[i];
}
atcoder::internal::butterfly_inv(v.m_vector);
v.resize(m);
v /= 2 * m;
// Step 2.h'
for (int i = 0; i < std::min(d - m, m); ++i) {
(*this)[m + i] = v[i];
}
}
return *this;
}
public:
F& exp_inplace(const int d) {
assert(d >= 0);
assert(is_leq_lpf_of_M(d));
assert(this->empty() || (*this)[0] == M::raw(0));
if (d == 0) {
this->clear();
return *this;
}
if constexpr (atcoder::internal::is_static_modint<M>::value && M::mod() <= 2000000000 && tools::is_prime(M::mod())) {
if (tools::pow2(tools::ceil_log2(d)) <= pow2_k(M::mod())) {
return this->exp_inplace_faster(d);
} else {
return this->exp_inplace_regular(d);
}
} else {
return this->exp_inplace_regular(d);
}
}
F& exp_inplace() { return this->exp_inplace(this->size()); }
F exp(const int d) const { return F(*this).exp_inplace(d); }
F exp() const { return this->exp(this->size()); }
private:
F& pow_inplace_regular(long long k, const int d, const int l) {
assert(k > 0);
assert(d > 0);
assert(l >= 0);
assert(d - l * k > 0);
this->erase(this->begin(), this->begin() + l);
this->resize(d - l * k);
F sum(d - l * k);
for (F p = *this; k > 0; k /= 2, p *= p) {
if (k & 1) sum += p;
}
*this = std::move(sum);
this->insert(this->begin(), l * k, 0);
return *this;
}
F& pow_inplace_faster(const long long k, const int d, const int l) {
assert(k > 0);
assert(d > 0);
assert(l >= 0);
assert(d - l * k > 0);
assert(is_leq_lpf_of_M(d - l * k));
assert(std::gcd((*this)[l].val(), M::mod()) == 1);
M c{(*this)[l]};
this->erase(this->begin(), this->begin() + l);
*this /= c;
this->log_inplace(d - l * k);
*this *= k;
this->exp_inplace();
*this *= c.pow(k);
this->insert(this->begin(), l * k, 0);
return *this;
}
public:
F& pow_inplace(const long long k, const int d) {
assert(k >= 0);
assert(d >= 0);
const int n = this->size();
if (d == 0) {
this->clear();
return *this;
}
if (k == 0) {
*this = F(d);
(*this)[0] = M(1);
return *this;
}
int l = 0;
while (l < n && (*this)[l] == M::raw(0)) ++l;
if (l == n || l > (d - 1) / k) {
return *this = F(d);
}
if (std::gcd((*this)[l].val(), M::mod()) == 1 && is_leq_lpf_of_M(d - l * k)) {
return this->pow_inplace_faster(k, d, l);
} else {
return this->pow_inplace_regular(k, d, l);
}
}
F& pow_inplace(const long long k) { return this->pow_inplace(k, this->size()); }
F pow(const long long k, const int d) const { return F(*this).pow_inplace(k, d); }
F pow(const long long k) const { return this->pow(k, this->size()); }
F operator()(const F& g) const {
assert(g.empty() || g[0] == M::raw(0));
const int n = this->size();
F h(n);
if (n == 0) return h;
const int m = g.size();
int l;
for (l = 0; l < std::min(m, n) && g[l] == M::raw(0); ++l);
h[0] = (*this)[0];
if (l == std::min(m, n)) return h;
const F g_1(g.begin() + l, g.begin() + std::min(m, n));
for (int i = l; i < std::min(m, n); ++i) {
h[i] += (*this)[1] * g[i];
}
auto g_k = g_1;
for (int k = 2, d; (d = std::min(k * (m - l - 1) + 1, n - l * k)) > 0; ++k) {
g_k.multiply_inplace(g_1, d);
for (int i = l * k; i < l * k + d; ++i) {
h[i] += (*this)[k] * g_k[i - l * k];
}
}
return h;
}
F compositional_inverse() const {
assert(this->size() >= 2);
assert((*this)[0] == M::raw(0));
assert(std::gcd((*this)[1].val(), M::mod()) == 1);
const int n = this->size();
std::vector<F> f;
f.reserve(std::max(2, n - 1));
f.emplace_back(n);
f[0][0] = M::raw(1);
f.push_back(*this);
for (int i = 2; i < n - 1; ++i) {
f.push_back(f.back() * f[1]);
}
std::vector<M> invpow_f11;
invpow_f11.reserve(n);
invpow_f11.push_back(M::raw(1));
invpow_f11.push_back(f[1][1].inv());
for (int i = 2; i < n; ++i) {
invpow_f11.push_back(invpow_f11.back() * invpow_f11[1]);
}
F g(n);
g[1] = invpow_f11[1];
for (int i = 2; i < n; ++i) {
for (int j = 1; j < i; ++j) {
g[i] -= f[j][i] * g[j];
}
g[i] *= invpow_f11[i];
}
return g;
}
friend F operator*(const F& f, const M& g) { return F(f) *= g; }
friend F operator*(const M& f, const F& g) { return F(g) *= f; }
friend F operator/(const F& f, const M& g) { return F(f) /= g; }
friend F operator+(const F& f, const F& g) { return F(f) += g; }
friend F operator-(const F& f, const F& g) { return F(f) -= g; }
friend F operator*(const F& f, const F& g) { return F(f) *= g; }
friend F operator/(const F& f, const F& g) { return F(f) /= g; }
friend F operator<<(const F& f, const int d) { return F(f) <<= d; }
friend F operator>>(const F& f, const int d) { return F(f) >>= d; }
};
template <tools::modint M>
tools::fps<M> detail::exp::impl<tools::fps<M>>::operator()(auto&& f) const {
return std::forward<decltype(f)>(f).exp();
};
template <tools::modint M>
tools::fps<M> detail::log::impl<tools::fps<M>>::operator()(auto&& f) const {
return std::forward<decltype(f)>(f).log();
};
template <tools::modint M, tools::integral E>
tools::fps<M> detail::pow::impl<tools::fps<M>, E>::operator()(auto&& f, const E k) const {
return std::forward<decltype(f)>(f).pow(k);
};
}
#line 1 "tools/pow_mod_cache.hpp"
#line 9 "tools/pow_mod_cache.hpp"
#include <optional>
#line 1 "tools/find_cycle.hpp"
#line 5 "tools/find_cycle.hpp"
namespace tools {
template <typename T, typename F>
std::pair<long long, long long> find_cycle(const T& seed, const F& f) {
auto i = 1LL;
auto j = 2LL;
T x = f(seed);
T y = f(f(seed));
for (; x != y; ++i, j += 2, x = f(x), y = f(f(y)));
i = 0;
x = seed;
for (; x != y; ++i, ++j, x = f(x), y = f(y));
const auto head = i;
++i;
j = i + 1;
x = f(x);
y = f(f(y));
for (; x != y; ++i, j += 2, x = f(x), y = f(f(y)));
const auto cycle = j - i;
return std::make_pair(head, cycle);
}
}
#line 1 "tools/floor.hpp"
#line 7 "tools/floor.hpp"
namespace tools {
template <tools::non_bool_integral M, tools::non_bool_integral N>
constexpr std::common_type_t<M, N> floor(const M x, const N y) noexcept {
assert(y != 0);
if (y >= 0) {
if (x >= 0) {
return x / y;
} else {
return (x + 1) / y - 1;
}
} else {
if (x > 0) {
return (x - 1) / y - 1;
} else {
return x / y;
}
}
}
}
#line 17 "tools/pow_mod_cache.hpp"
namespace tools {
template <tools::modint_compatible M>
class pow_mod_cache {
std::vector<M> m_pow;
std::vector<M> m_cumsum;
std::vector<M> m_inv_pow;
std::vector<M> m_inv_cumsum;
std::optional<std::pair<long long, long long>> m_period;
public:
pow_mod_cache() = default;
explicit pow_mod_cache(const M base) : m_pow({M(1), base}), m_cumsum({M::raw(0)}), m_inv_pow({M(1)}), m_inv_cumsum({M::raw(0)}) {
if (base == M(-1)) {
if (M::mod() > 2) {
this->m_period = std::make_pair(0LL, 2LL);
} else {
this->m_period = std::make_pair(0LL, 1LL);
this->m_pow.resize(1);
}
this->m_inv_pow.clear();
this->m_inv_cumsum.clear();
}
}
explicit pow_mod_cache(std::integral auto&& base) : pow_mod_cache(M(base)) {
}
M operator[](const long long n) {
if (!this->m_period) {
if (std::max<long long>(std::ssize(this->m_pow) - 1, n) - std::min<long long>(n, -(std::ssize(this->m_inv_pow) - 1)) + 1 < M::mod() - 1) {
if (n >= 0) {
const long long size = std::ssize(this->m_pow);
this->m_pow.resize(std::max(size, n + 1));
for (long long i = size; i < std::ssize(this->m_pow); ++i) {
this->m_pow[i] = this->m_pow[i - 1] * this->m_pow[1];
}
return this->m_pow[n];
} else {
if (this->m_inv_pow.size() == 1) {
this->m_inv_pow.push_back(this->m_pow[1].inv());
}
const long long size = std::ssize(this->m_inv_pow);
this->m_inv_pow.resize(std::max(size, -n + 1));
for (long long i = size; i < std::ssize(this->m_inv_pow); ++i) {
this->m_inv_pow[i] = this->m_inv_pow[i - 1] * this->m_inv_pow[1];
}
return this->m_inv_pow[-n];
}
}
this->m_period = tools::find_cycle(this->m_pow[0], [&](const M& prev) { return prev * this->m_pow[1]; });
const long long size = std::ssize(this->m_pow);
this->m_pow.resize(this->m_period->first + this->m_period->second);
for (long long i = size; i < std::ssize(this->m_pow); ++i) {
this->m_pow[i] = this->m_pow[i - 1] * this->m_pow[1];
}
this->m_inv_pow.clear();
this->m_inv_cumsum.clear();
}
if (this->m_period->first == 0) {
return this->m_pow[tools::mod(n, this->m_period->second)];
} else {
assert(n >= 0);
if (n < this->m_period->first + this->m_period->second) {
return this->m_pow[n];
} else {
return this->m_pow[(n - this->m_period->first) % this->m_period->second + this->m_period->first];
}
}
}
M sum(const long long l, const long long r) {
if (l >= r) return M::raw(0);
(*this)[r - 1];
(*this)[l];
{
const long long size = std::ssize(this->m_cumsum);
this->m_cumsum.resize(this->m_pow.size() + 1);
for (long long i = size; i < std::ssize(this->m_cumsum); ++i) {
this->m_cumsum[i] = this->m_cumsum[i - 1] + this->m_pow[i - 1];
}
}
if (!this->m_period) {
const long long size = std::ssize(this->m_inv_cumsum);
this->m_inv_cumsum.resize(this->m_inv_pow.size() + 1);
for (long long i = size; i < std::ssize(this->m_inv_cumsum); ++i) {
this->m_inv_cumsum[i] = this->m_inv_cumsum[i - 1] + this->m_pow[i - 1];
}
if (l >= 0) {
return this->m_cumsum[r] - this->m_cumsum[l];
} else if (r <= 0) {
return this->m_inv_cumsum[-l] - this->m_inv_cumsum[-r];
} else {
return (this->m_inv_cumsum[-l] - this->m_inv_cumsum[1]) + (this->m_cumsum[r] - this->m_cumsum[0]);
}
}
static const auto cumsum = [&](const long long ll, const long long rr) {
return this->m_cumsum[rr] - this->m_cumsum[ll];
};
if (l >= 0) {
static const auto f = [&](const long long x) {
if (x <= this->m_period->first + this->m_period->second) {
return cumsum(0, x);
} else {
return cumsum(0, this->m_period->first) +
cumsum(this->m_period->first, this->m_period->first + this->m_period->second) * ((x - this->m_period->first) / this->m_period->second) +
cumsum(this->m_period->first, (x - this->m_period->first) % this->m_period->second + this->m_period->first);
}
};
return f(r) - f(l);
} else {
const auto& n = this->m_period->second;
return cumsum(tools::mod(l, n), n) + cumsum(0, tools::mod(r, n)) + cumsum(0, n) * M(tools::floor(r, n) - tools::ceil(l, n));
}
}
};
}
#line 17 "tools/binomial_product.hpp"
namespace tools {
template <std::ranges::input_range R>
requires tools::modint<std::tuple_element_t<0, std::ranges::range_value_t<R>>>
&& std::same_as<std::tuple_element_t<1, std::ranges::range_value_t<R>>, std::tuple_element_t<0, std::ranges::range_value_t<R>>>
&& std::integral<std::tuple_element_t<2, std::ranges::range_value_t<R>>>
&& std::integral<std::tuple_element_t<3, std::ranges::range_value_t<R>>>
auto binomial_product(R&& f, const int n) {
using M = std::tuple_element_t<0, std::ranges::range_value_t<R>>;
using F = tools::fps<M>;
assert(n >= 0);
assert(std::ranges::all_of(std::views::iota(2, std::max(2, n)), [](const auto i) { return M::mod() % i != 0; }));
M multiplier(1);
int offset = 0;
std::vector<std::tuple<M, int, int>> factors;
for (const auto& [a, b, c, d] : f) {
assert(c >= 0);
assert(d >= 0);
if (d == 0) {
continue;
}
if (c == 0) {
multiplier *= (a + b).pow(d);
continue;
}
if (a.val() == 0) {
if (d >= tools::ceil(n - offset, c)) {
return F(n);
}
multiplier *= b.pow(d);
offset += c * d;
continue;
}
multiplier *= a.pow(d);
factors.emplace_back(b / a, c, d);
}
assert((factors | std::views::transform([](const auto& factor) { return std::get<1>(factor); }) | std::ranges::to<std::set>()).size() == factors.size());
tools::fact_mod_cache<M> cache;
F res(n - offset);
for (const auto& [b, c, d] : factors) {
tools::pow_mod_cache<M> pow_b(b);
for (int i = 1; c < tools::ceil(n - offset, i); ++i) {
res[c * i] += M(i % 2 == 0 ? -d : d) * pow_b[i] * cache.inv(i);
}
}
res.exp_inplace();
res *= multiplier;
res <<= offset;
return res;
}
}