This documentation is automatically generated by online-judge-tools/verification-helper
#include "math/fps/polynomial-interpolation-gemoetric.hpp"
#pragma once
#include "fps.hpp"
#include "multipoint-evaluation-geometric.hpp"
template <typename T>
FormalPowerSeries<T> polynomial_interpolation_gemoetric(vector<T> y, const T& a, const T& r) {
const int n = y.size();
if (n == 0) return {};
if (n == 1) return {y[0]};
if (r == 0) {
return {y[1], (y[0] - y[1]) / a};
}
vector<T> pow_r(n + 1), pow_invr(n + 1), inv_s(n);
T s = 1;
const T inv_r = T(1) / r;
{
pow_r[0] = pow_invr[0] = inv_s[0] = 1;
rep(i, n) {
if (i) s *= (1 - pow_r[i]);
pow_r[i + 1] = pow_r[i] * r;
pow_invr[i + 1] = pow_invr[i] * inv_r;
}
inv_s[n - 1] = T(1) / s;
s *= (1 - pow_r[n]);
rrep(i, n - 1) inv_s[i] = inv_s[i + 1] * (1 - pow_r[i + 1]);
}
T v = 1;
rep(i, n) {
y[i] *= v * inv_s[i] * inv_s[n - i - 1];
if (i & 1) y[i] = -y[i];
if (i != n - 1) v *= pow_invr[n - 2 - i];
}
FormalPowerSeries<T> f1(n + 1);
v = 1;
rep(i, n + 1) {
if (i == 0 || i == n)
f1[i] = v;
else
f1[i] = v * s * inv_s[i] * inv_s[n - i];
if (i & 1) f1[i] = -f1[i];
v *= pow_r[i];
}
FormalPowerSeries<T> f2 = multipoint_evaluation_geometric(FormalPowerSeries<T>(y), T(1), r, n);
auto res = (f1 * f2);
res.resize(n);
reverse(res.begin(), res.end());
const T inva = T(1) / a;
v = 1;
rep(i, n) res[i] *= v, v *= inva;
return res;
}
/**
* @brief Polynomial Interpolation (Geometric Sequence)
* @ref https://noshi91.github.io/algorithm-encyclopedia/polynomial-interpolation-geometric
*/
#line 2 "template/template.hpp"
#include <bits/stdc++.h>
#line 3 "template/alias.hpp"
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i128 = __int128_t;
using u128 = __uint128_t;
using pi = std::pair<int, int>;
using pl = std::pair<ll, ll>;
using vi = std::vector<int>;
using vl = std::vector<ll>;
using vs = std::vector<std::string>;
using vc = std::vector<char>;
using vvl = std::vector<vl>;
using vd = std::vector<double>;
using vp = std::vector<pl>;
using vb = std::vector<bool>;
template <typename T>
struct infinity {
static constexpr T max = std::numeric_limits<T>::max();
static constexpr T min = std::numeric_limits<T>::min();
static constexpr T value = std::numeric_limits<T>::max() / 2;
static constexpr T mvalue = std::numeric_limits<T>::min() / 2;
};
template <typename T>
constexpr T INF = infinity<T>::value;
constexpr ll inf = INF<ll>;
constexpr ld EPS = 1e-8;
constexpr ld PI = 3.1415926535897932384626;
constexpr int dx[8] = {-1, 0, 1, 0, 1, -1, -1, 1};
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
#line 3 "template/macro.hpp"
#ifndef __COUNTER__
#define __COUNTER__ __LINE__
#endif
#define SELECT4(a, b, c, d, e, ...) e
#define SELECT3(a, b, c, d, ...) d
#define REP_1(a, c) for (ll REP_##c = 0; REP_##c < (ll)(a); ++REP_##c)
#define REP1(a) REP_1(a, __COUNTER__)
#define REP2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define REP3(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define REP4(i, a, b, c) for (ll i = (ll)(a); i < (ll)(b); i += (ll)(c))
#define rep(...) SELECT4(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
#define RREP_1(a, c) for (ll RREP_##c = (ll)(a) - 1; RREP_##c >= 0; --RREP_##c)
#define RREP1(a) RREP_1(a, __COUNTER__)
#define RREP2(i, a) for (ll i = (ll)(a) - 1; i >= 0; --i)
#define RREP3(i, a, b) for (ll i = (ll)(b) - 1; i >= (ll)(a); --i)
#define rrep(...) SELECT3(__VA_ARGS__, RREP3, RREP2, RREP1)(__VA_ARGS__)
#define all(v) std::begin(v), std::end(v)
#define rall(v) std::rbegin(v), std::rend(v)
#define INT(...) \
int __VA_ARGS__; \
scan(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
scan(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
scan(__VA_ARGS__)
#define CHR(...) \
char __VA_ARGS__; \
scan(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
scan(__VA_ARGS__)
#define LD(...) \
ld __VA_ARGS__; \
scan(__VA_ARGS__)
#define pb push_back
#define eb emplace_back
#line 3 "template/type-traits.hpp"
#line 5 "template/type-traits.hpp"
template <typename T, typename... Args>
struct function_traits_impl {
using return_type = T;
static constexpr std::size_t arg_size = sizeof...(Args);
template <std::size_t idx>
using argument_type = typename std::tuple_element<idx, std::tuple<Args...>>::type;
using argument_types = std::tuple<Args...>;
};
template <typename>
struct function_traits_helper;
template <typename T, typename Tp, typename... Args>
struct function_traits_helper<T (Tp::*)(Args...)> : function_traits_impl<T, Args...> {};
template <typename T, typename Tp, typename... Args>
struct function_traits_helper<T (Tp::*)(Args...) const> : function_traits_impl<T, Args...> {};
template <typename T, typename Tp, typename... Args>
struct function_traits_helper<T (Tp::*)(Args...)&> : function_traits_impl<T, Args...> {};
template <typename T, typename Tp, typename... Args>
struct function_traits_helper<T (Tp::*)(Args...) const&> : function_traits_impl<T, Args...> {};
template <typename F>
using function_traits = function_traits_helper<decltype(&std::remove_reference<F>::type::operator())>;
template <typename F>
using function_return_type = typename function_traits<F>::return_type;
template <typename F, std::size_t idx>
using function_argument_type = typename function_traits<F>::template argument_type<idx>;
template <typename F>
using function_argument_types = typename function_traits<F>::argument_types;
template <class T>
using is_signed_int = std::integral_constant<bool, (std::is_integral<T>::value && std::is_signed<T>::value) || std::is_same<T, __int128_t>::value>;
template <class T>
using is_unsigned_int = std::integral_constant<bool, (std::is_integral<T>::value && std::is_unsigned<T>::value) || std::is_same<T, __uint128_t>::value>;
template <class T>
using is_int = std::integral_constant<bool, is_signed_int<T>::value || is_unsigned_int<T>::value>;
template <typename T, typename = void>
struct is_range : std::false_type {};
template <typename T>
struct is_range<
T,
decltype(all(std::declval<typename std::add_lvalue_reference<T>::type>()), (void)0)> : std::true_type {};
template <std::size_t size>
struct int_least {
static_assert(size <= 128, "size must be less than or equal to 128");
using type = typename std::conditional<
size <= 8, std::int_least8_t,
typename std::conditional<
size <= 16, std::int_least16_t,
typename std::conditional<
size <= 32, std::int_least32_t,
typename std::conditional<size <= 64, std::int_least64_t, __int128_t>::type>::type>::type>::type;
};
template <std::size_t size>
using int_least_t = typename int_least<size>::type;
template <std::size_t size>
struct uint_least {
static_assert(size <= 128, "size must be less than or equal to 128");
using type = typename std::conditional<
size <= 8, std::uint_least8_t,
typename std::conditional<
size <= 16, std::uint_least16_t,
typename std::conditional<
size <= 32, std::uint_least32_t,
typename std::conditional<size <= 64, std::uint_least64_t, __uint128_t>::type>::type>::type>::type;
};
template <std::size_t size>
using uint_least_t = typename uint_least<size>::type;
template <typename T>
using double_size_int = int_least<std::numeric_limits<T>::digits * 2 + 1>;
template <typename T>
using double_size_int_t = typename double_size_int<T>::type;
template <typename T>
using double_size_uint = uint_least<std::numeric_limits<T>::digits * 2>;
template <typename T>
using double_size_uint_t = typename double_size_uint<T>::type;
template <typename T>
using double_size = typename std::conditional<std::is_signed<T>::value, double_size_int<T>, double_size_uint<T>>::type;
template <typename T>
using double_size_t = typename double_size<T>::type;
#line 2 "template/in.hpp"
#include <unistd.h>
#line 5 "template/in.hpp"
namespace fastio {
template <std::size_t BUFF_SIZE = 1 << 17, int decimal_precision = 16>
struct Scanner {
private:
template <typename, typename = void>
struct has_scan : std::false_type {};
template <class T>
struct has_scan<T, decltype(std::declval<T>().scan(std::declval<Scanner&>()), (void)0)> : std::true_type {};
int fd;
char buffer[BUFF_SIZE + 1];
int idx, sz;
bool state;
inline void load() {
int len = sz - idx;
if (idx < len) return;
std::memcpy(buffer, buffer + idx, len);
sz = len + read(fd, buffer + len, BUFF_SIZE - len);
idx = 0;
buffer[sz] = 0;
}
inline char cur() {
if (idx == sz) load();
if (idx == sz) {
state = false;
return '\0';
}
return buffer[idx];
}
inline void next() {
if (idx == sz) load();
if (idx == sz) return;
idx++;
}
public:
Scanner() : Scanner(0) {}
explicit Scanner(int fd) : fd(fd), idx(0), sz(0), state(true) {}
explicit Scanner(FILE* file) : fd(fileno(file)), idx(0), sz(0), state(true) {}
inline char scan_char() {
if (idx == sz) load();
return (idx == sz ? '\0' : buffer[idx++]);
}
Scanner ignore(int n = 1) {
if (idx + n > sz) load();
idx += n;
return (*this);
}
inline void skip_space() {
if (idx == sz) load();
while (('\t' <= cur() && cur() <= '\r') || cur() == ' ') {
if (++idx == sz) load();
}
}
void scan(char& a) {
skip_space();
a = scan_char();
}
void scan(std::string& a) {
skip_space();
a.clear();
while (cur() != '\0' && (buffer[idx] < '\t' || '\r' < buffer[idx]) && buffer[idx] != ' ') {
a += scan_char();
}
}
template <std::size_t len>
void scan(std::bitset<len>& a) {
skip_space();
if (idx + len > sz) load();
rrep(i, len) a[i] = (buffer[idx++] != '0');
}
template <typename T, typename std::enable_if<is_int<T>::value && !has_scan<T>::value>::type* = nullptr>
void scan(T& a) {
skip_space();
bool neg = false;
if constexpr (std::is_signed<T>::value || std::is_same_v<T, __int128_t>) {
if (cur() == '-') {
neg = true;
next();
}
}
if (idx + 40 > sz && (idx == sz || ('0' <= buffer[sz - 1] && buffer[sz - 1] <= '9'))) load();
a = 0;
while ('0' <= buffer[idx] && buffer[idx] <= '9') {
a = a * 10 + (buffer[idx++] & 15);
}
if constexpr (std::is_signed<T>::value || std::is_same<T, __int128_t>::value) {
if (neg) a = -a;
}
}
template <typename T, typename std::enable_if<std::is_floating_point<T>::value && !has_scan<T>::value>::type* = nullptr>
void scan(T& a) {
skip_space();
bool neg = false;
if (cur() == '-') {
neg = true;
next();
}
a = 0;
while ('0' <= cur() && cur() <= '9') {
a = a * 10 + (scan_char() & 15);
}
if (cur() == '.') {
next();
T n = 0, d = 1;
for (int i = 0; '0' <= cur() && cur() <= '9' && i < decimal_precision; ++i) {
n = n * 10 + (scan_char() & 15);
d *= 10;
}
while ('0' <= cur() && cur() <= '9') next();
a += n / d;
}
if (neg) a = -a;
}
private:
template <std::size_t i, typename... Args>
void scan(std::tuple<Args...>& a) {
if constexpr (i < sizeof...(Args)) {
scan(std::get<i>(a));
scan<i + 1, Args...>(a);
}
}
public:
template <typename... Args>
void scan(std::tuple<Args...>& a) {
scan<0, Args...>(a);
}
template <typename T, typename U>
void scan(std::pair<T, U>& a) {
scan(a.first);
scan(a.second);
}
template <typename T, typename std::enable_if<is_range<T>::value && !has_scan<T>::value>::type* = nullptr>
void scan(T& a) {
for (auto& i : a) scan(i);
}
template <typename T, typename std::enable_if<has_scan<T>::value>::type* = nullptr>
void scan(T& a) {
a.scan(*this);
}
void operator()() {}
template <typename Head, typename... Tail>
void operator()(Head& head, Tail&... tail) {
scan(head);
operator()(std::forward<Tail&>(tail)...);
}
template <typename T>
Scanner& operator>>(T& a) {
scan(a);
return *this;
}
explicit operator bool() const { return state; }
friend Scanner& getline(Scanner& sc, std::string& a) {
a.clear();
char c;
if ((c = sc.scan_char()) == '\0' || c == '\n') return sc;
a += c;
while ((c = sc.scan_char()) != '\0' && c != '\n') a += c;
return sc;
}
};
Scanner<> sc;
} // namespace fastio
using fastio::sc;
#line 6 "template/out.hpp"
namespace fastio {
struct Pre {
char buffer[10000][4];
constexpr Pre() : buffer() {
for (int i = 0; i < 10000; ++i) {
int n = i;
for (int j = 3; j >= 0; --j) {
buffer[i][j] = n % 10 | '0';
n /= 10;
}
}
}
} constexpr pre;
template <std::size_t BUFF_SIZE = 1 << 17, bool debug = false>
struct Printer {
private:
template <typename, bool = debug, class = void>
struct has_print : std::false_type {};
template <typename T>
struct has_print<T, false, decltype(std::declval<T>().print(std::declval<Printer&>()), (void)0)> : std::true_type {};
template <typename T>
struct has_print<T, true, decltype(std::declval<T>().debug(std::declval<Printer&>()), (void)0)> : std::true_type {};
int fd;
char buffer[BUFF_SIZE];
int idx;
std::size_t decimal_precision;
public:
Printer() : Printer((debug ? 2 : 1)) {}
explicit Printer(int fd) : fd(fd), idx(0), decimal_precision(16) {}
explicit Printer(FILE* file) : fd(fileno(file)), idx(0), decimal_precision(16) {}
~Printer() {
flush();
}
void set_decimal_precision(std::size_t n) { decimal_precision = n; }
inline void print_char(char c) {
buffer[idx++] = c;
if (idx == BUFF_SIZE) flush();
}
inline void flush() {
idx = write(fd, buffer, idx);
idx = 0;
}
void print(char a) {
if constexpr (debug) print_char('\'');
print_char(a);
if constexpr (debug) print_char('\'');
}
void print(bool a) {
if constexpr (debug) print_char('\'');
print_char('0' + a);
if constexpr (debug) print_char('\'');
}
void print(const char* a) {
if constexpr (debug) print_char('\"');
for (; *a != '\0'; ++a) print_char(*a);
if constexpr (debug) print_char('\"');
}
template <std::size_t N>
void print(const char (&a)[N]) {
if constexpr (debug) print_char('\"');
for (auto i : a) print_char(i);
if constexpr (debug) print_char('\"');
}
void print(const std::string& a) {
if constexpr (debug) print_char('\"');
for (auto i : a) print_char(i);
if constexpr (debug) print_char('\"');
}
template <std::size_t len>
void print(const std::bitset<len>& a) {
for (int i = len - 1; i >= 0; --i) print_char('0' + a[i]);
}
template <typename T, typename std::enable_if<is_int<T>::value && !has_print<T>::value>::type* = nullptr>
void print(T a) {
if (!a) {
print_char('0');
return;
}
if constexpr (is_signed_int<T>::value) {
if (a < 0) {
print_char('-');
a = -a;
}
}
if (static_cast<size_t>(idx + 40) >= BUFF_SIZE) flush();
static char stk[40];
int top = 40;
while (a >= 10000) {
int i = a % 10000;
a /= 10000;
top -= 4;
std::memcpy(stk + top, pre.buffer[i], 4);
}
if (a >= 1000) {
std::memcpy(buffer + idx, pre.buffer[a], 4);
idx += 4;
} else if (a >= 100) {
std::memcpy(buffer + idx, pre.buffer[a] + 1, 3);
idx += 3;
} else if (a >= 10) {
std::memcpy(buffer + idx, pre.buffer[a] + 2, 2);
idx += 2;
} else {
buffer[idx++] = '0' | a;
}
std::memcpy(buffer + idx, stk + top, 40 - top);
idx += 40 - top;
}
template <typename T, typename std::enable_if<std::is_floating_point<T>::value && !has_print<T>::value>::type* = nullptr>
void print(T a) {
if (a == infinity<T>::max || a == infinity<T>::value) {
print("inf");
return;
}
if (a == infinity<T>::min || a == infinity<T>::mvalue) {
print("-inf");
return;
}
if (std::isnan(a)) {
print("nan");
return;
}
if (a < 0) {
print_char('-');
a = -a;
}
T b = a;
if (b < 1) {
print_char('0');
} else {
std::string s;
while (b >= 1) {
s += (char)('0' | (int)std::fmod(b, 10.0));
b /= 10;
}
for (auto i = s.rbegin(); i != s.rend(); ++i) {
print_char(*i);
}
}
print_char('.');
for (std::size_t _ = 0; _ < decimal_precision; ++_) {
a *= 10;
print_char('0' | (int)std::fmod(a, 10.0));
}
}
private:
template <std::size_t i, typename... Args>
void print(const std::tuple<Args...>& a) {
if constexpr (i < sizeof...(Args)) {
if constexpr (debug) print_char(',');
print_char(' ');
print(std::get<i>(a));
print<i + 1>(a);
}
}
public:
template <typename... Args>
void print(const std::tuple<Args...>& a) {
if constexpr (debug) print_char('(');
if constexpr (sizeof...(Args) != 0) {
print(std::get<0>(a));
}
print<1, Args...>(a);
if constexpr (debug) print_char(')');
}
template <typename T, typename U>
void print(const std::pair<T, U>& a) {
if constexpr (debug) print_char('(');
print(a.first);
if constexpr (debug) print_char(',');
print_char(' ');
print(a.second);
if constexpr (debug) print_char(')');
}
template <typename T, typename std::enable_if<is_range<T>::value>::type* = nullptr>
void print(const T& a) {
if constexpr (debug) print_char('{');
auto it = std::begin(a);
if (it != std::end(a)) {
print(*it);
while (++it != std::end(a)) {
if constexpr (debug) print_char(',');
print_char(' ');
print(*it);
}
}
if constexpr (debug) print_char('}');
}
template <typename T, typename std::enable_if<has_print<T>::value && !debug>::type* = nullptr>
void print(const T& a) {
a.print(*this);
}
template <typename T, typename std::enable_if<has_print<T>::value && debug>::type* = nullptr>
void print(const T& a) {
a.debug(*this);
}
void operator()() {}
template <typename Head, typename... Tail>
void operator()(const Head& head, const Tail&... tail) {
print(head);
operator()(std::forward<const Tail&>(tail)...);
}
template <typename T>
Printer& operator<<(const T& a) {
print(a);
return *this;
}
Printer& operator<<(Printer& (*f)(Printer&)) {
return f(*this);
}
};
template <std::size_t BUFF_SIZE, bool debug>
Printer<BUFF_SIZE, debug>& endl(Printer<BUFF_SIZE, debug>& out) {
out.print_char('\n');
out.flush();
return out;
}
template <std::size_t BUFF_SIZE, bool debug>
Printer<BUFF_SIZE, debug>& flush(Printer<BUFF_SIZE, debug>& out) {
out.flush();
return out;
}
Printer<> pr;
Printer<1 << 17, true> prd;
} // namespace fastio
using fastio::endl;
using fastio::flush;
using fastio::pr;
using fastio::prd;
#line 3 "template/func.hpp"
#line 8 "template/func.hpp"
inline constexpr int msb(ull x) {
int res = x ? 0 : -1;
if (x & 0xffffffff00000000) x &= 0xffffffff00000000, res += 32;
if (x & 0xffff0000ffff0000) x &= 0xffff0000ffff0000, res += 16;
if (x & 0xff00ff00ff00ff00) x &= 0xff00ff00ff00ff00, res += 8;
if (x & 0xf0f0f0f0f0f0f0f0) x &= 0xf0f0f0f0f0f0f0f0, res += 4;
if (x & 0xcccccccccccccccc) x &= 0xcccccccccccccccc, res += 2;
return res + (x & 0xaaaaaaaaaaaaaaaa ? 1 : 0);
}
inline constexpr int ceil_log2(ull x) { return x ? msb(x - 1) + 1 : 0; }
inline constexpr ull reverse(ull x) {
x = ((x & 0x5555555555555555) << 1) | ((x & 0xaaaaaaaaaaaaaaaa) >> 1);
x = ((x & 0x3333333333333333) << 2) | ((x & 0xcccccccccccccccc) >> 2);
x = ((x & 0x0f0f0f0f0f0f0f0f) << 4) | ((x & 0xf0f0f0f0f0f0f0f0) >> 4);
x = ((x & 0x00ff00ff00ff00ff) << 8) | ((x & 0xff00ff00ff00ff00) >> 8);
x = ((x & 0x0000ffff0000ffff) << 16) | ((x & 0xffff0000ffff0000) >> 16);
return (x << 32) | (x >> 32);
}
inline constexpr ull reverse(ull x, int len) { return reverse(x) >> (64 - len); }
inline constexpr int popcnt(ull x) {
#if __cplusplus >= 202002L
return std::popcount(x);
#endif
x = (x & 0x5555555555555555) + ((x >> 1) & 0x5555555555555555);
x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);
x = (x & 0x0f0f0f0f0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f0f0f0f0f);
x = (x & 0x00ff00ff00ff00ff) + ((x >> 8) & 0x00ff00ff00ff00ff);
x = (x & 0x0000ffff0000ffff) + ((x >> 16) & 0x0000ffff0000ffff);
return (x & 0x00000000ffffffff) + ((x >> 32) & 0x00000000ffffffff);
}
template <typename T, typename U>
inline constexpr bool chmin(T& a, U b) { return a > b && (a = b, true); }
template <typename T, typename U>
inline constexpr bool chmax(T& a, U b) { return a < b && (a = b, true); }
inline constexpr ll gcd(ll a, ll b) {
if (a < 0) a = -a;
if (b < 0) b = -b;
while (b) {
const ll c = b;
b = a % b;
a = c;
}
return a;
}
inline constexpr ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
inline constexpr bool is_prime(ll n) {
if (n <= 1) return false;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
inline constexpr ll my_pow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1) res *= a;
a *= a;
b >>= 1;
}
return res;
}
inline constexpr ll mod_pow(ll a, ll b, const ll& mod) {
if (mod == 1) return 0;
a %= mod;
ll res = 1;
while (b) {
if (b & 1) (res *= a) %= mod;
(a *= a) %= mod;
b >>= 1;
}
return res;
}
inline ll mod_inv(ll a, const ll& mod) {
ll b = mod, x = 1, u = 0, t;
while (b) {
t = a / b;
std::swap(a -= t * b, b);
std::swap(x -= t * u, u);
}
if (x < 0) x += mod;
return x;
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {
os << p.first << " " << p.second;
return os;
}
template <typename T, typename U>
std::istream& operator>>(std::istream& is, std::pair<T, U>& p) {
is >> p.first >> p.second;
return is;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
for (auto it = std::begin(v); it != std::end(v);) {
os << *it << ((++it) != std::end(v) ? " " : "");
}
return os;
}
template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
for (T& in : v) {
is >> in;
}
return is;
}
inline void scan() {}
template <class Head, class... Tail>
inline void scan(Head& head, Tail&... tail) {
sc >> head;
scan(tail...);
}
template <class T>
inline void print(const T& t) { pr << t << '\n'; }
template <class Head, class... Tail>
inline void print(const Head& head, const Tail&... tail) {
pr << head << ' ';
print(tail...);
}
template <class... T>
inline void fin(const T&... a) {
print(a...);
exit(0);
}
template <typename T>
inline void dump(const T& a) { prd << a; }
inline void trace() { prd << endl; }
template <typename Head, typename... Tail>
inline void trace(const Head& head, const Tail&... tail) {
dump(head);
if (sizeof...(tail)) prd.print_char(','), prd.print_char(' ');
trace(tail...);
}
#ifdef ONLINE_JUDGE
#define dbg(...) (void(0))
#else
#define dbg(...) \
do { \
prd << #__VA_ARGS__; \
prd.print_char(' '), prd.print_char('='), prd.print_char(' '); \
trace(__VA_ARGS__); \
} while (0)
#endif
#line 3 "template/util.hpp"
#line 6 "template/util.hpp"
template <typename F>
struct REC {
private:
F f;
public:
explicit constexpr REC(F&& f_) : f(std::forward<F>(f_)) {}
template <typename... Args>
constexpr auto operator()(Args&&... args) const {
return f(*this, std::forward<Args>(args)...);
}
};
template <typename T, typename Comp = std::less<T>>
struct compressor {
private:
std::vector<T> data;
Comp cmp;
bool sorted = false;
public:
compressor() : compressor(Comp()) {}
compressor(const Comp& cmp) : cmp(cmp) {}
compressor(const std::vector<T>& dat, const Comp& cmp = Comp()) : data(dat), cmp(cmp) {}
compressor(std::vector<T>&& dat, const Comp& cmp = Comp()) : data(std::move(dat)), cmp(cmp) {}
compressor(std::initializer_list<T> li, const Comp& cmp = Comp()) : data(li.begin(), li.end()), cmp(cmp) {}
void push_back(const T& v) {
assert(!sorted);
data.push_back(v);
}
void push_back(T&& v) {
assert(!sorted);
data.push_back(std::move(v));
}
template <typename... Args>
void emplace_back(Args&&... args) {
assert(!sorted);
data.emplace_back(std::forward<Args>(args)...);
}
void push(const std::vector<T>& v) {
assert(!sorted);
const int n = data.size();
data.resize(v.size() + n);
for (int i = 0; i < (int)v.size(); i++) data[i + n] = v[i];
}
void build() {
assert(!sorted);
sorted = 1;
std::sort(data.begin(), data.end(), cmp);
data.erase(unique(data.begin(), data.end(), [&](const T& l, const T& r) -> bool { return !cmp(l, r) && !cmp(r, l); }), data.end());
}
const T& operator[](int k) const& {
assert(sorted);
return data[k];
}
int get_index(const T& v) const {
assert(sorted);
return int(lower_bound(data.begin(), data.end(), v, cmp) - data.begin());
}
void press(std::vector<T>& v) const {
assert(sorted);
for (auto&& i : v) i = get_index(i);
}
std::vector<int> pressed(const std::vector<T>& v) const {
assert(sorted);
std::vector<int> ret(v.size());
for (int i = 0; i < (int)v.size(); i++) ret[i] = get_index(v[i]);
return ret;
}
int size() const {
assert(sorted);
return data.size();
}
};
#line 11 "template/template.hpp"
using namespace std;
#line 3 "math/modular/modint.hpp"
namespace internal {
struct modint_base {};
} // namespace internal
template <typename T>
using is_modint = is_base_of<internal::modint_base, T>;
template <typename T, T mod>
struct StaticModInt : internal::modint_base {
static_assert(is_integral<T>::value, "T must be integral");
static_assert(is_unsigned<T>::value, "T must be unsgined");
static_assert(mod > 0, "mod must be positive");
static_assert(mod <= INF<T>, "mod*2 must be less than or equal to T::max()");
private:
using large_t = typename double_size_uint<T>::type;
using signed_t = typename make_signed<T>::type;
T val;
public:
constexpr StaticModInt() : val(0) {}
template <typename U, typename enable_if<is_integral<U>::value && is_unsigned<U>::value>::type* = nullptr>
constexpr StaticModInt(U x) : val(x % mod) {}
template <typename U, typename enable_if<is_integral<U>::value && is_signed<U>::value>::type* = nullptr>
constexpr StaticModInt(U x) : val{} {
x %= static_cast<signed_t>(mod);
if (x < 0) x += static_cast<signed_t>(mod);
val = static_cast<T>(x);
}
constexpr T get() const { return val; }
static constexpr T get_mod() { return mod; }
static constexpr StaticModInt raw(T v) {
StaticModInt res;
res.val = v;
return res;
}
constexpr StaticModInt inv() const {
return mod_inv(val, mod);
}
constexpr StaticModInt& operator++() {
++val;
if (val == mod) val = 0;
return *this;
}
constexpr StaticModInt operator++(int) {
StaticModInt res = *this;
++*this;
return res;
}
constexpr StaticModInt& operator--() {
if (val == 0) val = mod;
--val;
return *this;
}
constexpr StaticModInt operator--(int) {
StaticModInt res = *this;
--*this;
return res;
}
constexpr StaticModInt& operator+=(const StaticModInt& x) {
val += x.val;
if (val >= mod) val -= mod;
return *this;
}
constexpr StaticModInt& operator-=(const StaticModInt& x) {
if (val < x.val) val += mod;
val -= x.val;
return *this;
}
constexpr StaticModInt& operator*=(const StaticModInt& x) {
val = static_cast<T>((static_cast<large_t>(val) * x.val) % mod);
return *this;
}
constexpr StaticModInt& operator/=(const StaticModInt& x) {
return *this *= x.inv();
}
friend constexpr StaticModInt operator+(const StaticModInt& l, const StaticModInt& r) { return StaticModInt(l) += r; }
friend constexpr StaticModInt operator-(const StaticModInt& l, const StaticModInt& r) { return StaticModInt(l) -= r; }
friend constexpr StaticModInt operator*(const StaticModInt& l, const StaticModInt& r) { return StaticModInt(l) *= r; }
friend constexpr StaticModInt operator/(const StaticModInt& l, const StaticModInt& r) { return StaticModInt(l) /= r; }
constexpr StaticModInt operator+() const { return StaticModInt(*this); }
constexpr StaticModInt operator-() const { return StaticModInt() - *this; }
friend constexpr bool operator==(const StaticModInt& l, const StaticModInt& r) { return l.val == r.val; }
friend constexpr bool operator!=(const StaticModInt& l, const StaticModInt& r) { return l.val != r.val; }
constexpr StaticModInt pow(ll a) const {
StaticModInt v = *this, res = 1;
while (a) {
if (a & 1) res *= v;
v *= v;
a >>= 1;
}
return res;
}
template <typename Sc>
void scan(Sc& a) {
ll x;
a.scan(x);
*this = x;
}
template <typename Pr>
void print(Pr& a) const {
a.print(val);
}
template <typename Pr>
void debug(Pr& a) const {
a.print(val);
}
};
template <unsigned int p>
using ModInt = StaticModInt<unsigned int, p>;
template <typename T, int id>
struct DynamicModInt {
static_assert(is_integral<T>::value, "T must be integral");
static_assert(is_unsigned<T>::value, "T must be unsigned");
private:
using large_t = typename double_size_uint<T>::type;
using signed_t = typename make_signed<T>::type;
T val;
static T mod;
public:
constexpr DynamicModInt() : val(0) {}
template <typename U, typename enable_if<is_integral<U>::value && is_unsigned<U>::value>::type* = nullptr>
constexpr DynamicModInt(U x) : val(x % mod) {}
template <typename U, typename enable_if<is_integral<U>::value && is_signed<U>::value>::type* = nullptr>
constexpr DynamicModInt(U x) : val{} {
x %= static_cast<signed_t>(mod);
if (x < 0) x += static_cast<signed_t>(mod);
val = static_cast<T>(x);
}
T get() const { return val; }
static T get_mod() { return mod; }
static void set_mod(T x) {
mod = x;
assert(mod > 0);
assert(mod <= INF<T>);
}
static DynamicModInt raw(T v) {
DynamicModInt res;
res.val = v;
return res;
}
DynamicModInt inv() const {
return mod_inv(val, mod);
}
DynamicModInt& operator++() {
++val;
if (val == mod) val = 0;
return *this;
}
DynamicModInt operator++(int) {
DynamicModInt res = *this;
++*this;
return res;
}
DynamicModInt& operator--() {
if (val == 0) val = mod;
--val;
return *this;
}
DynamicModInt operator--(int) {
DynamicModInt res = *this;
--*this;
return res;
}
DynamicModInt& operator+=(const DynamicModInt& x) {
val += x.val;
if (val >= mod) val -= mod;
return *this;
}
DynamicModInt& operator-=(const DynamicModInt& x) {
if (val < x.val) val += mod;
val -= x.val;
return *this;
}
DynamicModInt& operator*=(const DynamicModInt& x) {
val = static_cast<T>((static_cast<large_t>(val) * x.val) % mod);
return *this;
}
DynamicModInt& operator/=(const DynamicModInt& x) {
return *this *= x.inv();
}
friend DynamicModInt operator+(const DynamicModInt& l, const DynamicModInt& r) { return DynamicModInt(l) += r; }
friend DynamicModInt operator-(const DynamicModInt& l, const DynamicModInt& r) { return DynamicModInt(l) -= r; }
friend DynamicModInt operator*(const DynamicModInt& l, const DynamicModInt& r) { return DynamicModInt(l) *= r; }
friend DynamicModInt operator/(const DynamicModInt& l, const DynamicModInt& r) { return DynamicModInt(l) /= r; }
DynamicModInt operator+() const { return DynamicModInt(*this); }
DynamicModInt operator-() const { return DynamicModInt() - *this; }
friend bool operator==(const DynamicModInt& l, const DynamicModInt& r) { return l.val == r.val; }
friend bool operator!=(const DynamicModInt& l, const DynamicModInt& r) { return l.val != r.val; }
DynamicModInt pow(ll a) const {
DynamicModInt v = *this, res = 1;
while (a) {
if (a & 1) res *= v;
v *= v;
a >>= 1;
}
return res;
}
template <typename Sc>
void scan(Sc& a) {
ll x;
a.scan(x);
*this = x;
}
template <typename Pr>
void print(Pr& a) const {
a.print(val);
}
template <typename Pr>
void debug(Pr& a) const {
a.print(val);
}
};
template <typename T, int id>
T DynamicModInt<T, id>::mod = 998244353;
template <int id>
using dynamic_modint = DynamicModInt<unsigned int, id>;
using modint = dynamic_modint<-1>;
/**
* @brief ModInt
*/
#line 4 "math/modular/montgomery-modint.hpp"
template <typename T>
struct MontgomeryReduction {
static_assert(is_integral<T>::value, "template argument must be integral");
static_assert(is_unsigned<T>::value, "template argument must be unsigned");
private:
using large_t = typename double_size_uint<T>::type;
static constexpr int lg = numeric_limits<T>::digits;
T mod;
T r;
T r2;
T minv;
T calc_inv() const {
T t = 0, res = 0;
rep(i, lg) {
if (~t & 1) {
t += mod;
res += static_cast<T>(1) << i;
}
t >>= 1;
}
return res;
}
public:
MontgomeryReduction(T x) { set_mod(x); }
static constexpr int get_lg() { return lg; }
void set_mod(T x) {
assert(x > 0);
assert(x & 1);
assert(x <= INF<T>);
mod = x;
r = (-static_cast<T>(mod)) % mod;
r2 = (-static_cast<large_t>(mod)) % mod;
minv = calc_inv();
}
inline T get_r() const { return r; }
inline T get_mod() const { return mod; }
T reduce(large_t x) const {
large_t tmp = (x + static_cast<large_t>(static_cast<T>(x) * minv) * mod) >> lg;
return tmp >= mod ? tmp - mod : tmp;
}
T transform(large_t x) const { return reduce(x * r2); }
};
template <typename T, int id>
struct MontgomeryModInt : internal::modint_base {
static_assert(is_integral<T>::value, "template argument must be integral");
static_assert(is_unsigned<T>::value, "template argument must be unsigned");
private:
using large_t = typename double_size_uint<T>::type;
T val;
static MontgomeryReduction<T> reduction;
public:
MontgomeryModInt() : val(0) {}
template <typename U, typename enable_if<is_integral<U>::value && is_unsigned<U>::value>::type* = nullptr>
MontgomeryModInt(U x) : val(reduction.transform(x < (static_cast<large_t>(reduction.get_mod()) << reduction.get_lg()) ? static_cast<large_t>(x) : static_cast<large_t>(x % reduction.get_mod()))) {}
template <typename U, typename enable_if<is_integral<U>::value && is_signed<U>::value>::type* = nullptr>
MontgomeryModInt(U x) : MontgomeryModInt(static_cast<typename std::make_unsigned<U>::type>(x < 0 ? -x : x)) {
if (x < 0 && val) val = reduction.get_mod() - val;
}
T get() const { return reduction.reduce(val); }
static T get_mod() { return reduction.get_mod(); }
static void set_mod(T x) { reduction.set_mod(x); }
MontgomeryModInt& operator++() {
val += reduction.get_r();
if (val >= reduction.get_mod()) val -= reduction.get_mod();
return *this;
}
MontgomeryModInt operator++(int) {
MontgomeryModInt res = *this;
++*this;
return res;
}
MontgomeryModInt& operator--() {
if (val < reduction.get_r()) val += reduction.get_mod();
val -= reduction.get_r();
return *this;
}
MontgomeryModInt operator--(int) {
MontgomeryModInt res = *this;
--*this;
return res;
}
MontgomeryModInt& operator+=(const MontgomeryModInt& r) {
val += r.val;
if (val >= reduction.get_mod()) val -= reduction.get_mod();
return *this;
}
MontgomeryModInt& operator-=(const MontgomeryModInt& r) {
if (val < r.val) val += reduction.get_mod();
val -= r.val;
return *this;
}
MontgomeryModInt& operator*=(const MontgomeryModInt& r) {
val = reduction.reduce(static_cast<large_t>(val) * r.val);
return *this;
}
MontgomeryModInt pow(ull n) const {
MontgomeryModInt res = 1, tmp = *this;
while (n) {
if (n & 1) res *= tmp;
tmp *= tmp;
n >>= 1;
}
return res;
}
MontgomeryModInt inv() const { return pow(reduction.get_mod() - 2); }
MontgomeryModInt& operator/=(const MontgomeryModInt& r) { return *this *= r.inv(); }
friend MontgomeryModInt operator+(const MontgomeryModInt& l, const MontgomeryModInt& r) { return MontgomeryModInt(l) += r; }
friend MontgomeryModInt operator-(const MontgomeryModInt& l, const MontgomeryModInt& r) { return MontgomeryModInt(l) -= r; }
friend MontgomeryModInt operator*(const MontgomeryModInt& l, const MontgomeryModInt& r) { return MontgomeryModInt(l) *= r; }
friend MontgomeryModInt operator/(const MontgomeryModInt& l, const MontgomeryModInt& r) { return MontgomeryModInt(l) /= r; }
friend bool operator==(const MontgomeryModInt& l, const MontgomeryModInt& r) { return l.val == r.val; }
friend bool operator!=(const MontgomeryModInt& l, const MontgomeryModInt& r) { return l.val != r.val; }
template <typename Sc>
void scan(Sc& a) {
ll x;
a.scan(x);
*this = x;
}
template <typename Pr>
void print(Pr& a) const {
a.print(get());
}
template <typename Pr>
void debug(Pr& a) const {
a.print(get());
}
};
template <typename T, int id>
MontgomeryReduction<T>
MontgomeryModInt<T, id>::reduction = MontgomeryReduction<T>(998244353);
using ArbitraryModInt = MontgomeryModInt<unsigned int, -1>;
/**
* @brief MontgomeryModInt(モンゴメリ乗算)
*/
#line 4 "math/number/miller-rabin.hpp"
template <typename T>
constexpr bool miller_rabin(ull n, const ull base[], int sz) {
if (T::get_mod() != n) T::set_mod(n);
ull d = n - 1;
while (~d & 1) d >>= 1;
const T e1 = 1, e2 = n - 1;
rep(i, sz) {
ull a = base[i];
if (n <= a) return true;
ull t = d;
T y = T(a).pow(t);
while (t != n - 1 && y != e1 && y != e2) {
y *= y;
t <<= 1;
}
if (y != e2 && (~t & 1)) return false;
}
return true;
}
constexpr bool is_prime_fast(ull n) {
constexpr ull base_int[3] = {2, 7, 61}, base_ll[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
if (n == 2) return true;
if (n < 2 || n % 2 == 0) return false;
if (n < (1u << 31)) return miller_rabin<MontgomeryModInt<unsigned int, -2>>(n, base_int, 3);
return miller_rabin<MontgomeryModInt<ull, -2>>(n, base_ll, 7);
}
template <ull n>
constexpr bool is_prime_v = is_prime(n);
/**
* @brief Miller-Rabin Primality Test(ミラーラビン素数判定)
*/
#line 3 "others/random.hpp"
template <typename Engine>
struct Random {
private:
Engine rnd;
public:
using result_type = typename Engine::result_type;
Random() : Random(random_device{}()) {}
Random(result_type seed) : rnd(seed) {}
result_type operator()() { return rnd(); }
template <typename IntType = ll>
IntType uniform(IntType l, IntType r) {
static_assert(is_integral<IntType>::value, "template argument must be an integral type");
return uniform_int_distribution<IntType>{l, r}(rnd);
}
template <typename RealType = double>
RealType uniform_real(RealType l, RealType r) {
static_assert(is_floating_point<RealType>::value, "template argument must be a floating point type");
return uniform_real_distribution<RealType>{l, r}(rnd);
}
bool uniform_bool() { return uniform<int>(0, 1); }
template <typename T = ll>
pair<T, T> uniform_pair(T l, T r) {
T a, b;
do {
a = uniform<T>(l, r);
b = uniform<T>(l, r);
} while (a == b);
if (a > b) swap(a, b);
return {a, b};
}
template <typename Iter>
void shuffle(const Iter& first, const Iter& last) {
std::shuffle(first, last, rnd);
}
template <class T>
vector<T> permutalion(T n) {
static_assert(is_integral<T>::value, "template argument must be an integral type");
vector<T> res(n);
iota(res.begin(), res.end(), T());
shuffle(all(res));
return res;
}
};
using Random32 = Random<mt19937>;
using Random64 = Random<mt19937_64>;
Random32 rand32;
Random64 rand64;
/**
* @brief Random(乱数)
*/
#line 3 "string/run-length.hpp"
template <typename Cont, typename Comp>
vector<pair<typename Cont::value_type, int>> run_length(const Cont& c, const Comp& cmp) {
vector<pair<typename Cont::value_type, int>> ret;
if (c.empty()) return ret;
ret.emplace_back(c.front(), 1);
for (int i = 1; i < (int)c.size(); i++) {
if (cmp(c[i], ret.back().first)) {
ret.back().second++;
} else {
ret.emplace_back(c[i], 1);
}
}
return ret;
}
template <typename Cont>
vector<pair<typename Cont::value_type, int>> run_length(const Cont& c) { return run_length(c, equal_to<typename Cont::value_type>()); }
#line 7 "math/number/pollard-rho.hpp"
template <typename T, typename Rand>
ull pollard_rho(ull n, Rand& rand) {
if (~n & 1) return 2;
if (T::get_mod() != n) T::set_mod(n);
T c, e = 1;
auto f = [&](T x) -> T { return x * x + c; };
constexpr int m = 128;
while (1) {
c = rand.uniform(1ull, n - 1);
T x = rand.uniform(2ull, n - 1), y = x;
ull g = 1;
while (g == 1) {
T p = e, tx = x, ty = y;
rep(i, m) {
x = f(x);
y = f(f(y));
p *= x - y;
}
g = gcd(p.get(), n);
if (g == 1) continue;
rep(i, m) {
tx = f(tx);
ty = f(f(ty));
g = gcd((tx - ty).get(), n);
if (g != 1) {
if (g != n) return g;
break;
}
}
}
}
return -1;
}
template <typename T = MontgomeryModInt<ull, -3>, typename Rand = Random64>
vector<ull> factorize(ull n, Rand& rand = rand64) {
if (n == 1) return {};
vector<ull> res;
vector<ull> st = {n};
while (!st.empty()) {
ull t = st.back();
st.pop_back();
if (t == 1) continue;
if (is_prime_fast(t)) {
res.push_back(t);
continue;
}
ull p = pollard_rho<T>(t, rand);
st.push_back(p);
st.push_back(t / p);
}
sort(all(res));
return res;
}
template <typename T = MontgomeryModInt<ull, -3>, typename Rand = Random64>
vector<pair<ull, int>> expfactorize(ull n, Rand& rand = rand64) {
auto res = factorize<T>(n, rand);
return run_length(res);
}
/**
* @brief Pollard's Rho Factorization(ポラード・ロー法)
*/
#line 6 "math/number/primitive-root.hpp"
template <typename T = MontgomeryModInt<ull, -4>, typename Rand = Random64>
ull primitive_root(ull n, Rand rand = rand64) {
assert(is_prime_fast(n));
if (n == 2) return 1;
if (T::get_mod() != n) T::set_mod(n);
auto divs = factorize(n - 1);
divs.erase(unique(divs.begin(), divs.end()), divs.end());
for (auto& x : divs) x = (n - 1) / x;
const T e = 1;
while (1) {
ull g = rand.uniform(2ull, n - 1);
bool ok = 1;
for (auto x : divs) {
if (T(g).pow(x) == e) {
ok = false;
break;
}
}
if (ok) return g;
}
}
template <ull p, enable_if_t<is_prime_v<p>>* = nullptr>
constexpr ull constexpr_primitive_root() {
if (p == 2) return 1;
if (p == 167772161) return 3;
if (p == 469762049) return 3;
if (p == 754974721) return 11;
if (p == 998244353) return 3;
if (p == 1224736769) return 3;
if (p == 1811939329) return 11;
if (p == 2013265921) return 11;
rep(g, 2, p) {
if (mod_pow(g, (p - 1) >> 1, p) != 1) return g;
}
return -1;
}
/**
* @brief Primitive Root(原始根)
*/
#line 6 "math/convolution/convolution.hpp"
template <unsigned int p>
struct NthRoot {
private:
static constexpr unsigned int lg = msb((p - 1) & (1 - p));
public:
array<ModInt<p>, lg + 1> root, inv_root;
array<ModInt<p>, max(0u, lg - 1)> rate2, irate2;
array<ModInt<p>, max(0u, lg - 2)> rate3, irate3;
constexpr NthRoot() : root{}, inv_root{} {
root[lg] = mod_pow(constexpr_primitive_root<p>(), (p - 1) >> lg, p);
inv_root[lg] = root[lg].pow(p - 2);
;
rrep(i, lg) {
root[i] = root[i + 1] * root[i + 1];
inv_root[i] = inv_root[i + 1] * inv_root[i + 1];
}
{
ModInt<p> prod = 1, iprod = 1;
rep(i, lg - 1) {
rate2[i] = root[i + 2] * prod;
irate2[i] = inv_root[i + 2] * iprod;
prod *= inv_root[i + 2];
iprod *= root[i + 2];
}
}
{
ModInt<p> prod = 1, iprod = 1;
rep(i, lg - 2) {
rate3[i] = root[i + 3] * prod;
irate3[i] = inv_root[i + 3] * iprod;
prod *= inv_root[i + 3];
iprod *= root[i + 3];
}
}
}
static constexpr unsigned int get_lg() { return lg; }
};
template <unsigned int p>
constexpr NthRoot<p> nth_root;
template <typename T, enable_if_t<is_modint<T>::value>* = nullptr>
void ntt(vector<T>& a) {
constexpr unsigned int p = T::get_mod();
const int sz = a.size();
assert((unsigned int)sz <= ((1 - p) & (p - 1)));
assert((sz & (sz - 1)) == 0);
const int lg = msb(sz);
static constexpr T im = nth_root<p>.root[2];
for (int i = lg; i >= 1; i -= 2) {
if (i == 1) {
T z = 1;
for (int j = 0; j < sz; j += (1 << i)) {
for (int k = j; k < j + (1 << (i - 1)); ++k) {
const T x = a[k], y = a[k + (1 << (i - 1))] * z;
a[k] = x + y, a[k + (1 << (i - 1))] = x - y;
}
if (j + (1 << i) != sz) z *= nth_root<p>.rate2[__builtin_ctz(~(unsigned int)(j >> i))];
}
} else {
const int offset = 1 << (i - 2);
T z = 1;
for (int j = 0; j < sz; j += (1 << i)) {
for (int k = j; k < j + (1 << (i - 2)); ++k) {
const T z2 = z * z, z3 = z2 * z;
const T c0 = a[k], c1 = a[k + offset] * z, c2 = a[k + offset * 2] * z2, c3 = a[k + offset * 3] * z3;
const T c0c2 = c0 + c2, c0mc2 = c0 - c2, c1c3 = c1 + c3, c1mc3im = (c1 - c3) * im;
a[k] = c0c2 + c1c3;
a[k + offset] = c0c2 - c1c3;
a[k + offset * 2] = c0mc2 + c1mc3im;
a[k + offset * 3] = c0mc2 - c1mc3im;
}
if (j + (1 << i) != sz) z *= nth_root<p>.rate3[__builtin_ctz(~(unsigned int)(j >> i))];
}
}
}
}
template <typename T, enable_if_t<is_modint<T>::value>* = nullptr>
void intt(vector<T>& a, const bool& f = true) {
constexpr unsigned int p = T::get_mod();
const int sz = a.size();
assert((unsigned int)sz <= ((1 - p) & (p - 1)));
assert((sz & (sz - 1)) == 0);
const int lg = msb(sz);
static constexpr T im = nth_root<p>.inv_root[2];
for (int i = 2 - (lg & 1); i <= lg; i += 2) {
if (i == 1) {
T z = 1;
for (int j = 0; j < sz; j += (1 << i)) {
for (int k = j; k < j + (1 << (i - 1)); ++k) {
const T x = a[k], y = a[k + (1u << (i - 1))];
a[k] = x + y, a[k + (1u << (i - 1))] = (x - y) * z;
}
if (j + (1 << i) != sz) z *= nth_root<p>.irate2[__builtin_ctz(~(unsigned int)(j >> i))];
}
} else {
const int offset = 1 << (i - 2);
T z = 1;
for (int j = 0; j < sz; j += (1 << i)) {
for (int k = j; k < j + (1 << (i - 2)); ++k) {
const T z2 = z * z, z3 = z2 * z;
const T c0 = a[k], c1 = a[k + offset], c2 = a[k + offset * 2], c3 = a[k + offset * 3];
const T c0c1 = c0 + c1, c0mc1 = c0 - c1, c2c3 = c2 + c3, c2mc3im = (c2 - c3) * im;
a[k] = c0c1 + c2c3;
a[k + offset] = (c0mc1 + c2mc3im) * z;
a[k + offset * 2] = (c0c1 - c2c3) * z2;
a[k + offset * 3] = (c0mc1 - c2mc3im) * z3;
}
if (j + (1 << i) != sz) z *= nth_root<p>.irate3[__builtin_ctz(~(unsigned int)(j >> i))];
}
}
}
if (f) {
const T inv_sz = T(1) / sz;
for (auto& x : a) x *= inv_sz;
}
}
template <typename T>
vector<T> convolution_naive(const vector<T>& a, const vector<T>& b) {
const int sz1 = a.size(), sz2 = b.size();
vector<T> c(sz1 + sz2 - 1);
rep(i, sz1) rep(j, sz2) c[i + j] += a[i] * b[j];
return c;
}
template <unsigned int p>
vector<ModInt<p>> convolution_for_any_mod(const vector<ModInt<p>>& a, const vector<ModInt<p>>& b);
template <typename T, enable_if_t<is_modint<T>::value>* = nullptr>
vector<T> convole(vector<T> a, vector<T> b) {
const int n = a.size() + b.size() - 1;
const int lg = ceil_log2(n);
const int sz = 1 << lg;
a.resize(sz), b.resize(sz);
ntt(a), ntt(b);
rep(i, sz) a[i] *= b[i];
intt(a);
a.resize(n);
return a;
}
template <typename T, enable_if_t<is_modint<T>::value>* = nullptr>
vector<T> convolution(const vector<T>& a, const vector<T>& b) {
constexpr unsigned int p = T::get_mod();
const unsigned int sz1 = a.size(), sz2 = b.size();
if (sz1 == 0 || sz2 == 0) return {};
if (sz1 <= 64 || sz2 <= 64) return convolution_naive(a, b);
if constexpr (((p - 1) & (1 - p)) >= 128) {
if (sz1 + sz2 - 1 <= ((p - 1) & (1 - p))) return convole(a, b);
}
return convolution_for_any_mod(a, b);
}
template <unsigned int p = 998244353>
vector<ll> convolution(const vector<ll>& a, const vector<ll>& b) {
const int sz1 = a.size(), sz2 = b.size();
vector<ModInt<p>> a1(sz1), b1(sz2);
rep(i, sz1) a1[i] = a[i];
rep(i, sz2) b1[i] = b[i];
auto c1 = convolution(a1, b1);
vector<ll> c(sz1 + sz2 - 1);
rep(i, sz1 + sz2 - 1) c[i] = c1[i].get();
return c;
}
template <unsigned int p>
vector<ModInt<p>> convolution_for_any_mod(const vector<ModInt<p>>& a, const vector<ModInt<p>>& b) {
const int sz1 = a.size(), sz2 = b.size();
assert(sz1 + sz2 - 1 <= (1 << 26));
vector<ll> a1(sz1), b1(sz2);
rep(i, sz1) a1[i] = a[i].get();
rep(i, sz2) b1[i] = b[i].get();
static constexpr ull MOD1 = 469762049;
static constexpr ull MOD2 = 1811939329;
static constexpr ull MOD3 = 2013265921;
static constexpr ull INV1_2 = mod_pow(MOD1, MOD2 - 2, MOD2);
static constexpr ull INV1_3 = mod_pow(MOD1, MOD3 - 2, MOD3);
static constexpr ull INV2_3 = mod_pow(MOD2, MOD3 - 2, MOD3);
auto c1 = convolution<MOD1>(a1, b1);
auto c2 = convolution<MOD2>(a1, b1);
auto c3 = convolution<MOD3>(a1, b1);
vector<ModInt<p>> c(sz1 + sz2 - 1);
rep(i, sz1 + sz2 - 1) {
ull x1 = c1[i];
ull x2 = (c2[i] - x1 + MOD2) * INV1_2 % MOD2;
ull x3 = ((c3[i] - x1 + MOD3) * INV1_3 % MOD3 - x2 + MOD3) * INV2_3 % MOD3;
c[i] = ModInt<p>(x1 + (x2 + x3 * MOD2) % p * MOD1);
}
return c;
}
/**
* @brief Convolution(畳み込み)
*/
#line 4 "math/others/combinatorics.hpp"
template <typename T>
struct Combinatorics {
private:
static vector<T> dat, idat;
public:
static void extend(int sz) {
const int pre_sz = dat.size();
if (sz < pre_sz) return;
dat.resize(sz + 1, 1);
idat.resize(sz + 1, 1);
for (int i = pre_sz; i <= sz; i++) dat[i] = dat[i - 1] * i;
idat[sz] = T(1) / dat[sz];
for (int i = sz - 1; i >= pre_sz; i--) idat[i] = idat[i + 1] * (i + 1);
}
static T fac(ll n) {
if (n < 0) return T();
extend(n);
return dat[n];
}
static T finv(ll n) {
if (n < 0) return T();
extend(n);
return idat[n];
}
static T inv(ll n) {
if (n <= 0) return T();
extend(n);
return dat[n - 1] * idat[n];
}
static T com(ll n, ll k) {
if (k < 0 || n < k) return T();
extend(n);
return dat[n] * idat[k] * idat[n - k];
}
static T hom(ll n, ll k) {
if (n < 0 || k < 0) return T();
return k == 0 ? 1 : com(n + k - 1, k);
}
static inline T per(ll n, ll k) {
if (k < 0 || n < k) return T();
extend(n);
return dat[n] * idat[n - k];
}
};
template <typename T>
vector<T> Combinatorics<T>::dat = vector<T>(2, 1);
template <typename T>
vector<T> Combinatorics<T>::idat = vector<T>(2, 1);
template <long long p>
struct COMB {
private:
static vector<vector<ModInt<p>>> comb;
static void init() {
if (!comb.empty()) return;
comb.assign(p, vector<ModInt<p>>(p));
comb[0][0] = 1;
for (int i = 1; i < p; i++) {
comb[i][0] = 1;
for (int j = i; j > 0; j--) comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];
}
}
public:
COMB() {
init();
}
ModInt<p> com(int n, int k) {
init();
ModInt<p> ret = 1;
while (n > 0 || k > 0) {
int ni = n % p, ki = k % p;
ret *= comb[ni][ki];
n /= p;
k /= p;
}
return ret;
}
};
template <long long p>
vector<vector<ModInt<p>>> COMB<p>::comb = vector<vector<ModInt<p>>>();
/**
* @brief Combinatorics(組み合わせ)
*/
#line 5 "math/fps/fps.hpp"
template <typename mint = ModInt<998244353>>
struct FormalPowerSeries : vector<mint> {
using vector<mint>::vector;
using FPS = FormalPowerSeries<mint>;
using Comb = Combinatorics<mint>;
private:
static constexpr unsigned int p = mint::get_mod();
public:
FormalPowerSeries() : vector<mint>() {}
FormalPowerSeries(const vector<mint>& v) : vector<mint>(v) {}
FormalPowerSeries(vector<mint>&& v) : vector<mint>(move(v)) {}
inline void shrink() {
while (!(*this).empty() && (*this).back() == mint()) (*this).pop_back();
}
FPS& dot(const FPS& r) {
rep(i, min((*this).size(), r.size()))(*this)[i] *= r[i];
return *this;
}
FPS inv(int d = -1) const {
const int n = (*this).size();
if (d == -1) d = n;
FPS res(d);
res[0] = (*this)[0].inv();
for (int sz = 1; sz < d; sz <<= 1) {
FPS f((*this).begin(), (*this).begin() + min(n, 2 * sz));
FPS g(res.begin(), res.begin() + sz);
f.resize(2 * sz), g.resize(2 * sz);
ntt(f), ntt(g);
f.dot(g);
intt(f);
rep(i, sz) f[i] = 0;
ntt(f);
f.dot(g);
intt(f);
rep(j, sz, min(2 * sz, d)) res[j] = -f[j];
}
return res;
}
FPS operator+() const { return *this; }
FPS operator-() const {
FPS res(*this);
for (auto& x : res) x = -x;
return res;
}
FPS& operator+=(const mint& r) {
shrink();
if ((*this).empty()) (*this).resize(1);
(*this)[0] += r;
return *this;
}
FPS& operator-=(const mint& r) {
shrink();
if ((*this).empty()) (*this).resize(1);
(*this)[0] -= r;
return *this;
}
FPS& operator*=(const mint& r) {
shrink();
for (auto& x : *this) x *= r;
return *this;
}
FPS& operator/=(const mint& r) {
shrink()(*this) *= r.inv();
return *this;
}
FPS& operator+=(const FPS& r) {
shrink();
if ((*this).size() < r.size()) (*this).resize(r.size());
rep(i, r.size())(*this)[i] += r[i];
return *this;
}
FPS& operator-=(const FPS& r) {
shrink();
if ((*this).size() < r.size()) (*this).resize(r.size());
rep(i, r.size())(*this)[i] -= r[i];
return *this;
}
FPS& operator*=(const FPS& r) {
shrink();
auto ret = convolution(*this, r);
(*this) = {ret.begin(), ret.end()};
return *this;
}
FPS& operator/=(FPS r) {
shrink();
const int n = (*this).size(), m = r.size();
if (n < m) {
(*this).clear();
return *this;
}
const int d = n - m + 1;
reverse((*this).begin(), (*this).end());
reverse(r.begin(), r.end());
(*this).resize(d);
(*this) *= r.inv(d);
(*this).resize(d);
reverse((*this).begin(), (*this).end());
return *this;
}
FPS& operator%=(const FPS& r) {
shrink();
const int n = (*this).size(), m = r.size();
if (n < m) return *this;
(*this) -= (*this) / r * r;
shrink();
return *this;
}
FPS& operator<<=(ll k) {
shrink();
(*this).insert((*this).begin(), k, mint(0));
return *this;
}
FPS& operator>>=(ll k) {
shrink();
if (k > (ll)(*this).size())
(*this).clear();
else
(*this).erase((*this).begin(), (*this).begin() + k);
return *this;
}
FPS operator<<(ll k) const { return FPS(*this) <<= k; }
FPS operator>>(ll k) const { return FPS(*this) >>= k; }
friend FPS operator+(const FPS& l, const mint& r) { return FPS(l) += r; }
friend FPS operator-(const FPS& l, const mint& r) { return FPS(l) -= r; }
friend FPS operator*(const FPS& l, const mint& r) { return FPS(l) *= r; }
friend FPS operator/(const FPS& l, const mint& r) { return FPS(l) /= r; }
friend FPS operator+(const mint& l, const FPS& r) { return FPS(r) += l; }
friend FPS operator-(const mint& l, const FPS& r) { return FPS(-r) += l; }
friend FPS operator*(const mint& l, const FPS& r) { return FPS(r) *= l; }
friend FPS operator+(const FPS& l, const FPS& r) { return FPS(l) += r; }
friend FPS operator-(const FPS& l, const FPS& r) { return FPS(l) -= r; }
friend FPS operator*(const FPS& l, const FPS& r) { return FPS(l) *= r; }
friend FPS operator/(const FPS& l, const FPS& r) { return FPS(l) /= r; }
friend FPS operator%(const FPS& l, const FPS& r) { return FPS(l) %= r; }
pair<FPS, FPS> div_mod(const FPS& r) const {
FPS q = (*this) / r;
FPS m;
if ((*this).size() >= r.size())
m = (*this) - q * r;
else
m = *this;
q.shrink(), m.shrink();
return {q, m};
}
mint operator()(const mint& x) const {
mint res = 0, w = 1;
for (auto& v : *this) res += v * w, w *= x;
return res;
}
FPS diff() const {
const int n = (*this).size();
FPS res(n - 1);
rep(i, 1, n) res[i - 1] = (*this)[i] * i;
return res;
}
FPS& inplace_diff() {
shrink();
(*this).erase((*this).begin());
mint coeff = 1;
for (int i = 0; i < (int)(*this).size(); i++) {
(*this)[i] *= coeff;
coeff++;
}
return *this;
}
FPS integral() const {
const int n = (*this).size();
FPS res(n + 1);
Comb::extend(n);
rep(i, n) res[i + 1] = (*this)[i] * Comb::inv(i + 1);
return res;
}
FPS& inplace_integral() {
shrink();
const int n = (*this).size();
vector<mint> iv(n + 1, 1);
rep(i, 2, n + 1) iv[i] = -iv[p % i] * (p / i);
(*this).insert((*this).begin(), mint(0));
rep(i, 1, n + 1)(*this)[i] *= iv[i];
return *this;
}
FPS log(int d = -1) const {
const int n = (*this).size();
if (d == -1) d = n;
FPS res = diff() * inv(d);
res.resize(d - 1);
return res.integral();
}
FPS& inplace_log(int d = -1) {
shrink();
const int n = (*this).size();
if (d == -1) d = n;
FPS tmp = inv(d);
(*this).inplace_diff() *= tmp;
(*this).resize(d - 1);
return (*this).inplace_integral();
}
FPS exp(int d = -1) const {
const int n = (*this).size();
if (d == -1) d = n;
if (n <= 1) {
FPS res(d, mint());
res[0] = 1;
return res;
}
FPS f = {mint(1) + (*this)[0], (*this)[1]}, res{1, (*this)[1]};
for (int sz = 2; sz < d; sz <<= 1) {
f.insert(f.end(), (*this).begin() + min(sz, n), (*this).begin() + min(n, sz << 1));
f.resize(sz << 1);
res = res * (f - res.log(sz << 1));
res.resize(sz << 1);
}
res.resize(d);
return res;
}
FPS pow(ll k, int d = -1) const {
const int n = (*this).size();
if (d == -1) d = n;
if (k == 0) {
FPS ans(d, mint());
ans[0] = 1;
return ans;
}
for (int i = 0; i < n; i++) {
if ((*this)[i] != mint()) {
if (i > d / k) return FPS(d, mint());
mint rev = (*this)[i].inv();
FPS res = (((*this * rev) >> i).log(d) * k).exp(d) * ((*this)[i].pow(k));
res = (res << (i * k));
res.resize(d);
return res;
}
}
return FPS(d, mint());
}
FPS sqrt(
const function<mint(mint)>& get_sqrt = [](mint) { return mint(1); }, int d = -1) const {
const int n = (*this).size();
if (d == -1) d = n;
if ((*this)[0] == mint(0)) {
rep(i, 1, n) {
if ((*this)[i] != mint(0)) {
if (i & 1) return {};
if (d - i / 2 <= 0) break;
auto res = (*this >> i).sqrt(get_sqrt, d - i / 2);
if (res.empty()) return {};
res = res << (i / 2);
res.resize(d);
return res;
}
}
return FPS(d);
}
auto sqr = get_sqrt((*this)[0]);
if (sqr * sqr != (*this)[0]) return {};
FPS res{sqr};
const mint inv2 = mint(2).inv();
FPS f = {(*this)[0]};
for (int i = 1; i < d; i <<= 1) {
if (i < n) f.insert(f.end(), (*this).begin() + i, (*this).begin() + min(n, i << 1));
if ((int)f.size() < (i << 1)) f.resize(i << 1);
res = (res + f * res.inv(i << 1)) * inv2;
}
res.resize(d);
return res;
}
};
/**
* @brief Formal Power Series(形式的冪級数)
*/
#line 3 "math/fps/multipoint-evaluation-geometric.hpp"
template <typename T>
vector<T> multipoint_evaluation_geometric(FormalPowerSeries<T> f, T a, T r, int m) {
if (m == 0) return {};
const int n = f.size();
if (r == T(0)) {
vector<T> res(m, f[0]);
res[0] = f(a);
return res;
}
if (n < 64 || m < 64) {
vector<T> res(m);
rep(i, m) res[i] = f(a), a *= r;
return res;
}
T pow_a = 1;
rep(i, n) f[i] *= pow_a, pow_a *= a;
auto calc = [](T r, int m) -> vector<T> {
vector<T> res(m);
T pow_r = 1;
res[0] = 1;
rep(i, m - 1) res[i + 1] = res[i] * pow_r, pow_r *= r;
return res;
};
auto r_memo = calc(r, n + m - 1), r_inv_memo = calc(T(1) / r, max(n, m));
rep(i, n) f[i] *= r_inv_memo[i];
reverse(f.begin(), f.end());
const int l = 1 << ceil_log2(n + m - 1);
f.resize(l);
r_memo.resize(l);
ntt(f), ntt(r_memo);
rep(i, l) f[i] *= r_memo[i];
intt(f);
f >>= n - 1;
f.resize(m);
rep(i, m) f[i] *= r_inv_memo[i];
return f;
}
/**
* @brief Multipoint Evaluation (Geometric Sequence)
* @ref https://noshi91.github.io/algorithm-encyclopedia/chirp-z-transform
*/
#line 4 "math/fps/polynomial-interpolation-gemoetric.hpp"
template <typename T>
FormalPowerSeries<T> polynomial_interpolation_gemoetric(vector<T> y, const T& a, const T& r) {
const int n = y.size();
if (n == 0) return {};
if (n == 1) return {y[0]};
if (r == 0) {
return {y[1], (y[0] - y[1]) / a};
}
vector<T> pow_r(n + 1), pow_invr(n + 1), inv_s(n);
T s = 1;
const T inv_r = T(1) / r;
{
pow_r[0] = pow_invr[0] = inv_s[0] = 1;
rep(i, n) {
if (i) s *= (1 - pow_r[i]);
pow_r[i + 1] = pow_r[i] * r;
pow_invr[i + 1] = pow_invr[i] * inv_r;
}
inv_s[n - 1] = T(1) / s;
s *= (1 - pow_r[n]);
rrep(i, n - 1) inv_s[i] = inv_s[i + 1] * (1 - pow_r[i + 1]);
}
T v = 1;
rep(i, n) {
y[i] *= v * inv_s[i] * inv_s[n - i - 1];
if (i & 1) y[i] = -y[i];
if (i != n - 1) v *= pow_invr[n - 2 - i];
}
FormalPowerSeries<T> f1(n + 1);
v = 1;
rep(i, n + 1) {
if (i == 0 || i == n)
f1[i] = v;
else
f1[i] = v * s * inv_s[i] * inv_s[n - i];
if (i & 1) f1[i] = -f1[i];
v *= pow_r[i];
}
FormalPowerSeries<T> f2 = multipoint_evaluation_geometric(FormalPowerSeries<T>(y), T(1), r, n);
auto res = (f1 * f2);
res.resize(n);
reverse(res.begin(), res.end());
const T inva = T(1) / a;
v = 1;
rep(i, n) res[i] *= v, v *= inva;
return res;
}
/**
* @brief Polynomial Interpolation (Geometric Sequence)
* @ref https://noshi91.github.io/algorithm-encyclopedia/polynomial-interpolation-geometric
*/