結果
| 問題 | No.1171 Runs in Subsequences |
| コンテスト | |
| ユーザー |
miscalc
|
| 提出日時 | 2026-09-17 00:58:22 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 11 ms / 2,000 ms |
| + 216µs | |
| コード長 | 45,233 bytes |
| 記録 | |
| コンパイル時間 | 4,104 ms |
| コンパイル使用メモリ | 383,424 KB |
| 実行使用メモリ | 12,288 KB |
| 最終ジャッジ日時 | 2026-09-17 00:58:29 |
| 合計ジャッジ時間 | 6,316 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 18 |
ソースコード
#define SINGLE_TESTCASE
#define FAST_IO
#define INF 4'000'000'000'000'000'037LL
#define EPS 1e-11
// https://github.com/miscalculation53/library/tree/wip/template/template_all.hpp
// https://github.com/miscalculation53/library/tree/wip/template/template_all_but_modint.hpp
// https://github.com/miscalculation53/library/tree/wip/template/template_types.hpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using pll = pair<ll, ll>;
#define vc vector
template <class T>
using vvc = vc<vc<T>>;
using vl = vc<ll>;
using vstr = vc<string>;
using i128 = __int128_t;
using u128 = __uint128_t;
i128 stoi128(const string &s)
{
const bool neg = s.front() == '-';
u128 res = 0;
for (int i = neg; i < (int)s.size(); i++)
res = 10 * res + s[i] - '0';
if (neg)
return -i128(res - 1) - 1;
return i128(res);
}
string i128tos(i128 x)
{
if (x == 0) return "0";
string sign = "", res = "";
u128 ux;
if (x < 0)
ux = u128(-(x + 1)) + 1, sign = "-";
else
ux = x;
while (ux > 0)
{
res += '0' + ux % 10;
ux /= 10;
}
reverse(res.begin(), res.end());
return sign + res;
}
istream &operator>>(istream &is, i128 &a)
{
string s;
is >> s;
a = stoi128(s);
return is;
}
ostream &operator<<(ostream &os, const i128 &a)
{
os << i128tos(a);
return os;
}
#define cauto const auto
// https://github.com/miscalculation53/library/tree/wip/template/template_rep.hpp
#define overload4(_1,_2,_3,_4,name,...) name
#define rep1(i,n) for (ll i = 0, nnnnn = ll(n); i < nnnnn; i++)
#define rep2(i,l,r) for (ll i = ll(l), rrrrr = ll(r); i < rrrrr; i++)
#define rep3(i,l,r,d) for (ll i = ll(l), rrrrr = ll(r), ddddd = ll(d); ddddd > 0 ? i < rrrrr : i > rrrrr; i += d)
#define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define repi1(i,n) for (int i = 0, nnnnn = int(n); i < nnnnn; i++)
#define repi2(i,l,r) for (int i = int(l), rrrrr = int(r); i < rrrrr; i++)
#define repi3(i,l,r,d) for (int i = int(l), rrrrr = int(r), ddddd = int(d); ddddd > 0 ? i < rrrrr : i > rrrrr; i += d)
#define repi(...) overload4(__VA_ARGS__, repi3, repi2, repi1)(__VA_ARGS__)
#define fe(...) for (auto __VA_ARGS__)
#define fec(...) for (cauto &__VA_ARGS__)
#define fem(...) for (auto &__VA_ARGS__)
// https://github.com/miscalculation53/library/tree/wip/template/template_math.hpp
// https://github.com/miscalculation53/library/tree/wip/utils/is_integral_ext.hpp
template <class T>
constexpr bool is_integral_ext = is_integral_v<T> || is_same_v<T, i128> || is_same_v<T, u128>;
template <class T>
constexpr bool is_signed_ext = is_signed_v<T> || is_same_v<T, i128>;
template <class T>
constexpr bool is_unsigned_ext = is_unsigned_v<T> || is_same_v<T, u128>;
// https://github.com/miscalculation53/library/tree/wip/utils/default_infty.hpp
namespace default_infty_detail
{
template <class T, class = void>
struct has_infty : false_type {};
template <class T>
struct has_infty<T, void_t<decltype(T::infty())>> : true_type {};
template <class T>
const T &custom_value()
{
static const T value = T::infty();
return value;
}
template <class T>
inline constexpr bool unsupported = false;
}
template <class T>
constexpr decltype(auto) default_infty()
{
if constexpr (default_infty_detail::has_infty<T>::value)
return default_infty_detail::custom_value<T>();
else if constexpr (is_same_v<T, i128> || is_same_v<T, u128>)
return T(INF) * T(INF);
else if constexpr (is_integral_ext<T> && !is_same_v<T, bool>)
{
if constexpr (sizeof(T) >= sizeof(ll))
return T(INF);
else if constexpr (numeric_limits<T>::digits >= 31)
return (T(1) << 30) - 1;
else
return T(numeric_limits<T>::max() / 2);
}
else if constexpr (numeric_limits<T>::has_infinity)
return numeric_limits<T>::infinity();
else
static_assert(default_infty_detail::unsupported<T>, "No default infinity for this type; specify infty explicitly or define T::infty().");
}
template <class T, class U>
inline bool chmin(T &a, U b) { return a > b ? a = b, true : false; }
template <class T = ll, class U, class V, typename = enable_if_t<is_integral_ext<U> && is_integral_ext<V>>>
inline constexpr T divfloor(U a, V b) { return T(a) / T(b) - (T(a) % T(b) && (T(a) ^ T(b)) < 0); }
template <class T = ll, class U, class V, typename = enable_if_t<is_integral_ext<U> && is_integral_ext<V>>>
inline constexpr T safemod(U a, V b) { return T(a) - T(b) * divfloor<T>(a, b); }
// https://github.com/miscalculation53/library/tree/wip/template/template_vector.hpp
#define ALL(a) (a).begin(), (a).end()
template <class T = ll, class V>
inline T SZ(const V &x) { return x.size(); }
#define eb emplace_back
#define LMD(x,fx) ([&](const auto &x) { return fx; })
template <class F>
auto gen_vec(int n, const F &f)
{
vc<decltype(f(0))> res(n);
repi(i, n) res[i] = f(i);
return res;
}
#define GEN_VEC(n,i,fi) (gen_vec(n, LMD(i, fi)))
template <class T = ll>
vc<T> stov(const string &s, char first)
{
return gen_vec(SZ<int>(s), [&](int i) -> T
{ return s[i] - first; });
}
// https://github.com/miscalculation53/library/tree/wip/template/template_algo.hpp
// https://github.com/miscalculation53/library/tree/wip/utils/resolved_infty.hpp
// https://github.com/miscalculation53/library/tree/wip/utils/resolved_value.hpp
template <class T, auto x, enable_if_t<!is_invocable_v<decltype(x)>, int> = 0>
constexpr T resolved_value()
{
return T(x);
}
template <class T, auto x, enable_if_t<is_invocable_v<decltype(x)>, long> = 0>
const T &resolved_value()
{
static const T value = T(x());
return value;
}
template <class T, auto x = nullptr>
constexpr decltype(auto) resolved_infty()
{
if constexpr (is_same_v<decltype(x), nullptr_t>)
return default_infty<T>();
else
return resolved_value<T, x>();
}
template <class V>
auto MAX(const V &v) { return *max_element(ALL(v)); }
template <class T>
vvc<T> top(const vvc<T> &a)
{
if (a.empty())
return {};
const int n = a.size(), m = a[0].size();
vvc<T> b(m, vc<T>(n));
repi(i, n)
{
assert(SZ<int>(a[i]) == m);
repi(j, m) b[j][i] = a[i][j];
}
return b;
}
vstr top(const vstr &a)
{
vvc<char> a_(a.size());
repi(i, SZ<int>(a)) a_[i] = {ALL(a[i])};
vvc<char> b_ = top(a_);
vstr b(b_.size());
repi(i, SZ<int>(b)) b[i] = {ALL(b_[i])};
return b;
}
template <class T, class = void>
struct has_e0 : false_type {};
template <class T>
struct has_e0<T, void_t<decltype(T::e0())>> : true_type {};
template <class T>
inline constexpr bool has_e0_v = has_e0<T>::value;
template <class T>
struct MonoidAdd
{
using S = T;
static constexpr S op(S a, S b) { return a + b; }
static constexpr S e()
{
if constexpr (has_e0_v<S>)
return S::e0();
else
return {};
}
};
namespace internal
{
}
template <class M>
vc<typename M::S> cuml(const vc<typename M::S> &v, int left_index = 0)
{
const int n = v.size();
vc<typename M::S> res(n + 1);
res[0] = M::e();
repi(i, n) res[i + 1] = M::op(res[i], v[i]);
res.erase(res.begin(), res.begin() + left_index);
return res;
}
template <class T>
vc<T> cumlsum(const vc<T> &v, int left_index = 0)
{ return cuml<MonoidAdd<T>>(v, left_index); }
constexpr array<pll, 4> DRULgrid = {{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}};
constexpr array<pll, 4> DRULplane = {{{0, -1}, {1, 0}, {0, 1}, {-1, 0}}};
// https://github.com/miscalculation53/library/tree/wip/template/template_binsearch.hpp
template <class T>
struct is_random_access_iterator
{
static constexpr bool value = is_same_v<
typename iterator_traits<T>::iterator_category,
random_access_iterator_tag
>;
};
template <class T>
constexpr bool is_random_access_iterator_v = is_random_access_iterator<T>::value;
#define DEFAULT_COMP ranges::less
namespace internal
{
};
// https://github.com/miscalculation53/library/tree/wip/template/template_bit.hpp
template <class T>
inline constexpr ull MASK(T k) { return (1ULL << k) - 1ULL; }
inline constexpr ll bit_width(ll x) { return std::bit_width((ull)x); }
inline constexpr ll bit_floor(ll x) { return std::bit_floor((ull)x); }
inline constexpr ll bit_ceil(ll x) { return std::bit_ceil((ull)x); }
inline constexpr ll countr_zero(ll x) { assert(x != 0); return std::countr_zero((ull)x); }
inline constexpr ll popcount(ll x) { return std::popcount((ull)x); }
inline constexpr bool has_single_bit(ll x) { return std::has_single_bit((ull)x); }
inline constexpr ull lsb_pos(ull x) { assert(x != 0); return countr_zero(x); }
inline constexpr ull msb_pos(ull x) { assert(x != 0); return bit_width(x) - 1; }
inline constexpr ull lsb_mask(ull x) { assert(x != 0); return x & -x; }
inline constexpr ull msb_mask(ull x) { assert(x != 0); return bit_floor(x); }
inline constexpr bool btest(ull x, uint k) { return (x >> k) & 1; }
inline constexpr bool bsubset(ull x, ull y) { return (x & y) == x; }
inline constexpr bool bsupset(ull x, ull y) { return (x & y) == y; }
inline constexpr ull bsetminus(ull x, ull y) { return x & ~y; }
// https://github.com/miscalculation53/library/tree/wip/template/template_inout.hpp
// https://github.com/miscalculation53/library/tree/wip/template/template_dump.hpp
// https://github.com/miscalculation53/library/tree/wip/template/template_dump_map.hpp
#define CPP_DUMP_DEFINE_DATA(...)
#define dump(...)
#define local(...)
#define oj(...) __VA_ARGS__
#define local_oj(a,b) (b)
namespace fastio {
template <class T>
struct unsigned_integer
{
using type = make_unsigned_t<T>;
};
template <>
struct unsigned_integer<i128>
{
using type = u128;
};
template <>
struct unsigned_integer<u128>
{
using type = u128;
};
template <class T>
using unsigned_integer_t = typename unsigned_integer<T>::type;
static constexpr uint32_t SIZ = 1 << 17;
char ibuf[SIZ];
char obuf[SIZ];
char out[100];
uint32_t pil = 0, pir = 0, por = 0;
struct Pre {
char num[10000][4];
constexpr Pre() : num() {
for (int i = 0; i < 10000; i++) {
int n = i;
for (int j = 3; j >= 0; j--) {
num[i][j] = n % 10 | '0';
n /= 10;
}
}
}
} constexpr pre;
inline void load() {
memcpy(ibuf, ibuf + pil, pir - pil);
pir = pir - pil + fread(ibuf + pir - pil, 1, SIZ - pir + pil, stdin);
pil = 0;
if (pir < SIZ) ibuf[pir++] = '\n';
}
inline void flush() {
fwrite(obuf, 1, por, stdout);
por = 0;
}
void rd1(char &c) {
do {
if (pil + 1 > pir) load();
c = ibuf[pil++];
} while (c <= ' ');
}
void rd1(string &x) {
x.clear();
while (true) {
if (pil == pir) load();
while (pil < pir && ibuf[pil] <= ' ') ++pil;
if (pil < pir) break;
}
while (true) {
uint32_t p = pil;
while (pil < pir && ibuf[pil] > ' ') ++pil;
x.append(ibuf + p, pil - p);
if (pil < pir) {
++pil;
return;
}
load();
}
}
template <typename T>
void rd1_real(T &x) {
string s;
rd1(s);
if constexpr (!is_same_v<T, long double>)
{
auto [p, ec] = from_chars(s.data(), s.data() + s.size(), x);
if (ec == errc{} && p == s.data() + s.size()) return;
}
if constexpr (is_same_v<T, long double>)
x = stold(s);
else
x = stod(s);
}
template <bool check_buffer = true, typename T>
void rd1_integer(T &x) {
using U = unsigned_integer_t<T>;
bool minus = false;
U val = 0;
if constexpr (check_buffer)
if (pil + 100 > pir) load();
uint32_t p = pil;
while (ibuf[p] < '-') ++p;
if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
if (ibuf[p] == '-') minus = true, ++p;
}
while ('0' <= ibuf[p]) val = val * 10 + (ibuf[p++] & 15);
pil = p;
if constexpr (is_signed<T>::value || is_same_v<T, i128>)
{
if (minus)
{
const U min_abs = U(numeric_limits<T>::max()) + 1;
x = val == min_abs ? numeric_limits<T>::lowest() : -T(val);
}
else x = T(val);
}
else
x = T(val);
}
void rd1(int &x) { rd1_integer(x); }
void rd1(ll &x) { rd1_integer(x); }
void rd1(i128 &x) { rd1_integer(x); }
void rd1(uint &x) { rd1_integer(x); }
void rd1(ull &x) { rd1_integer(x); }
void rd1(u128 &x) { rd1_integer(x); }
void rd1(double &x) { rd1_real(x); }
void rd1(long double &x) { rd1_real(x); }
template <class... T>
void read(T &...x) {
if constexpr (sizeof...(T) <= SIZ / 100 &&
((!is_same_v<T, char> &&
(is_integral_v<T> || is_same_v<T, i128> || is_same_v<T, u128>)) && ...)) {
if (pil + 100 * sizeof...(T) > pir) load();
(rd1_integer<false>(x), ...);
}
else
(rd1(x), ...);
}
void wt1(const char c) {
if (por == SIZ) flush();
obuf[por++] = c;
}
void wt1(string_view s) {
while (!s.empty()) {
if (por == SIZ) flush();
size_t n = min<size_t>(s.size(), SIZ - por);
memcpy(obuf + por, s.data(), n);
por += n;
s.remove_prefix(n);
}
}
template <typename T>
void wt1_integer(T x) {
if (por > SIZ - 100) flush();
using U = unsigned_integer_t<T>;
U ux;
if constexpr (is_signed<T>::value || is_same_v<T, i128>)
{
if (x < 0)
obuf[por++] = '-', ux = U(0) - U(x);
else
ux = U(x);
}
else
ux = x;
int outi;
for (outi = 96; ux >= 10000; outi -= 4) {
memcpy(out + outi, pre.num[ux % 10000], 4);
ux /= 10000;
}
if (ux >= 1000) {
memcpy(obuf + por, pre.num[ux], 4);
por += 4;
} else if (ux >= 100) {
memcpy(obuf + por, pre.num[ux] + 1, 3);
por += 3;
} else if (ux >= 10) {
int q = (ux * 103) >> 10;
obuf[por] = q | '0';
obuf[por + 1] = (ux - q * 10) | '0';
por += 2;
} else
obuf[por++] = ux | '0';
memcpy(obuf + por, out + outi + 4, 96 - outi);
por += 96 - outi;
}
template <typename T>
void wt1_real(T x) {
if constexpr (!is_same_v<T, long double>)
{
auto [p, ec] = to_chars(out, out + sizeof(out), x, chars_format::fixed, 15);
if (ec == errc{}) {
wt1(string_view(out, p));
return;
}
}
ostringstream oss;
oss << fixed << setprecision(15) << x;
wt1(oss.str());
}
void wt1(int x) { wt1_integer(x); }
void wt1(i128 x) { wt1_integer(x); }
void wt1(u128 x) { wt1_integer(x); }
void wt1(double x) { wt1_real(x); }
void wt1(long double x) { wt1_real(x); }
template <class... T>
void write(T &&...x) {
(wt1(std::forward<T>(x)), ...);
}
template <class... T>
void print(T &&...x) {
if constexpr (sizeof...(T))
{
int i = 0;
((i++ ? wt1(' ') : void(), wt1(std::forward<T>(x))), ...);
}
wt1('\n');
}
}
struct Dummy {
Dummy() { atexit(fastio::flush); }
} dummy;
namespace internal
{
template <class... Ts>
void READnodump(Ts &...a) { fastio::read(a...); }
};
#define READ(...) internal::READnodump(__VA_ARGS__); dump(__VA_ARGS__)
#define IN(T,...) T __VA_ARGS__; READ(__VA_ARGS__)
#define CHAR(...) IN(char, __VA_ARGS__)
#define INT(...) IN(int, __VA_ARGS__)
#define LL(...) IN(ll, __VA_ARGS__)
#define STR(...) IN(string, __VA_ARGS__)
#define ARR(T,n,...) array<T, n> __VA_ARGS__; READ(__VA_ARGS__)
#define READVEC(...) internal::READVECnodump(__VA_ARGS__); dump(__VA_ARGS__)
#define READVEC2(...) internal::READVEC2nodump(__VA_ARGS__); dump(__VA_ARGS__)
#define VEC(T,n,...) vc<T> __VA_ARGS__; READVEC(n, __VA_ARGS__)
#define VEC2(T,n,m,...) vvc<T> __VA_ARGS__; READVEC2(n, m, __VA_ARGS__)
#define READJAG(...) internal::READJAGnodump(__VA_ARGS__); dump(__VA_ARGS__)
#define JAG(T,n,...) vvc<T> __VA_ARGS__; READJAG(n, __VA_ARGS__)
#define ENDL '\n'
#define WRITE fastio::write
#define PRINT fastio::print
#define PRINTEXIT(...) do { PRINT(__VA_ARGS__); exit(0); } while (false)
#define PRINTRETURN(...) do { PRINT(__VA_ARGS__); return; } while (false)
#define PRINTVEXIT(...) do { PRINTV(__VA_ARGS__); exit(0); } while (false)
#define PRINTVRETURN(...) do { PRINTV(__VA_ARGS__); return; } while (false)
namespace internal
{
};
namespace internal
{
};
#define UNZIP(vt,...) auto [__VA_ARGS__] = unzip(vt)
#define ZIP(vt,...) auto vt = zip(tuple{__VA_ARGS__})
// https://github.com/miscalculation53/library/tree/wip/template/template_random.hpp
mt19937_64 mt;
bool randbool(double p)
{
assert(0 <= p && p <= 1);
return bernoulli_distribution(p)(mt);
}
namespace internal
{
};
// https://github.com/miscalculation53/library/tree/wip/math/modint/template_modint.hpp
// https://github.com/miscalculation53/library/tree/wip/math/modint/modint.hpp
// https://github.com/miscalculation53/library/tree/wip/math/modint/modint_internal_static.hpp
// https://github.com/miscalculation53/library/tree/wip/utils/larger_int.hpp
namespace larger_int_detail
{
}
template <class T>
struct larger_int
{
private:
static constexpr bool check();
static_assert(check());
public:
using type = T;
};
#define LARGER_INT(T,U) template <> struct larger_int<T> { using type = U; };
LARGER_INT(signed char, short)
LARGER_INT(short, int)
LARGER_INT(int, long long)
LARGER_INT(long, __int128_t)
LARGER_INT(long long, __int128_t)
LARGER_INT(unsigned char, unsigned short)
LARGER_INT(unsigned short, unsigned int)
LARGER_INT(unsigned int, unsigned long long)
LARGER_INT(unsigned long, __uint128_t)
LARGER_INT(unsigned long long, __uint128_t)
#undef LARGER_INT
template <class T>
struct Rational;
template <class T>
struct larger_int<Rational<T>>
{
using type = Rational<typename larger_int<T>::type>;
};
template <class T>
using larger_int_t = typename larger_int<T>::type;
// https://github.com/miscalculation53/library/tree/wip/math/modint/modint_internal_isprime.hpp
namespace internal
{
template <class T>
constexpr ll powmod_constexpr(ll x, ll n, T m)
{
if (m == 1)
return 0;
using U = make_unsigned_t<T>;
using L = larger_int_t<U>;
U r = 1, y = safemod(x, m);
while (n)
{
if (n & 1)
r = L(r) * y % m;
y = L(y) * y % m;
n >>= 1;
}
return r;
}
template <class T>
constexpr bool isprime_constexpr(T n)
{
if constexpr (sizeof(T) > 4)
{
if (n <= INT_MAX)
return isprime_constexpr<int>(n);
}
if (n <= 1)
return false;
if (n == 2 || n == 7 || n == 61)
return true;
if (n % 2 == 0)
return false;
ll d = n - 1;
while (d % 2 == 0)
d /= 2;
using U = make_unsigned_t<T>;
using L = larger_int_t<U>;
auto miller_rabin = [&](const auto &bases) constexpr
{
for (ll a : bases)
{
ll t = d, y = powmod_constexpr(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1)
{
y = L(y) * y % n;
t <<= 1;
}
if (y != n - 1 && t % 2 == 0)
return false;
}
return true;
};
if constexpr(sizeof(T) <= 4)
{
constexpr ll bases[3] = {2, 7, 61};
return miller_rabin(bases);
}
else
{
constexpr ll bases[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
return miller_rabin(bases);
}
}
template <auto n>
constexpr bool isprime = isprime_constexpr(n);
};
namespace internal
{
template <auto M>
struct policy_static
{
using mod_type = decltype(M);
using value_type = make_unsigned_t<mod_type>;
using calc_type = larger_int_t<value_type>;
static constexpr bool is_prime = isprime_constexpr(M);
static constexpr mod_type mod() { return M; }
static constexpr value_type umod() { return M; }
static constexpr value_type init(value_type v) { return v; }
static constexpr mod_type val(value_type v) { return v; }
static constexpr value_type mul(value_type a, value_type b)
{
return (value_type)((calc_type(a) * b) % M);
}
};
};
// https://github.com/miscalculation53/library/tree/wip/math/modint/modint_internal_barrett32.hpp
namespace internal
{
struct barrett32
{
uint m;
ull im;
explicit barrett32(uint m) : m(m), im((ull)(-1) / m + 1) {}
uint umod() const { return m; }
uint mul(uint a, uint b) const
{
ull z = a;
z *= b;
ull x = ull((u128(z) * im) >> 64);
ull y = x * m;
return uint(z - y + (z < y ? m : 0));
}
};
template <int id>
struct policy_barrett32
{
using value_type = uint;
using calc_type = ull;
using mod_type = int;
static constexpr bool is_prime = false;
static inline barrett32 reducer{998244353};
static mod_type mod() { return reducer.umod(); }
static value_type umod() { return reducer.umod(); }
static value_type init(value_type v) { return v; }
};
};
// https://github.com/miscalculation53/library/tree/wip/math/modint/modint_internal_montgomery64.hpp
namespace internal
{
inline constexpr ull inv64(ull a)
{
ull x = a;
while (a * x != 1) x *= 2 - a * x;
return x;
}
struct montgomery64odd
{
ull m, im, sq;
explicit montgomery64odd(ull m) : m(m), im(inv64(m)), sq(-u128(m) % m) {}
ull umod() const { return m; }
ull reduce(u128 x) const
{
auto t = (x + u128(m) * (-im * ull(x))) >> 64;
if (t >= m) t -= m;
return (ull)t;
}
ull inv_reduce(i128 v) const { return reduce(u128(v % m + m) * sq); }
};
struct montgomery64
{
ull m, mx, imx, d, q;
uint b;
explicit montgomery64(ull m) : m(m)
{
b = countr_zero(m), mx = m >> b;
imx = inv64(mx);
d = powmod_constexpr((mx + 1) / 2, b, mx);
u128 sq = -u128(mx) % mx;
q = (1 + (((sq - 1) * d) << b)) % m;
}
ull umod() const { return m; }
ull reduce(u128 x) const
{
if (b == 0)
{
auto t = (x + u128(mx) * (-imx * ull(x))) >> 64;
if (t >= m) t -= m;
return (ull)t;
}
ull p = x & MASK(b);
x = (x >> b) + p * d;
ull y = p << (64 - b);
auto t = (x + u128(mx) * (imx * (y - ull(x)))) >> (64 - b);
if (t >= m) { t -= m; if (t >= m) t -= m; }
return (ull)t;
}
ull inv_reduce(i128 v) const { return reduce(u128(v % m + m) * q); }
};
template <int id>
struct policy_montgomery64_odd
{
using value_type = ull;
using calc_type = u128;
using mod_type = ll;
static constexpr bool is_prime = false;
static inline montgomery64odd reducer{(1LL << 61) - 1};
static mod_type mod() { return reducer.umod(); }
static value_type umod() { return reducer.umod(); }
static value_type init(value_type v) { return reducer.inv_reduce(v); }
};
template <int id>
struct policy_montgomery64
{
using value_type = ull;
using calc_type = u128;
using mod_type = ll;
static constexpr bool is_prime = false;
static inline montgomery64 reducer{(1LL << 61) - 1};
static mod_type mod() { return reducer.umod(); }
static value_type umod() { return reducer.umod(); }
static value_type init(value_type v) { return reducer.inv_reduce(v); }
};
};
// https://github.com/miscalculation53/library/tree/wip/math/extgcd.hpp
template <class T = ll>
constexpr tuple<T, T, T> extgcd(T a, T b)
{
if (a == 0 && b == 0)
return {0, 0, 0};
T x1 = 1, y1 = 0, z1 = a;
T x2 = 0, y2 = 1, z2 = b;
while (z2 != 0)
{
T q = z1 / z2;
tie(x1, x2) = make_pair(x2, x1 - q * x2);
tie(y1, y2) = make_pair(y2, y1 - q * y2);
tie(z1, z2) = make_pair(z2, z1 - q * z2);
}
if (z1 < 0)
x1 = -x1, y1 = -y1, z1 = -z1;
return {z1, x1, y1};
}
namespace internal
{
template <class Policy>
struct modint_impl
{
using V = typename Policy::value_type;
using M = typename Policy::mod_type;
using mint = modint_impl;
private:
V _v;
public:
static constexpr M mod() { return Policy::mod(); }
static mint raw(V v)
{
mint x;
x._v = v;
return x;
}
modint_impl() : _v(0) {}
template <class T, typename = enable_if_t<is_integral_ext<T>>>
modint_impl(T v)
{
V rem;
if constexpr (is_signed_ext<T>)
{
using S = make_signed_t<V>;
S x = v % S(Policy::umod());
if (x < 0)
x += Policy::umod();
rem = x;
}
else
rem = V(v % Policy::umod());
_v = Policy::init(rem);
};
M val() const { return Policy::val(_v); }
mint &operator+=(const mint &rhs)
{
_v += rhs._v;
if (_v >= Policy::umod())
_v -= Policy::umod();
return *this;
}
mint &operator-=(const mint &rhs)
{
_v -= rhs._v;
if (_v >= Policy::umod())
_v += Policy::umod();
return *this;
}
mint &operator*=(const mint &rhs)
{
_v = Policy::mul(_v, rhs._v);
return *this;
}
mint operator-() const { return mint() - *this; }
template <class T>
mint pow(T n) const
{
assert(n >= 0);
mint x = *this, r = 1;
while (n)
{
if (n & 1)
r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const
{
if constexpr (Policy::is_prime)
{
return pow(mod() - 2);
}
else
{
auto [g, x, y] = extgcd<M>(val(), mod());
assert(g == 1);
return mint(x);
}
}
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; }
friend M safe_hash_key(const mint &x) { return x.val(); }
friend void rd1(mint &x)
{
long long a;
fastio::rd1(a);
x = a;
}
friend void wt1(const mint &x)
{
fastio::wt1(x.val());
}
};
};
template <int mod>
using static_modint32 = internal::modint_impl<internal::policy_static<mod>>;
template <int id>
using dynamic_modint32 = internal::modint_impl<internal::policy_barrett32<id>>;
template <ll mod>
using static_modint64 = internal::modint_impl<internal::policy_static<mod>>;
template <int id>
using dynamic_modint64_odd = internal::modint_impl<internal::policy_montgomery64_odd<id>>;
template <int id>
using dynamic_modint64 = internal::modint_impl<internal::policy_montgomery64<id>>;
using modint1000000007 = static_modint32<1000000007>;
template <class T>
struct is_modint : std::false_type
{
};
template <class Policy>
struct is_modint<internal::modint_impl<Policy>> : std::true_type
{
};
template <class T>
inline constexpr bool is_modint_v = is_modint<T>::value;
template <class T>
struct is_static_modint : false_type {};
template <int m>
struct is_static_modint<static_modint32<m>> : true_type {};
template <ll m>
struct is_static_modint<static_modint64<m>> : true_type {};
template <class T>
inline constexpr bool is_static_modint_v = is_static_modint<T>::value;
template <class T>
struct is_dynamic_modint : false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint32<id>> : true_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint64_odd<id>> : true_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint64<id>> : true_type {};
template <class T>
inline constexpr bool is_dynamic_modint_v = is_dynamic_modint<T>::value;
// https://github.com/miscalculation53/library/tree/wip/math/modint/power_table.hpp
template <class mint>
struct PowerTable
{
private:
decltype(mint::mod()) mod;
mint base;
vc<mint> pw;
public:
PowerTable(const mint &base) : mod(mint::mod()), base(base), pw(1, 1) {}
void reserve(int n)
{
if (mod != mint::mod())
{
mod = mint::mod();
pw = {1};
}
int i = pw.size();
if (n < i)
return;
pw.resize(n + 1);
for (; i <= n; i++)
pw[i] = pw[i - 1] * base;
}
mint pow(int n)
{
assert(n >= 0);
reserve(n);
return pw[n];
}
};
// https://github.com/miscalculation53/library/tree/wip/math/modint/binomial.hpp
template <class T>
struct Binomial
{
private:
inline static decltype(T::mod()) mod;
public:
inline static vc<T> fac_, finv_, inv_;
static void reserve(int n)
{
if constexpr (is_dynamic_modint_v<T>)
{
if (mod != T::mod())
{
mod = T::mod();
fac_ = {1, 1}, finv_ = {1, 1}, inv_ = {0, 1};
}
}
else
{
if (fac_.empty())
fac_ = {1, 1}, finv_ = {1, 1}, inv_ = {0, 1};
}
if (n < SZ(fac_))
return;
chmin(n, T::mod() - 1);
int si = fac_.size();
fac_.resize(n + 1), finv_.resize(n + 1), inv_.resize(n + 1);
repi(i, si, n + 1)
{
fac_[i] = fac_[i - 1] * T::raw(i);
inv_[i] = -inv_[T::mod() % i] * T::raw(T::mod() / i);
finv_[i] = finv_[i - 1] * inv_[i];
}
}
static T inv(int n)
{
n %= T::mod();
if (n < 0)
n += T::mod();
assert(n != 0);
reserve(n);
return inv_[n];
}
static T C(int n, int k)
{
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
if (n >= T::mod())
return 0;
reserve(n);
return fac_[n] * finv_[k] * finv_[n - k];
}
};
// https://github.com/miscalculation53/library/tree/wip/math/modint/stom.hpp
// https://github.com/miscalculation53/library/tree/wip/math/modint/to_rational.hpp
// https://github.com/miscalculation53/library/tree/wip/math/svp2d.hpp
namespace cpp_dump
{
template <class T>
struct rat_value
{
T p, q;
friend ostream &operator<<(ostream &os, const rat_value &x)
{
os << x.p;
if (x.q != 1)
os << '/' << x.q;
return os;
}
};
template <class T>
inline constexpr bool rat_is_map = false;
template <class... Args>
inline constexpr bool rat_is_map<std::map<Args...>> = true;
template <class... Args>
inline constexpr bool rat_is_map<std::multimap<Args...>> = true;
template <class... Args>
inline constexpr bool rat_is_map<std::unordered_map<Args...>> = true;
template <class... Args>
inline constexpr bool rat_is_map<std::unordered_multimap<Args...>> = true;
struct rat_closure : std::ranges::range_adaptor_closure<rat_closure>
{
};
constexpr rat_closure rat()
{
return rat_closure{};
}
}
using mint = modint1000000007;
using bi = Binomial<mint>;
void init()
{
oj(mt.seed(random_device()()));
}
// https://github.com/miscalculation53/library/tree/wip/ds/group_index.hpp
// https://github.com/miscalculation53/library/tree/wip/ds/csr.hpp
template <class T, bool is_erasable = false>
struct CSR
{
protected:
int n, m;
vc<int> start;
vc<T> elist;
vc<int> len;
inline int get_last(int i) const
{
if constexpr (is_erasable)
return start[i] + len[i];
else
return start[i + 1];
}
template <class Iter>
struct RowBase
{
using iterator = Iter;
using reference = typename iterator_traits<iterator>::reference;
private:
iterator begi, endi;
public:
RowBase(const iterator &begi, const iterator &endi) : begi(begi), endi(endi) {}
inline iterator begin() const { return begi; }
template <class I = ll>
inline I size() const { return endi - begi; }
inline bool empty() const { return size() == 0; }
inline reference operator[](int i) const { return *(begi + i); }
inline reference back() const
{
assert(!empty());
return *prev(endi);
}
};
using Row = RowBase<typename vc<T>::iterator>;
using ConstRow = RowBase<typename vc<T>::const_iterator>;
public:
CSR() {}
CSR(const vc<int> &row_sizes)
: n(row_sizes.size())
{
fec(s : row_sizes) assert(s >= 0);
start = cumlsum(row_sizes);
m = start.back();
elist.resize(m);
if constexpr (is_erasable)
len = row_sizes;
}
template <class I>
CSR(int n, const vc<pair<I, T>> &ies) : n(n), m(ies.size()), start(n, 0), elist(m)
{
if constexpr (is_erasable)
len.resize(n);
fec([ i, e ] : ies)
{
assert(0 <= i && i < n);
start[i]++;
}
start = cumlsum(start);
if constexpr (is_erasable)
repi(i, n) len[i] = start[i + 1] - start[i];
auto cnt = start;
repi(j, m)
{
cauto & [ i, e ] = ies[j];
int &k = cnt[i];
elist[k] = e;
k++;
}
}
CSR(const vvc<T> &vv) : n(vv.size()), start(n + 1, 0)
{
m = 0;
fec(row : vv) m += row.size();
elist.resize(m);
if constexpr (is_erasable)
len.resize(n);
for (int i = 0, j = 0; i < n; i++)
{
start[i] = j;
if constexpr (is_erasable)
len[i] = vv[i].size();
fec(e : vv[i])
{
elist[j] = e;
j++;
}
}
start.back() = m;
}
ConstRow at(int i) const
{
if (!(0 <= i && i < n))
return ConstRow(elist.begin(), elist.begin());
return ConstRow(elist.begin() + start[i], elist.begin() + get_last(i));
}
template <class I = ll>
I size() const { return n; }
};
template <class I = ll>
struct GroupIndex
{
private:
int n, m;
CSR<I> csr;
public:
GroupIndex() {}
template <class T>
GroupIndex(const vc<T> &a) : n(a.size()), m(a.empty() ? 0 : MAX(a) + 1)
{
vc<pair<int, I>> ies(n);
repi(i, n)
{
assert(0 <= a[i]);
ies[i] = {a[i], i};
}
csr = CSR(m, ies);
}
auto idxs(int val) const { return csr.at(val); }
I value_bound() const { return m; }
};
// https://github.com/miscalculation53/library/tree/wip/math/convolution/convolution_ll.hpp
// https://github.com/miscalculation53/library/tree/wip/math/convolution/convolution.hpp
// https://github.com/miscalculation53/library/tree/wip/math/crt.hpp
template <class T = ll, class R0, class R1, class M0, class M1>
constexpr tuple<bool, T, T> crt2(R0 r0_, R1 r1_, M0 m0_, M1 m1_)
{
T m0 = m0_, m1 = m1_;
assert(m0 >= 1 && m1 >= 1);
T r0 = safemod(r0_, m0), r1 = safemod(r1_, m1);
if (m0 < m1)
swap(r0, r1), swap(m0, m1);
if (m0 % m1 == 0)
{
if (r0 % m1 != r1)
return {false, 0, 0};
return {true, r0, m0};
}
auto [g, im, _] = extgcd<T>(m0, m1);
T u1 = m1 / g;
if ((r1 - r0) % g)
return {false, 0, 0};
T x = (r1 - r0) / g % u1 * im % u1;
r0 += x * m0;
m0 *= u1;
if (r0 < 0)
r0 += m0;
return {true, r0, m0};
}
template <class T = ll, class V1, class V2>
constexpr tuple<bool, T, T> crt(const V1 &rs, const V2 &ms)
{
assert(rs.size() == ms.size());
const int n = rs.size();
T r = 0, m = 1;
repi(i, n)
{
auto [ok, nr, nm] = crt2<T>(r, rs[i], m, ms[i]);
if (!ok)
return {false, 0, 0};
r = nr, m = nm;
}
return {true, r, m};
}
// https://github.com/miscalculation53/library/tree/wip/math/dot_product.hpp
// https://github.com/miscalculation53/library/tree/wip/algebra/algebra_basic_ops.hpp
// https://github.com/miscalculation53/library/tree/wip/algebra/algebra_base.hpp
template <class T, class = void>
struct has_e1 : false_type {};
template <class T>
struct has_e1<T, void_t<decltype(T::e1())>> : true_type {};
template <class T>
inline constexpr bool has_e1_v = has_e1<T>::value;
// https://github.com/miscalculation53/library/tree/wip/math/modint/internal_mod32.hpp
namespace internal
{
template <class S>
struct ordinary_mod32 : false_type {};
template <int mod>
struct ordinary_mod32<static_modint32<mod>> : true_type {};
template <int id>
struct ordinary_mod32<dynamic_modint32<id>> : true_type {};
}
namespace internal
{
template <class S>
using dot_product_mod32_value = ordinary_mod32<S>;
}
namespace internal
{
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;
if (m == 1107296257)
return 10;
if (m == 1711276033)
return 29;
if (m == 1811939329)
return 13;
if (m == 2013265921)
return 31;
if (m == 2113929217)
return 5;
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 (powmod_constexpr(g, (m - 1) / divs[i], m) == 1)
{
ok = false;
break;
}
}
if (ok)
return g;
}
}
template <int m>
constexpr int primitive_root_for_convolution = primitive_root_constexpr(m);
template <class mint, int g = internal::primitive_root_for_convolution<mint::mod()>>
struct fft_info
{
static constexpr int rank2 = countr_zero(mint::mod() - 1);
std::array<mint, std::max(3, rank2 + 1)> root;
std::array<mint, std::max(3, rank2 + 1)> iroot;
std::array<mint, std::max(1, rank2 - 2 + 1)> rate2;
std::array<mint, std::max(1, rank2 - 2 + 1)> irate2;
std::array<mint, std::max(1, rank2 - 3 + 1)> rate3;
std::array<mint, std::max(1, 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>
bool ntt_ok(int n)
{
if (n <= 0)
return false;
if constexpr (is_static_modint_v<mint>)
{
if constexpr (!internal::isprime<mint::mod()>)
return false;
static constexpr int rank2 = countr_zero(mint::mod() - 1);
return n <= (1 << rank2);
}
else
return false;
}
template <int id>
void ntt(vc<dynamic_modint32<id>> &) { assert(false); }
template <auto mod>
void ntt(vc<internal::modint_impl<internal::policy_static<mod>>> &a)
{
using mint = internal::modint_impl<internal::policy_static<mod>>;
int n = int(a.size());
assert(n > 0);
int h = countr_zero((unsigned int)n);
assert(n == (1 << h));
assert(ntt_ok<mint>(n));
static const internal::fft_info<mint> info;
int len = 0;
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
{
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 <auto mod>
void intt(vc<internal::modint_impl<internal::policy_static<mod>>> &a)
{
using mint = internal::modint_impl<internal::policy_static<mod>>;
int n = int(a.size());
assert(n > 0);
int h = countr_zero((unsigned int)n);
assert(n == (1 << h));
assert(ntt_ok<mint>(n));
static const internal::fft_info<mint> info;
int len = h;
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)mint::mod() + l.val() - (uint)r.val()) *
irot.val();
;
}
if (s + 1 != (1 << (len - 1)))
irot *= info.irate2[countr_zero(~(unsigned int)(s))];
}
len--;
}
else
{
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;
}
}
}
namespace internal
{
template <class mint>
vc<mint> convolution_naive(const vc<mint> &a, const vc<mint> &b)
{
const int n = a.size(), m = b.size();
const int cnta = n - count(ALL(a), 0), cntb = m - count(ALL(b), 0);
vc<mint> c(n + m - 1);
if constexpr (dot_product_mod32_value<mint>::value)
{
if (min(n, m) >= 16 && ll(cnta) * 2 >= n && ll(cntb) * 2 >= m)
{
repi(p, n + m - 1) c[p] = convolution_point_get(a, b, p);
return c;
}
}
if ((ll)m * cnta > (ll)n * cntb)
{
repi(j, m)
{
if (b[j] == 0)
continue;
repi(i, n) c[i + j] += a[i] * b[j];
}
}
else
{
repi(i, n)
{
if (a[i] == 0)
continue;
repi(j, m) c[i + j] += a[i] * b[j];
}
}
return c;
}
template <class mint>
vc<mint> convolution_ntt(vc<mint> a, vc<mint> b)
{
const int n = a.size(), m = b.size();
const int z = bit_ceil(n + m - 1);
if (a == b)
{
a.resize(z);
ntt(a);
repi(i, z) a[i] *= a[i];
}
else
{
a.resize(z), b.resize(z);
ntt(a), ntt(b);
repi(i, z) a[i] *= b[i];
}
intt(a);
mint iz = mint(z).inv();
fem(ai : a) ai *= iz;
a.resize(n + m - 1);
return a;
}
template <size_t j, int mod, class T, size_t k>
void convolution_crt_helper(const vc<T> &a, const vc<T> &b, vc<array<T, k>> &cs)
{
using mint = static_modint32<mod>;
const int n = a.size(), m = b.size();
auto c = convolution_ntt(vc<mint>(ALL(a)), vc<mint>(ALL(b)));
repi(i, n + m - 1) cs[i][j] = c[i].val();
}
template <int ...ms, class T>
vc<T> convolution_crt(const vc<T> &a, const vc<T> &b)
{
const int n = a.size(), m = b.size();
constexpr size_t k = sizeof...(ms);
vc<array<T, k>> cs(n + m - 1);
constexpr array<int, k> ms_arr = {ms...};
[&]<size_t... Is>(index_sequence<Is...>)
{
(convolution_crt_helper<Is, ms_arr[Is], T, k>(a, b, cs), ...);
}(make_index_sequence<k>{});
vc<T> c(n + m - 1);
repi(i, n + m - 1) c[i] = get<1>(crt(cs[i], ms_arr));
return c;
}
}
vc<ll> convolution_4e18(const vc<ll> &a, const vc<ll> &b)
{
if (a.empty() || b.empty())
return {};
const int n = a.size(), m = b.size();
const int cnta = n - count(ALL(a), 0), cntb = m - count(ALL(b), 0);
int z = bit_ceil(n + m - 1);
ll naive_work = min(ll(cnta) * m, ll(cntb) * n);
ll ntt_work = ll(z) * max(1, int(countr_zero((unsigned)z)));
if (naive_work <= 18 * ntt_work)
return internal::convolution_naive(a, b);
return internal::convolution_crt<2013265921, 2113929217>(a, b);
}
void main2()
{
STR(S);
ll N = S.size();
vl A = stov(S, 'a');
PowerTable<mint> pw2(2);
mint ans = 0;
rep(len, 1, N + 1) ans += len * bi::C(N, len);
GroupIndex grp(A);
rep(ch, grp.value_bound())
{
auto is = grp.idxs(ch);
mint sm = 0;
rep(k, SZ(is))
{
ans -= sm * pw2.pow(N - 1 - is[k]);
sm += pw2.pow(is[k]);
}
}
PRINT(ans);
}
void test()
{
}
// https://github.com/miscalculation53/library/tree/wip/template/template_main.hpp
template <auto init, auto main2, auto test>
struct Main
{
Main()
{
cauto CERR = [](string val, string color)
{
string s = "\033[" + color + "m" + val + "\033[m";
};
CERR("\n[FAST_IO]\n\n", "32");
cout << fixed << setprecision(20);
init();
CERR("\n[SINGLE_TESTCASE]\n\n", "36");
main2();
}
};
Main<init, main2, test> main_dummy;
int main() {}
miscalc