結果
| 問題 |
No.310 2文字しりとり
|
| コンテスト | |
| ユーザー |
miscalc
|
| 提出日時 | 2025-07-22 05:35:57 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 49,208 bytes |
| コンパイル時間 | 4,971 ms |
| コンパイル使用メモリ | 341,720 KB |
| 実行使用メモリ | 814,400 KB |
| 最終ジャッジ日時 | 2025-07-22 05:36:04 |
| 合計ジャッジ時間 | 7,443 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 MLE * 1 -- * 6 |
ソースコード
#define INF 4'000'000'000'000'000'037LL
#define EPS 1e-11
#include <bits/stdc++.h>
using namespace std;
namespace {
using ld = decltype(EPS);
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using tlll = tuple<ll, ll, ll>;
using tllll = tuple<ll, ll, ll, ll>;
#define vc vector
template <class T>
using vvc = vc<vc<T>>;
using vpll = vc<pll>;
using vstr = vc<string>;
#ifdef __SIZEOF_INT128__
using i128 = __int128_t;
using u128 = __uint128_t;
#endif
#define cauto const auto
#define overload4(_1, _2, _3, _4, name, ...) name
#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__)
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>
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>
inline constexpr T safemod(U a, V b) { return T(a) - T(b) * divfloor<T>(a, b); }
template <class T = ll, class U, class V>
constexpr T ipow(U a, V b)
{
assert(b >= 0);
if (b == 0)
return 1;
if (a == 0 || a == 1)
return a;
if (a < 0 && a == -1)
return b & 1 ? -1 : 1;
T res = 1, tmp = a;
while (true)
{
if (b & 1)
res *= tmp;
b >>= 1;
if (b == 0)
break;
tmp *= tmp;
}
return res;
}
template <class T = ll, class A, class B, class M>
T mul_limited(A a, B b, M m)
{
assert(a >= 0 && b >= 0 && m >= 0);
if (b == 0)
return 0;
return T(a) > T(m) / T(b) ? T(m) : T(a) * T(b);
}
template <class T = ll, class A, class B>
T mul_limited(A a, B b) { return mul_limited<T>(a, b, INF); }
template <class T = ll, class A, class B, class M>
T pow_limited(A a, B b, M m)
{
assert(a >= 0 && b >= 0 && m >= 0);
if (a <= 1 || b == 0)
return min(ipow<T>(a, b), T(m));
T res = 1, tmp = a;
while (true)
{
if (b & 1)
{
if (res > T(m) / tmp)
return m;
res *= tmp;
}
b >>= 1;
if (b == 0)
break;
if (tmp > T(m) / tmp)
return m;
tmp *= tmp;
}
return res;
}
template <class T = ll, class A, class B>
T pow_limited(A a, B b) { return pow_limited<T>(a, b, INF); }
template <class T = ll, class U, class V>
vc<T> base_repr(U val, V base)
{
assert(val >= 0);
assert(base >= 2);
if (val == 0)
return {0};
vc<T> a;
while (val > 0)
{
a.emplace_back(val % base);
val /= base;
}
reverse(a.begin(), a.end());
return a;
}
template <class T = ll, class U, class V>
vc<T> base_repr(U val, V base, int n)
{
assert(val >= 0);
assert(base >= 2);
assert(n >= 0);
vc<T> a(n);
repi(i, n)
{
a[i] = val % base;
val /= base;
}
reverse(a.begin(), a.end());
return a;
}
#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) ([&](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;
}
template <class T, size_t d, size_t i = 0, class V>
auto dvec(const V (&sz)[d], const T &init)
{
if constexpr (i < d)
return vc(sz[i], dvec<T, d, i + 1>(sz, init));
else
return init;
}
template <class T = ll>
T ctol(const char &c, const string &s)
{
repi(i, SZ<int>(s)) if (s[i] == c) return i;
return -1;
}
template <class T, class... Ts>
vc<T> concat(vc<T> v, const vc<Ts> &...vs)
{
(v.insert(v.end(), ALL(vs)), ...);
return v;
}
template <class V>
auto SUM(const V &v)
{
typename V::value_type s{};
fec(vi : v) s += vi;
return s;
}
template <class T, class V>
T SUM(const V &v)
{
T s{};
fec(vi : v) s += vi;
return s;
}
template <class T, class U>
vc<T> permuted(const vc<T> &a, const vc<U> &p)
{
const int n = p.size();
vc<T> res(n);
repi(i, n)
{
assert(0 <= p[i] && p[i] < U(a.size()));
res[i] = a[p[i]];
}
return res;
}
template <class T, class U, class... Ts>
vc<T> permuted(const vc<T> &p, const vc<U> &q, const vc<Ts> &...rs)
{
return permuted(permuted(p, q), rs...);
}
template <class V>
V reversed(const V &v) { return V(v.rbegin(), v.rend()); }
#if __cplusplus < 202002L
#else
#endif
template <class V>
void unique(V &v) { v.erase(std::unique(ALL(v)), v.end()); }
template <class V, class U>
void rotate(V &v, U k)
{
const U n = v.size();
k = (k % n + n) % n;
std::rotate(v.begin(), v.begin() + k, v.end());
}
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;
}
template <class T>
struct MonoidAdd
{
using S = T;
static constexpr S op(S a, S b) { return a + b; }
static constexpr S e() { return 0; }
};
template <class T, const T infty = INF>
struct MonoidMin
{
using S = T;
static constexpr S op(S a, S b) { return min(a, b); }
static constexpr S e() { return infty; }
};
template <class T, const T infty = INF>
struct MonoidMax
{
using S = T;
static constexpr S op(S a, S b) { return max(a, b); }
static constexpr S e() { return -infty; }
};
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); }
const vpll DRULgrid = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const vpll DRULplane = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
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;
#if __cplusplus < 202002L
struct identity
{
template <class T>
constexpr T &&operator()(T &&t) const noexcept
{ return forward<T>(t); }
};
namespace internal
{
template <class T = ll, class V, class Judge>
inline T bound_helper(const V &v, Judge judge)
{
int l = -1, r = v.size();
while (r - l > 1)
{
int m = (l + r) / 2;
if (judge(m))
l = m;
else
r = m;
}
return r;
}
};
template <class T = ll, class V, class Value, class Comp = less<>, class Proj = identity>
inline T LB(const V &v, const Value &val, Comp comp = {}, Proj proj = {})
{
return internal::bound_helper(v, [&](int i) -> bool
{ return comp(proj(*(v.begin() + i)), val); });
}
#define DEFAULT_COMP less<>
#else
template <class T = ll, class V, class Value, class Comp = ranges::less, class Proj = identity>
inline T LB(const V &v, const Value &val, Comp comp = {}, Proj proj = {})
{ return ranges::lower_bound(v, val, comp, proj) - v.begin(); }
template <class T = ll, class V, class Value, class Comp = ranges::less, class Proj = identity>
inline T UB(const V &v, const Value &val, Comp comp = {}, Proj proj = {})
{ return ranges::upper_bound(v, val, comp, proj) - v.begin(); }
#define DEFAULT_COMP ranges::less
#endif
template <class T>
inline constexpr ull MASK(T k) { return (1ULL << k) - 1ULL; }
#if __cplusplus < 202002L
inline constexpr ull bit_width(ull x) { return x == 0 ? 0 : 64 - __builtin_clzll(x); }
inline constexpr ull countr_zero(ull x) { assert(x != 0); return __builtin_ctzll(x); }
inline constexpr ull popcount(ull x) { return __builtin_popcountll(x); }
#else
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); }
#endif
#define dump(...)
#define oj(...) __VA_ARGS__
namespace fastio {
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(string &x) {
x.clear();
char c;
do {
if (pil + 1 > pir) load();
c = ibuf[pil++];
} while (isspace(c));
do {
x += c;
if (pil == pir) load();
c = ibuf[pil++];
} while (!isspace(c));
}
template <typename T>
void rd1_integer(T &x) {
if (pil + 100 > pir) load();
char c;
do
c = ibuf[pil++];
while (c < '-');
bool minus = 0;
if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
if (c == '-') { minus = 1, c = ibuf[pil++]; }
}
x = 0;
while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; }
if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
if (minus) x = -x;
}
}
void rd1(ll &x) { rd1_integer(x); }
template <class T, class U>
void rd1(pair<T, U> &p) {
return rd1(p.first), rd1(p.second);
}
template <size_t N = 0, typename T>
void rd1_tuple(T &t) {
if constexpr (N < std::tuple_size<T>::value) {
auto &x = std::get<N>(t);
rd1(x);
rd1_tuple<N + 1>(t);
}
}
template <class... T>
void rd1(tuple<T...> &tpl) {
rd1_tuple(tpl);
}
template <size_t N = 0, typename T>
void rd1(array<T, N> &x) {
for (auto &d: x) rd1(d);
}
template <class T>
void rd1(vc<T> &x) {
for (auto &d: x) rd1(d);
}
void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
rd1(h), read(t...);
}
void wt1(const char c) {
if (por == SIZ) flush();
obuf[por++] = c;
}
void wt1(const string s) {
for (char c: s) wt1(c);
}
template <typename T>
void wt1_integer(T x) {
if (por > SIZ - 100) flush();
if (x < 0) { obuf[por++] = '-', x = -x; }
int outi;
for (outi = 96; x >= 10000; outi -= 4) {
memcpy(out + outi, pre.num[x % 10000], 4);
x /= 10000;
}
if (x >= 1000) {
memcpy(obuf + por, pre.num[x], 4);
por += 4;
} else if (x >= 100) {
memcpy(obuf + por, pre.num[x] + 1, 3);
por += 3;
} else if (x >= 10) {
int q = (x * 103) >> 10;
obuf[por] = q | '0';
obuf[por + 1] = (x - q * 10) | '0';
por += 2;
} else
obuf[por++] = x | '0';
memcpy(obuf + por, out + outi + 4, 96 - outi);
por += 96 - outi;
}
void wt1(int x) { wt1_integer(x); }
template <class T, enable_if_t<is_integral_v<T>, int> = 0>
void wt1(T x) { wt1_integer(x); }
template <class T, class U>
void wt1(const pair<T, U> &val) {
wt1(val.first);
wt1(' ');
wt1(val.second);
}
template <size_t N = 0, typename T>
void wt1_tuple(const T &t) {
if constexpr (N < std::tuple_size<T>::value) {
if constexpr (N > 0) { wt1(' '); }
const auto x = std::get<N>(t);
wt1(x);
wt1_tuple<N + 1>(t);
}
}
template <class... T>
void wt1(const tuple<T...> &tpl) {
wt1_tuple(tpl);
}
template <class T, size_t S>
void wt1(const array<T, S> &val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt1(' ');
wt1(val[i]);
}
}
template <class T>
void wt1(const vector<T> &val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt1(' ');
wt1(val[i]);
}
}
void print() { wt1('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
wt1(head);
if (sizeof...(Tail)) wt1(' ');
print(forward<Tail>(tail)...);
}
} // namespace fastio
struct Dummy {
Dummy() { atexit(fastio::flush); }
} dummy;
namespace internal
{
template <class... Ts>
void READnodump(Ts &...a) { fastio::read(a...); }
template <class T>
void READVECnodump(int n, vc<T> &v)
{
v.resize(n);
READnodump(v);
}
template <class T, class... Ts>
void READVECnodump(int n, vc<T> &v, vc<Ts> &...vs)
{ READVECnodump(n, v), READVECnodump(n, vs...); }
template <class T>
void READVEC2nodump(int n, int m, vvc<T> &v)
{
v.assign(n, vc<T>(m));
READnodump(v);
}
template <class T, class... Ts>
void READVEC2nodump(int n, int m, vvc<T> &v, vvc<Ts> &...vs)
{ READVEC2nodump(n, m, v), READVEC2nodump(n, m, vs...); }
template <class T>
void READJAGnodump(int n, vvc<T> &v)
{
v.resize(n);
repi(i, n)
{
int k;
READnodump(k);
READVECnodump(k, v[i]);
}
}
template <class T, class... Ts>
void READJAGnodump(int n, vvc<T> &v, vvc<Ts> &...vs)
{ READJAGnodump(n, v), READJAGnodump(n, vs...); }
}; // namespace internal
#define READ(...) internal::READnodump(__VA_ARGS__); dump(__VA_ARGS__)
#define IN(T, ...) T __VA_ARGS__; READ(__VA_ARGS__)
#define LL(...) IN(ll, __VA_ARGS__)
#define READVEC(...) internal::READVECnodump(__VA_ARGS__); dump(__VA_ARGS__)
#define VEC(T, n, ...) vc<T> __VA_ARGS__; READVEC(n, __VA_ARGS__)
#define PRINT fastio::print
template <class T, class U, class P>
pair<T, U> operator+=(pair<T, U> &a, const P &b)
{
a.first += b.first;
a.second += b.second;
return a;
}
template <class T, class U, class P>
pair<T, U> operator+(pair<T, U> &a, const P &b) { return a += b; }
template <class T, size_t n, class A>
array<T, n> operator+=(array<T, n> &a, const A &b)
{
for (size_t i = 0; i < n; i++)
a[i] += b[i];
return a;
}
template <class T, size_t n, class A>
array<T, n> operator+(array<T, n> &a, const A &b) { return a += b; }
namespace internal
{
template <size_t... I, class A, class B>
auto tuple_add_impl(A &a, const B &b, const index_sequence<I...>)
{
((get<I>(a) += get<I>(b)), ...);
return a;
}
}; // namespace internal
template <class... Ts, class Tp>
tuple<Ts...> operator+=(tuple<Ts...> &a, const Tp &b)
{ return internal::tuple_add_impl(a, b, make_index_sequence<tuple_size_v<tuple<Ts...>>>{}); }
template <class... Ts, class Tp>
tuple<Ts...> operator+(tuple<Ts...> &a, const Tp &b) { return a += b; }
template <class T, class Add>
void offset(vc<T> &v, const Add &add) { for (auto &vi : v) vi += add; }
template <class T, class Add>
void offset(vvc<T> &v, const Add &add) { for (auto &vi : v) for (auto &vij : vi) vij += add; }
template <class T, const size_t m>
array<vc<T>, m> top(const vc<array<T, m>> &vt)
{
const size_t n = vt.size();
array<vc<T>, m> tv;
tv.fill(vc<T>(n));
for (size_t i = 0; i < n; i++)
for (size_t j = 0; j < m; j++)
tv[j][i] = vt[i][j];
return tv;
}
template <class T, const size_t m>
vc<array<T, m>> top(const array<vc<T>, m> &tv)
{
if (tv.empty()) return {};
const size_t n = tv[0].size();
vc<array<T, m>> vt(n);
for (size_t j = 0; j < m; j++)
{
assert(tv[j].size() == n);
for (size_t i = 0; i < n; i++)
vt[i][j] = tv[j][i];
}
return vt;
}
template <class T, class U>
pair<vc<T>, vc<U>> top(const vc<pair<T, U>> &vt)
{
const size_t n = vt.size();
pair<vc<T>, vc<U>> tv;
tv.first.resize(n), tv.second.resize(n);
for (size_t i = 0; i < n; i++)
tie(tv.first[i], tv.second[i]) = vt[i];
return tv;
}
template <class T, class U>
vc<pair<T, U>> top(const pair<vc<T>, vc<U>> &tv)
{
const size_t n = tv.first.size();
assert(n == tv.second.size());
vc<pair<T, U>> vt(n);
for (size_t i = 0; i < n; i++)
vt[i] = make_pair(tv.first[i], tv.second[i]);
return vt;
}
namespace internal
{
template <size_t... I, class V, class Tp>
auto vt_to_tv_impl(V &tv, const Tp &t, index_sequence<I...>, size_t index)
{ ((get<I>(tv)[index] = get<I>(t)), ...); }
template <size_t... I, class Tp>
auto tv_to_vt_impl(const Tp &tv, index_sequence<I...>, size_t index)
{ return make_tuple(get<I>(tv)[index]...); }
};
template <class... Ts>
auto top(const vc<tuple<Ts...>> &vt)
{
const size_t n = vt.size();
tuple<vc<Ts>...> tv;
apply([&](auto &...v)
{ ((v.resize(n)), ...); }, tv);
for (size_t i = 0; i < n; i++)
internal::vt_to_tv_impl(tv, vt[i], make_index_sequence<tuple_size_v<decltype(tv)>>{}, i);
return tv;
}
template <class... Ts>
auto top(const tuple<vc<Ts>...> &tv)
{
size_t n = get<0>(tv).size();
apply([&](auto &...v)
{ ((assert(v.size() == n)), ...); }, tv);
vc<tuple<Ts...>> vt(n);
for (size_t i = 0; i < n; i++)
vt[i] = internal::tv_to_vt_impl(tv, index_sequence_for<Ts...>{}, i);
return vt;
}
mt19937_64 mt;
template <class T = ll, class U1, class U2>
T randrange(U1 l, U2 r)
{
assert(T(l) < T(r));
return T(l) + mt() % (T(r) - T(l));
}
namespace internal
{
constexpr ll powmod32_constexpr(ll x, ll n, int m)
{
if (m == 1)
return 0;
uint _m = (uint)m;
ull r = 1;
ull y = safemod(x, m);
while (n)
{
if (n & 1)
r = (r * y) % _m;
y = (y * y) % _m;
n >>= 1;
}
return r;
}
constexpr bool isprime32_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;
constexpr ll bases[3] = {2, 7, 61};
for (ll a : bases)
{
ll t = d;
ll y = powmod32_constexpr(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1)
{
y = y * y % n;
t <<= 1;
}
if (y != n - 1 && t % 2 == 0)
return false;
}
return true;
}
template <int n>
constexpr bool isprime32 = isprime32_constexpr(n);
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));
}
};
}
namespace internal
{
#define REF static_cast<mint &>(*this)
#define CREF static_cast<const mint &>(*this)
#define VAL *static_cast<const mint *>(this)
template <class mint>
struct modint_base
{
mint &operator+=(const mint &rhs)
{
mint &self = REF;
self._v += rhs._v;
if (self._v >= self.umod())
self._v -= self.umod();
return self;
}
mint &operator-=(const mint &rhs)
{
mint &self = REF;
self._v -= rhs._v;
if (self._v >= self.umod())
self._v += self.umod();
return self;
}
mint &operator/=(const mint &rhs)
{
mint &self = REF;
return self = self * rhs.inv();
}
mint &operator++()
{
mint &self = REF;
self._v++;
if (self._v == self.umod())
self._v = 0;
return self;
}
mint &operator--()
{
mint &self = REF;
if (self._v == 0)
self._v = self.umod();
self._v--;
return self;
}
mint operator++(int)
{
mint res = VAL;
++REF;
return res;
}
mint operator--(int)
{
mint res = VAL;
--REF;
return res;
}
mint operator+() const { return VAL; }
mint operator-() const { return mint() - VAL; }
mint pow(ll n) const
{
assert(n >= 0);
mint x = VAL, r = 1;
while (n)
{
if (n & 1)
r *= x;
x *= x;
n >>= 1;
}
return r;
}
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 mint(lhs).eq(rhs); }
friend bool operator!=(const mint &lhs, const mint &rhs)
{ return mint(lhs).neq(rhs); }
private:
bool eq(const mint &rhs) { return REF._v == rhs._v; }
bool neq(const mint &rhs) { return REF._v != rhs._v; }
};
}
template <typename T, std::enable_if_t<std::is_base_of_v<internal::modint_base<T>, T>, int> = 0>
void rd1(T &x)
{
ll a;
fastio::rd1(a);
x = a;
}
template <typename T, std::enable_if_t<std::is_base_of_v<internal::modint_base<T>, T>, int> = 0>
void wt1(const T &x) { fastio::wt1(x.val()); }
template <class T = ll>
constexpr tuple<T, T, T> extgcd(const T &a, const 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};
}
template <int m>
struct static_modint : internal::modint_base<static_modint<m>>
{
using mint = static_modint;
private:
friend struct internal::modint_base<static_modint<m>>;
uint _v;
static constexpr uint umod() { return m; }
static constexpr bool prime = internal::isprime32<m>;
public:
static constexpr int mod() { return m; }
static mint raw(int v)
{
mint x;
x._v = v;
return x;
}
static_modint() : _v(0) {}
template <class T>
static_modint(T v)
{
if constexpr (is_signed_v<T>)
{
ll x = (ll)(v % (ll)(umod()));
if (x < 0)
x += umod();
_v = (uint)x;
}
else if constexpr (is_unsigned_v<T>)
{
_v = (uint)(v % umod());
}
else
{
static_assert(is_signed_v<T> || is_unsigned_v<T>, "Unsupported Type");
}
}
int val() const { return (int)_v; }
mint& operator*=(const mint &rhs)
{
ull z = _v;
z *= rhs._v;
_v = (uint)(z % umod());
return *this;
}
mint inv() const
{
if (prime)
{
assert(_v != 0);
return CREF.pow(umod() - 2);
}
else
{
auto [g, x, y] = extgcd<int>(_v, m);
assert(g == 1);
return x;
}
}
};
template <int id>
struct dynamic_modint : internal::modint_base<dynamic_modint<id>>
{
using mint = dynamic_modint;
private:
friend struct internal::modint_base<dynamic_modint<id>>;
uint _v;
static internal::barrett32 bt;
static uint umod() { return bt.umod(); }
public:
static int mod() { return (int)(bt.umod()); }
static mint raw(int v)
{
mint x;
x._v = v;
return x;
}
dynamic_modint() : _v(0) {}
template <class T>
dynamic_modint(T v)
{
if constexpr (is_signed_v<T>)
{
ll x = (ll)(v % (ll)(umod()));
if (x < 0)
x += umod();
_v = (uint)x;
}
else if constexpr (is_unsigned_v<T>)
{
_v = (uint)(v % umod());
}
else
{
static_assert(is_signed_v<T> || is_unsigned_v<T>, "Unsupported Type");
}
}
int val() const { return (int)_v; }
mint& operator*=(const mint &rhs)
{
_v = bt.mul(_v, rhs._v);
return *this;
}
mint inv() const
{
auto [g, x, y] = extgcd<int>(_v, mod());
assert(g == 1);
return x;
}
};
template <int id>
internal::barrett32 dynamic_modint<id>::bt(998244353);
using modint1000000007 = static_modint<1000000007>;
template <class T>
struct is_static_modint : false_type {};
template <int m>
struct is_static_modint<static_modint<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_modint<id>> : true_type {};
template <class T>
inline constexpr bool is_dynamic_modint_v = is_dynamic_modint<T>::value;
template <class T>
inline constexpr bool is_modint_v = is_static_modint_v<T> || is_dynamic_modint_v<T>;
using mint = modint1000000007;
template <class T>
struct CSR
{
protected:
int n, m;
vc<int> start;
vc<T> elist;
vc<int> eid_to_elistid;
struct Row
{
using iterator = typename vc<T>::const_iterator;
private:
iterator begi, endi;
public:
Row(const iterator &begi, const iterator &endi) : begi(begi), endi(endi) {}
inline iterator begin() const { return begi; }
inline iterator end() const { return endi; }
template <class I = ll>
inline I size() const { return endi - begi; }
inline bool empty() const { return size() == 0; }
inline T operator[](int i) const { return *(begi + i); }
inline T at(int i) const
{
assert(0 <= i && i < size());
return *(begi + i);
}
inline T front() const
{
assert(!empty());
return *begi;
}
inline T back() const
{
assert(!empty());
return *prev(endi);
}
};
public:
CSR() {}
template <class I>
CSR(int n, const vc<pair<I, T>> &ies) : n(n), m(ies.size()), start(n, 0), elist(m), eid_to_elistid(m)
{
fec([ i, e ] : ies)
{
assert(0 <= i && i < n);
start[i]++;
}
start = cumlsum(start);
auto cnt = start;
repi(j, m)
{
cauto &[i, e] = ies[j];
int &k = cnt[i];
elist[k] = e;
eid_to_elistid[j] = k;
k++;
}
}
CSR(const vvc<T> &vv) : n(vv.size()), start(n + 1, 0)
{
m = 0;
fec(row : vv) m += row.size();
elist.resize(m);
eid_to_elistid.resize(m);
int k = 0;
for (int i = 0, j = 0; i < n; i++)
{
start[i] = k;
fec(e : vv[i])
{
elist[k] = e;
eid_to_elistid[j++] = k;
k++;
}
}
start.back() = m;
}
Row operator[](int i) const { return Row(elist.begin() + start[i], elist.begin() + start[i + 1]); }
Row at(int i) const
{
if (!(0 <= i && i < n))
return Row(elist.begin(), elist.begin());
return Row(elist.begin() + start[i], elist.begin() + start[i + 1]);
}
template <class I = ll>
I size() const { return n; }
const T &find_by_eid(int eid) const
{
assert(0 <= eid && eid < m);
return elist[eid_to_elistid[eid]];
}
};
template <class Cost>
struct Edge
{
int from, to;
Cost cost;
int index;
Edge() : from(-1), to(-1), index(-1) {}
Edge(int s, int t, Cost c, int i = -1) : from(s), to(t), cost(c), index(i) {}
operator int() const { return to; }
bool operator<(const Edge &rhs) const { return cost < rhs.cost; }
Edge rev() const { return Edge(to, from, cost, index); }
};
template <bool is_directed, class Cost>
struct Graph
{
using E = Edge<Cost>;
protected:
int n, m;
CSR<E> g;
public:
Graph() {}
template <class I>
Graph(int n, const vc<pair<I, I>> &es, const Cost &dflt_cost = 1) : n(n), m(es.size())
{
if constexpr (is_directed)
{
vc<pair<int, E>> edges(m);
repi(i, m)
{
auto [u, v] = es[i];
assert(0 <= u && u < n);
assert(0 <= v && v < n);
edges[i] = {u, E(u, v, dflt_cost, i)};
}
g = CSR<E>(n, edges);
}
else
{
vc<pair<int, E>> edges(2 * m);
repi(i, m)
{
auto [u, v] = es[i];
assert(0 <= u && u < n);
assert(0 <= v && v < n);
edges[2 * i] = {u, E(u, v, dflt_cost, i)};
edges[2 * i + 1] = {v, E(v, u, dflt_cost, i)};
}
g = CSR<E>(n, edges);
}
}
template <class I>
Graph(int n, const vc<tuple<I, I, Cost>> &es) : n(n), m(es.size())
{
if constexpr (is_directed)
{
vc<pair<int, E>> edges(m);
repi(i, m)
{
auto [u, v, w] = es[i];
assert(0 <= u && u < n);
assert(0 <= v && v < n);
edges[i] = {u, E(u, v, w, i)};
}
g = CSR<E>(n, edges);
}
else
{
vc<pair<int, E>> edges(2 * m);
repi(i, m)
{
auto [u, v, w] = es[i];
assert(0 <= u && u < n);
assert(0 <= v && v < n);
edges[2 * i] = {u, E(u, v, w, i)};
edges[2 * i + 1] = {v, E(v, u, w, i)};
}
g = CSR<E>(n, edges);
}
}
template <class I = ll>
I size() const { return n; }
auto out_edges(int v) const { return g[v]; }
E get_edge(int eid) const
{
if constexpr (is_directed)
return g.find_by_eid(eid);
else
{
E e = g.find_by_eid(eid * 2);
return e.from > e.to ? e.rev() : e;
}
}
};
template <class Cost>
using GraphDirected = Graph<true, Cost>;
template <class Cost>
using GraphUndirected = Graph<false, Cost>;
template <class T>
struct MyQueue
{
private:
vc<T> d;
int pos = 0;
public:
void reserve(int n) { d.reserve(n); }
template <class I = ll>
I size() const { return SZ<I>(d) - pos; }
bool empty() const { return pos == SZ<int>(d); }
void push(const T &t) { d.eb(t); }
T front() const { return d[pos]; }
T &front() { return d[pos]; }
void clear()
{
d.clear();
pos = 0;
}
void pop() { pos++; }
T operator[](int i) const { return d[pos + i]; }
T &operator[](int i) { return d[pos + i]; }
T at(int i) const
{
assert(0 <= i && i < size<int>());
return d[pos + i];
}
T &at(int i)
{
assert(0 <= i && i < size<int>());
return d[pos + i];
}
vc<T> content() { return {d.begin() + pos, d.end()}; }
};
template <class I = ll, class Cost>
vc<I> connected_component_ids(const GraphUndirected<Cost> &g)
{
const int n = g.size();
vc<I> res(n, -1);
int id = 0;
MyQueue<int> que;
repi(sv, n)
{
if (res[sv] != -1)
continue;
res[sv] = id;
que.clear();
que.push(sv);
while (!que.empty())
{
int v = que.front();
que.pop();
fe(nv : g.out_edges(v))
{
if (res[nv] != -1)
continue;
res[nv] = id;
que.push(nv);
}
}
id++;
}
return res;
}
template <class S_, auto op_, auto e_>
struct Monoid
{
using S = S_;
static constexpr auto op = op_;
static constexpr auto e = e_;
};
template <class S_, auto op_, auto e_, auto inv_>
struct Group
{
using S = S_;
static constexpr auto op = op_;
static constexpr auto e = e_;
static constexpr auto inv = inv_;
};
template <class Madd, class Mmul>
struct SemiRingFromMonoidMonoid
{
static_assert(is_same_v<typename Madd::S, typename Mmul::S>, "Madd::S and Mmul::S must be identical");
using S = typename Madd::S;
static constexpr auto add = Madd::op;
static constexpr auto e0 = Madd::e;
static constexpr auto mul = Mmul::op;
static constexpr auto e1 = Mmul::e;
};
template <class Gadd, class Mmul>
struct RingFromGroupMonoid
{
static_assert(is_same_v<typename Gadd::S, typename Mmul::S>, "Gadd::S and Mmul::S must be identical");
using S = typename Gadd::S;
static constexpr auto add = Gadd::op;
static constexpr auto e0 = Gadd::e;
static constexpr auto minus = Gadd::inv;
static constexpr auto mul = Mmul::op;
static constexpr auto e1 = Mmul::e;
};
template <class Gadd, class Gmul>
struct FieldFromGroupGroup
{
static_assert(is_same_v<typename Gadd::S, typename Gmul::S>, "Gadd::S and Gmul::S must be identical");
using S = typename Gadd::S;
static constexpr auto add = Gadd::op;
static constexpr auto e0 = Gadd::e;
static constexpr auto minus = Gadd::inv;
static constexpr auto mul = Gmul::op;
static constexpr auto e1 = Gmul::e;
static constexpr auto inv = Gmul::inv;
};
template <class T>
struct MonoidMul
{
using S = T;
static constexpr S op(S a, S b) { return a * b; }
static constexpr S e() { return 1; }
};
template <class T>
struct GroupAddSub
{
using S = T;
static constexpr S op(S a, S b) { return a + b; }
static constexpr S e() { return S(0); }
static constexpr S inv(S a) { return -a; }
};
template <class T>
struct GroupMulDiv
{
using S = T;
static constexpr S op(S a, S b) { return a * b; }
static constexpr S e() { return S(1); }
static constexpr S inv(S a) { return S(1) / a; }
};
template <class T>
using FieldAddSubMulDiv = FieldFromGroupGroup<GroupAddSub<T>, GroupMulDiv<T>>;
template <class F>
vc<typename F::S> berlekamp_massey(const vc<typename F::S> &a)
{
using S = typename F::S;
const int n = a.size();
vc<S> b, c;
int pos = -1;
S x = F::e0();
repi(i, n)
{
const int d = c.size();
S y = a[i];
repi(j, d) y = F::add(y, F::minus(F::mul(c[j], a[i - 1 - j])));
if (y == F::e0())
continue;
if (c.empty())
{
c.assign(i + 1, F::e0());
pos = i;
x = y;
continue;
}
S z = F::mul(y, F::inv(x));
int d2 = i - pos + b.size();
vc<S> tmp;
if (d2 >= d)
{
tmp = c;
c.resize(d2, F::e0());
}
c[i - 1 - pos] = F::add(c[i - 1 - pos], z);
repi(j, b.size()) c[i - pos + j] = F::add(c[i - pos + j], F::minus(F::mul(z, b[j])));
if (d2 >= d)
pos = i, x = y, b = tmp;
}
c.insert(c.begin(), F::minus(F::e1()));
return c;
}
template <class mint>
mint random_sample_mint() { return randrange(1, mint::mod()); }
template <class F, class RandomSample = decltype(random_sample_mint<typename F::S>)>
vc<typename F::S> minimal_polynomial_of_vector_sequence(const vvc<typename F::S> &vs, const RandomSample &random_sample = random_sample_mint)
{
using S = typename F::S;
const int n = vs.size();
assert(n > 0);
const int m = vs[0].size();
fec(v : vs) assert(SZ(v) == m);
vc<S> u(m, F::e0());
repi(j, m) u[j] = random_sample();
vc<S> a(n, F::e0());
repi(i, n) repi(j, m) a[i] = F::add(a[i], F::mul(u[j], vs[i][j]));
return reversed(berlekamp_massey<F>(a));
}
template <class F, class LinearMap, class RandomSample = decltype(random_sample_mint<typename F::S>)>
vc<typename F::S> minimal_polynomial_of_linear_map(int n, const LinearMap &linear_map, const RandomSample &random_sample = random_sample_mint)
{
using S = typename F::S;
assert(n > 0);
vvc<S> vs(2 * n + 1);
vs[0].resize(n, F::e0());
repi(j, n) vs[0][j] = random_sample();
repi(i, 1, 2 * n + 1) vs[i] = linear_map(vs[i - 1]);
return minimal_polynomial_of_vector_sequence<F>(vs, random_sample);
}
template <class F, class LinearMap, class RandomSample = decltype(random_sample_mint<typename F::S>)>
typename F::S det_of_linear_map(int n, const LinearMap &linear_map, const RandomSample &random_sample = random_sample_mint)
{
using S = typename F::S;
while (true)
{
vc<S> d(n);
repi(i, n) d[i] = random_sample();
auto linear_map_ad = [&](const vc<S> &v)
{
vc<S> w(n, F::e0());
repi(i, n) w[i] = F::mul(v[i], d[i]);
return linear_map(w);
};
auto m = minimal_polynomial_of_linear_map<F>(n, linear_map_ad, random_sample);
if (m[0] == F::e0())
return F::e0();
if (SZ(m) != n + 1)
continue;
S detd = F::e1();
fec(di : d) detd = F::mul(detd, di);
S res = F::mul(m[0], F::inv(detd));
return n & 1 ? res : F::minus(res);
}
}
namespace internal
{
constexpr ll powmod64_constexpr(ll x, ll n, ll m)
{
if (m == 1)
return 0;
ull _m = (ull)m;
ull r = 1;
ull y = safemod(x, m);
while (n)
{
u128 y128(y);
if (n & 1)
r = (y128 * r) % _m;
y = (y128 * y) % _m;
n >>= 1;
}
return r;
}
constexpr bool isprime64_constexpr(ll n)
{
if (n <= INT_MAX)
return isprime32_constexpr(n);
if (n % 2 == 0)
return false;
ll d = n - 1;
while (d % 2 == 0)
d /= 2;
constexpr ll bases[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
for (ll a : bases)
{
ll t = d;
ll y = powmod64_constexpr(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1)
{
y = (u128(y) * y) % n;
t <<= 1;
}
if (y != n - 1 && t % 2 == 0)
return false;
}
return true;
}
template <ll n>
constexpr bool isprime64 = isprime64_constexpr(n);
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; // m == 2^b * mx, mx is odd
imx = inv64(mx);
d = powmod64_constexpr((mx + 1) / 2, b, mx); // 2^{-b} mod mx
u128 sq = -u128(mx) % mx; // 2^128 mod mx
q = (1 + (((sq - 1) * d) << b)) % m;
}
ull umod() const { return m; }
ull reduce(u128 x) const
{
ull p = x & MASK(b); // x mod 2^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 <ll m>
struct static_modint64 : internal::modint_base<static_modint64<m>>
{
using mint = static_modint64;
private:
friend struct internal::modint_base<static_modint64<m>>;
ull _v;
static constexpr ull umod() { return m; }
static constexpr bool prime = internal::isprime64<m>;
public:
static constexpr ll mod() { return m; }
static mint raw(ll v)
{
mint x;
x._v = v;
return x;
}
static_modint64() : _v(0) {}
template <class T>
static_modint64(T v)
{
if constexpr (is_unsigned_v<T>)
{
_v = (ull)(v % umod());
}
else
{
ll x = (ll)(v % (ll)(umod()));
if (x < 0)
x += umod();
_v = (ull)x;
}
}
ll val() const { return (ll)_v; }
mint& operator*=(const mint &rhs)
{
u128 z = _v;
z *= rhs._v;
_v = (ull)(z % umod());
return *this;
}
mint inv() const
{
if (prime)
{
assert(_v != 0);
return CREF.pow(umod() - 2);
}
else
{
auto [g, x, y] = extgcd<ll>(_v, m);
assert(g == 1);
return x;
}
}
};
template <int id>
struct dynamic_modint64_odd : internal::modint_base<dynamic_modint64_odd<id>>
{
using mint = dynamic_modint64_odd;
private:
friend struct internal::modint_base<dynamic_modint64_odd<id>>;
ull _v; // montgomery expression
static internal::montgomery64odd mg;
static ull umod() { return mg.umod(); }
public:
static ll mod() { return (ll)(mg.umod()); }
dynamic_modint64_odd() : _v(0) {}
dynamic_modint64_odd(i128 v)
{ _v = mg.inv_reduce(v); }
ll val() const { return (ll)mg.reduce(_v); }
mint& operator*=(const mint &rhs)
{
_v = mg.reduce(u128(_v) * rhs._v);
return *this;
}
mint inv() const
{
auto [g, x, y] = extgcd<ll>(val(), mod());
assert(g == 1);
return x;
}
};
template <int id>
internal::montgomery64odd dynamic_modint64_odd<id>::mg((1LL << 61) - 1);
template <int id>
struct dynamic_modint64 : internal::modint_base<dynamic_modint64<id>>
{
using mint = dynamic_modint64;
private:
friend struct internal::modint_base<dynamic_modint64<id>>;
ull _v; // montgomery expression
static internal::montgomery64 mg;
static ull umod() { return mg.umod(); }
public:
static ll mod() { return (ll)(mg.umod()); }
static void set_mod(ll m)
{
assert(m >= 1);
mg = internal::montgomery64(m);
}
dynamic_modint64() : _v(0) {}
dynamic_modint64(i128 v)
{ _v = mg.inv_reduce(v); }
ll val() const { return (ll)mg.reduce(_v); }
mint& operator*=(const mint &rhs)
{
_v = mg.reduce(u128(_v) * rhs._v);
return *this;
}
mint inv() const
{
auto [g, x, y] = extgcd<ll>(val(), mod());
assert(g == 1);
return x;
}
};
template <int id>
internal::montgomery64 dynamic_modint64<id>::mg((1LL << 61) - 1);
template <class T>
struct is_static_modint64 : false_type {};
template <int m>
struct is_static_modint64<static_modint<m>> : true_type {};
template <class T>
inline constexpr bool is_static_modint64_v = is_static_modint64<T>::value;
template <class T>
struct is_dynamic_modint64 : false_type {};
template <int id>
struct is_dynamic_modint64<dynamic_modint<id>> : true_type {};
template <class T>
inline constexpr bool is_dynamic_modint64_v = is_dynamic_modint64<T>::value;
template <class T>
inline constexpr bool is_modint64_v = is_static_modint64_v<T> || is_dynamic_modint64_v<T>;
template <class F, int BS = 32>
struct Matrix : vvc<typename F::S>
{
using S = typename F::S;
using M = Matrix;
using V = vc<S>;
using vvc<S>::vector;
using vvc<S>::operator=;
Matrix(int n, int m, const S &diag = F::e0(), const S &non_diag = F::e0())
{
*this = vvc<S>(n, vc<S>(m, non_diag));
repi(i, min(n, m))(*this)[i][i] = diag;
}
Matrix(const vvc<S> &a) { *this = a; }
template <class I = ll>
pair<I, I> shape() const
{
const int n = (*this).size();
if (n == 0)
return {0, 0};
const int m = (*this)[0].size();
return {n, m};
}
M operator-() const
{
auto [n, m] = shape<int>();
M res(*this);
repi(i, n) repi(j, m) res[i][j] = F::minus(res[i][j]);
return res;
}
M &operator+=(const M &b)
{
assert(shape<int>() == b.shape<int>());
auto [n, m] = shape<int>();
repi(i, n) repi(j, m) (*this)[i][j] = F::add((*this)[i][j], b[i][j]);
return *this;
}
M &operator-=(const M &b) { return *this += F::minus(b); }
M &operator*=(const S &x)
{
auto [n, m] = shape<int>();
repi(i, n) repi(j, m) (*this)[i][j] = F::mul((*this)[i][j], x);
return *this;
}
M &operator/=(const S &x) { return *this *= F::inv(x); }
V operator*(const V &v) const
{
auto [n, m] = shape<int>();
assert(SZ(v) == m);
V res(n, F::e0());
repi(i, n)
{
S sm = F::e0();
repi(j, m) sm = F::add(sm, F::mul((*this)[i][j], v[j]));
res[i] = sm;
}
return res;
}
M operator*(const M &b) const
{
auto [n, m] = shape<int>();
auto [m_, p] = b.shape<int>();
assert(m == m_);
M res(n, p);
repi(ii, 0, n, BS) repi(kk, 0, m, BS) repi(jj, 0, p, BS)
{
repi(i, ii, min(ii + BS, n)) repi(k, kk, min(kk + BS, m))
{
S aik = (*this)[i][k];
if (aik == F::e0())
continue;
repi(j, jj, min(jj + BS, p)) res[i][j] = F::add(res[i][j], F::mul(aik, b[k][j]));
}
}
return res;
}
template <class T = ll>
M pow(T k) const
{
auto [n, m] = shape<int>();
assert(n == m);
M res(n, n, F::e1()), tmp(*this);
while (k > 0)
{
if (k & 1)
res *= tmp;
tmp *= tmp;
k >>= 1;
}
return res;
}
M operator+(const M &a) const { return M(*this) += a; }
M operator-(const M &a) const { return M(*this) -= a; }
M operator*(const S &x) const { return M(*this) *= x; }
M operator/(const S &x) const { return M(*this) /= x; }
M &operator*=(const M &a) { return *this = *this * a; }
template <class I = ll>
tuple<M, I, S> row_reduction(bool rref = false) const
{
auto [n, m] = shape<int>();
M a(*this);
I rk = 0;
S de = F::e1();
for (int i = 0, j = 0; i < n && j < m; j++)
{
repi(k, i, n)
{
if (a[k][j] != F::e0())
{
swap(a[k], a[i]);
if (k != i)
de = F::minus(de);
break;
}
}
if (a[i][j] == F::e0())
{
de = 0;
continue;
}
de = F::mul(de, a[i][j]);
S aij_inv = F::inv(a[i][j]);
repi(l, m) a[i][l] = F::mul(a[i][l], aij_inv);
if (rref)
{
repi(k, i)
{
S akj = a[k][j];
repi(l, m) a[k][l] = F::add(a[k][l], F::minus(F::mul(a[i][l], akj)));
}
}
repi(k, i + 1, n)
{
S akj = a[k][j];
repi(l, m) a[k][l] = F::add(a[k][l], F::minus(F::mul(a[i][l], akj)));
}
i++;
rk++;
}
return {a, rk, de};
}
S det() const
{
auto [n, m] = shape<int>();
assert(n == m);
if (n == 0)
return 1;
if constexpr (is_modint_v<S> || is_modint64_v<S>)
{
S cand = (*this)[0][0];
int cnt = 0;
repi(i, n) repi(j, n)
{
S x = (*this)[i][j];
if (cnt == 0)
cand = x, cnt = 1;
else if (cand == x)
cnt++;
else
cnt--;
}
int k = n * n;
repi(i, n) repi(j, n) if ((*this)[i][j] == cand) k--;
if (k < n * n / 8)
return det_sparse(cand);
}
return get<2>(row_reduction());
}
template <class RandomSample = decltype(random_sample_mint<typename F::S>)>
S det_sparse(const S &majority = F::e0(), const RandomSample &random_sample = random_sample_mint) const
{
auto [n, m] = shape<int>();
assert(n == m);
vc<pair<short, short>> elms;
repi(i, n) repi(j, n)
{
if ((*this)[i][j] != majority)
elms.eb(i, j);
}
auto linear_map = [&](const vc<S> &x)
{
S sm = F::e0();
fec(xi : x) sm = F::add(sm, xi);
vc<S> y(n, F::mul(majority, sm));
fec([ i, j ] : elms) y[i] = F::add(y[i], F::mul(F::add((*this)[i][j], F::minus(majority)), x[j]));
return y;
};
return det_of_linear_map<F>(n, linear_map, random_sample);
}
pair<bool, M> inv() const
{
auto [n, m] = shape<int>();
assert(n == m);
M a(n, 2 * n);
repi(i, n) repi(j, n) a[i][j] = (*this)[i][j];
repi(i, n) a[i][n + i] = F::e1();
M b = get<0>(a.row_reduction(true));
repi(i, n) if (b[i][i] == F::e0()) return {false, {}};
M res(n, n);
repi(i, n) repi(j, n) res[i][j] = b[i][n + j];
return {true, res};
}
};
template <class T>
struct Binomial
{
private:
static decltype(T::mod()) mod;
static vc<T> fac_, finv_, inv_;
public:
static void reserve(int n)
{
if (mod != T::mod())
{
mod = T::mod();
fac_ = {1, 1}, finv_ = {1, 1}, inv_ = {0, 1};
}
int i = fac_.size();
chmin(n, T::mod() - 1);
if (n < i)
return;
fac_.resize(n + 1), finv_.resize(n + 1), inv_.resize(n + 1);
for (; i <= n; i++)
{
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 fac(int n)
{
assert(n >= 0);
if (n >= T::mod())
return 0;
reserve(n);
return fac_[n];
}
static T inv(T n)
{
assert(n != 0);
reserve(n.val());
return inv_[n.val()];
}
};
template <class T> decltype(T::mod()) Binomial<T>::mod{};
template <class T> vc<T> Binomial<T>::fac_{};
template <class T> vc<T> Binomial<T>::finv_{};
template <class T> vc<T> Binomial<T>::inv_{};
template <class mint, class I>
mint count_spanning_trees_directed(const vvc<I> &g, int r)
{
const int n = g.size();
if (n == 0)
return 1;
repi(i, n) assert(SZ(g[i]) == n);
Matrix<FieldAddSubMulDiv<mint>> mat(n - 1, n - 1);
repi(i, n) repi(j, n)
{
if (i == j)
continue;
const int u = i < r ? i : i - 1;
const int v = j < r ? j : j - 1;
if (i != r && j != r)
mat[u][v] = -g[i][j];
if (j != r)
mat[v][v] += g[i][j];
}
return mat.det();
}
template <class mint, class I>
mint count_eularian_circuits(const vvc<I> &g)
{
const int n = g.size();
repi(i, n) assert(SZ(g[i]) == n);
vc<int> indeg(n, 0), outdeg(n, 0);
repi(i, n) repi(j, n) indeg[j] += g[i][j], outdeg[i] += g[i][j];
int k = 0;
vc<int> id(n, -1);
repi(i, n) if (indeg[i] != 0 || outdeg[i] != 0) id[i] = k++;
vvc<I> h(k, vc<I>(k, 0));
vc<pair<int, int>> es;
repi(i, n) repi(j, n)
{
if (g[i][j] == 0)
continue;
h[id[i]][id[j]] = g[i][j];
es.eb(id[i], id[j]);
}
auto cc_ids = connected_component_ids(GraphUndirected<bool>(k, es));
if (any_of(ALL(cc_ids), LMD(x, x > 0)))
return 0;
repi(i, n) if (indeg[i] != outdeg[i]) return 0;
mint res = count_spanning_trees_directed<mint>(h, 0);
repi(i, n) if (indeg[i] != 0) res *= Binomial<mint>::fac(indeg[i] - 1);
return res;
}
template <class mint, class I>
mint count_eularian_trails(const vvc<I> &g)
{
const int n = g.size();
repi(i, n) assert(SZ(g[i]) == n);
vc<int> indeg(n, 0), outdeg(n, 0);
repi(i, n) repi(j, n) indeg[j] += g[i][j], outdeg[i] += g[i][j];
const int m = SUM(indeg);
if (m == 0)
return 1;
int u = -1, v = -1;
repi(i, n)
{
if (indeg[i] - outdeg[i] == 1)
{
if (u != -1)
return 0;
u = i;
}
else if (indeg[i] - outdeg[i] == -1)
{
if (v != -1)
return 0;
v = i;
}
else
{
if (indeg[i] - outdeg[i] != 0)
return 0;
}
}
if (u == -1 && v == -1)
return count_eularian_circuits<mint>(g) * m;
else if (u != -1 && v != -1)
{
auto h = g;
h[u][v]++;
return count_eularian_circuits<mint>(h);
}
else
return 0;
}
void init()
{
oj(mt.seed(random_device()()));
}
void main2()
{
LL(N, M);
VEC(pll, M, AB);
offset(AB, pll{-1, -1});
vvc<short> G(N, vc<short>(N, 1));
fec([ a, b ] : AB) G.at(a).at(b) = 0;
PRINT(count_eularian_trails<mint>(G));
}
void test()
{
}
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 << val;
//*/
};
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