// #define _GLIBCXX_DEBUG #include // clang-format off std::ostream&operator<<(std::ostream&os,std::int8_t x){return os<<(int)x;} std::ostream&operator<<(std::ostream&os,std::uint8_t x){return os<<(int)x;} std::ostream&operator<<(std::ostream&os,const __int128_t &u){if(!u)os<<"0";__int128_t tmp=u<0?(os<<"-",-u):u;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os< template constexpr inline Int mod_inv(Int a, Int mod) { static_assert(std::is_signed_v); Int x= 1, y= 0, b= mod; for (Int q= 0, z= 0; b;) z= x, x= y, y= z - y * (q= a / b), z= a, a= b, b= z - b * q; return assert(a == 1), x < 0 ? mod - (-x) % mod : x % mod; } namespace math_internal { using namespace std; using u8= unsigned char; using u32= unsigned; using i64= long long; using u64= unsigned long long; using u128= __uint128_t; #define CE constexpr #define IL inline #define NORM \ if (n >= mod) n-= mod; \ return n #define PLUS(U, M) \ CE IL U plus(U l, U r) const { return l+= r, l < (M) ? l : l - (M); } #define DIFF(U, C, M) \ CE IL U diff(U l, U r) const { return l-= r, l >> C ? l + (M) : l; } #define SGN(U) \ static CE IL U set(U n) { return n; } \ static CE IL U get(U n) { return n; } \ static CE IL U norm(U n) { return n; } template struct MP_Mo { u_t mod; CE MP_Mo(): mod(0), iv(0), r2(0) {} CE MP_Mo(u_t m): mod(m), iv(inv(m)), r2(-du_t(mod) % mod) {} CE IL u_t mul(u_t l, u_t r) const { return reduce(du_t(l) * r); } PLUS(u_t, mod << 1) DIFF(u_t, A, mod << 1) CE IL u_t set(u_t n) const { return mul(n, r2); } CE IL u_t get(u_t n) const { n= reduce(n); NORM; } CE IL u_t norm(u_t n) const { NORM; } private: u_t iv, r2; static CE u_t inv(u_t n, int e= 6, u_t x= 1) { return e ? inv(n, e - 1, x * (2 - x * n)) : x; } CE IL u_t reduce(const du_t &w) const { return u_t(w >> B) + mod - ((du_t(u_t(w) * iv) * mod) >> B); } }; struct MP_Na { u32 mod; CE MP_Na(): mod(0){}; CE MP_Na(u32 m): mod(m) {} CE IL u32 mul(u32 l, u32 r) const { return u64(l) * r % mod; } PLUS(u32, mod) DIFF(u32, 31, mod) SGN(u32) }; struct MP_Br { // mod < 2^31 u32 mod; CE MP_Br(): mod(0), s(0), x(0) {} CE MP_Br(u32 m): mod(m), s(95 - __builtin_clz(m - 1)), x(((u128(1) << s) + m - 1) / m) {} CE IL u32 mul(u32 l, u32 r) const { return rem(u64(l) * r); } PLUS(u32, mod) DIFF(u32, 31, mod) SGN(u32) private: u8 s; u64 x; CE IL u64 quo(u64 n) const { return (u128(x) * n) >> s; } CE IL u32 rem(u64 n) const { return n - quo(n) * mod; } }; struct MP_Br2 { // 2^20 < mod <= 2^41 u64 mod; CE MP_Br2(): mod(0), x(0) {} CE MP_Br2(u64 m): mod(m), x((u128(1) << 84) / m) {} CE IL u64 mul(u64 l, u64 r) const { return rem(u128(l) * r); } PLUS(u64, mod << 1) DIFF(u64, 63, mod << 1) static CE IL u64 set(u64 n) { return n; } CE IL u64 get(u64 n) const { NORM; } CE IL u64 norm(u64 n) const { NORM; } private: u64 x; CE IL u128 quo(const u128 &n) const { return (n * x) >> 84; } CE IL u64 rem(const u128 &n) const { return n - quo(n) * mod; } }; struct MP_D2B1 { u8 s; u64 mod, d, v; CE MP_D2B1(): s(0), mod(0), d(0), v(0) {} CE MP_D2B1(u64 m): s(__builtin_clzll(m)), mod(m), d(m << s), v(u128(-1) / d) {} CE IL u64 mul(u64 l, u64 r) const { return rem((u128(l) * r) << s) >> s; } PLUS(u64, mod) DIFF(u64, 63, mod) SGN(u64) private: CE IL u64 rem(const u128 &u) const { u128 q= (u >> 64) * v + u; u64 r= u64(u) - (q >> 64) * d - d; if (r > u64(q)) r+= d; if (r >= d) r-= d; return r; } }; template CE u_t pow(u_t x, u64 k, const MP &md) { for (u_t ret= md.set(1);; x= md.mul(x, x)) if (k & 1 ? ret= md.mul(ret, x) : 0; !(k>>= 1)) return ret; } #undef NORM #undef PLUS #undef DIFF #undef SGN #undef CE } namespace math_internal { struct m_b {}; struct s_b: m_b {}; } template constexpr bool is_modint_v= std::is_base_of_v; template constexpr bool is_staticmodint_v= std::is_base_of_v; namespace math_internal { #define CE constexpr template struct SB: s_b { protected: static CE MP md= MP(MOD); }; template struct MInt: public B { using Uint= U; static CE inline auto mod() { return B::md.mod; } CE MInt(): x(0) {} template && !is_same_v>> CE MInt(T v): x(B::md.set(v.val() % B::md.mod)) {} CE MInt(__int128_t n): x(B::md.set((n < 0 ? ((n= (-n) % B::md.mod) ? B::md.mod - n : n) : n % B::md.mod))) {} CE MInt operator-() const { return MInt() - *this; } #define FUNC(name, op) \ CE MInt name const { \ MInt ret; \ return ret.x= op, ret; \ } FUNC(operator+(const MInt & r), B::md.plus(x, r.x)) FUNC(operator-(const MInt & r), B::md.diff(x, r.x)) FUNC(operator*(const MInt & r), B::md.mul(x, r.x)) FUNC(pow(u64 k), math_internal::pow(x, k, B::md)) #undef FUNC CE MInt operator/(const MInt& r) const { return *this * r.inv(); } CE MInt& operator+=(const MInt& r) { return *this= *this + r; } CE MInt& operator-=(const MInt& r) { return *this= *this - r; } CE MInt& operator*=(const MInt& r) { return *this= *this * r; } CE MInt& operator/=(const MInt& r) { return *this= *this / r; } CE bool operator==(const MInt& r) const { return B::md.norm(x) == B::md.norm(r.x); } CE bool operator!=(const MInt& r) const { return !(*this == r); } CE bool operator<(const MInt& r) const { return B::md.norm(x) < B::md.norm(r.x); } CE inline MInt inv() const { return mod_inv(val(), B::md.mod); } CE inline Uint val() const { return B::md.get(x); } friend ostream& operator<<(ostream& os, const MInt& r) { return os << r.val(); } friend istream& operator>>(istream& is, MInt& r) { i64 v; return is >> v, r= MInt(v), is; } private: Uint x; }; template using ModInt= conditional_t < (MOD < (1 << 30)) & MOD, MInt, MOD>>, conditional_t < (MOD < (1ull << 62)) & MOD, MInt, MOD>>, conditional_t>, conditional_t>, conditional_t>, MInt>>>>>>; #undef CE } using math_internal::ModInt; class Nimber { using u64= unsigned long long; using u32= unsigned; using u16= unsigned short; static inline std::array pw, ln; template static inline u16 half(u16 A) { return A ? pw[(ln[A] + h) % 65535] : 0; } template static inline u16 mul(u16 A, u16 B) { return A && B ? pw[(ln[A] + ln[B] + h) % 65535] : 0; } template static inline u16 mul(u16 A, u16 B, u16 C) { return A && B && C ? pw[(ln[A] + ln[B] + ln[C] + h) % 65535] : 0; } static inline u16 inv(u16 A) { return assert(A), pw[65535 - ln[A]]; } static inline u16 sqrt(u16 A) { return A ? pw[u16((65537 * u32(ln[A])) >> 1)] : 0; } static inline u64 mul(u64 A, u64 B) { u16 a0= u16(A), a1= u16(A >> 16), a2= u16(A >> 32), a3= A >> 48, b0= u16(B), b1= u16(B >> 16), b2= u16(B >> 32), b3= B >> 48, x0= a1 ^ a0, x1= a3 ^ a2, y0= b1 ^ b0, y1= b3 ^ b2, c0= mul(a0, b0), c1= mul(x0, y0) ^ c0, c2= mul<0>(a2 ^ a0, b2 ^ b0), c3= mul<0>(x0 ^ x1, y0 ^ y1) ^ c2 ^ c1; return c2^= (c0^= mul<3>(a1, b1)) ^ mul<3>(u16(a3 ^ a1), u16(b3 ^ b1)), c1^= mul<6>(a3, b3) ^ mul<3>(x1, y1), c0^= mul<6>(a2, b2) ^ mul<6>(x1, y1), (u64(c3) << 48) | (u64(c2) << 32) | (u32(c1) << 16) | c0; } static inline u64 inv(u64 A) { u16 a0= u16(A), a1= u16(A >> 16), a2= u16(A >> 32), a3= A >> 48, x= a2 ^ a3, y= a1 ^ a3, w= a0 ^ a2, v= a0 ^ a1, b3= mul(a1, a2, a1 ^ x), b2= mul(a0, a2, a0 ^ x), b1= mul(a0, a1, a0 ^ y), b0= mul(a0, v, w), t= mul<3>(w, x, x); return b0^= b1 ^ b2, b1^= b3, b2^= b3, b0^= b3^= mul(a0, a0, a3), b1^= t ^ mul<3>(a1, y, y), b0^= t ^ mul<3>(v, y, y), b3^= t= mul<3>(a1, a3, y) ^ mul<3>(a2, x, x), b2^= t ^ mul<3>(a0, a3, a3) ^ mul<3>(a1, a1, a2), b3^= mul<6>(a3, a3, x), b2^= mul<6>(a3, x, x), b1^= mul<6>(a3, a3, y ^ w), b0^= mul<6>(y, x, x), b2^= mul<9>(a3, a3, a3), b0^= mul<9>(a3, a3, y), t= mul<6>(x, b3) ^ mul<6>(a3, b2) ^ mul<3>(a1, b1) ^ mul(a0, b0), t= inv(t), (u64(mul(b3, t)) << 48) | (u64(mul(b2, t)) << 32) | (u32(mul(b1, t)) << 16) | mul(b0, t); } static inline u64 square(u64 A) { u16 a0= u16(A), a1= u16(A >> 16), a2= u16(A >> 32), a3= A >> 48; return a3= mul(a3, a3), a2= mul(a2, a2), a1= mul(a1, a1), a0= mul(a0, a0), a0^= half(a1) ^ half<6>(a3), a2^= half(a3), a1^= half(a3 ^ a2), (u64(a3) << 48) | (u64(a2) << 32) | (u32(a1) << 16) | a0; } static inline u64 pow(u64 A, u64 k) { for (u64 ret= 1;; A= square(A)) if (k & 1 ? ret= mul(ret, A) : 0; !(k>>= 1)) return ret; } template static inline int mdif(int a, int b) { return a+= mod & -((a-= b) < 0); } template static inline int mmul(int a, int b) { return u64(a) * b % mod; } static inline int log16(u16 A, u16 B) { int a= ln[A], b= ln[B], x= 1; if (a == 0) return b == 0 ? 1 : -1; for (int q, z, u, y= 0, t= 65535; t;) z= x, u= a, x= y, y= z - y * (q= a / t), a= t, t= u - t * q; return b % a ? -1 : u32(b / a) * (x < 0 ? 65535 + x : x) % 65535; } template static inline int bsgs(u64 x, u64 y) { static constexpr int mask= size - 1; std::pair vs[size]; int os[size + 1]= {}; u64 so[size], big= 1; for (int i= 0; i < size; ++i, big= mul(big, x)) ++os[(so[i]= big) & mask]; for (int i= 0; i < size; ++i) os[i + 1]+= os[i]; for (int i= 0; i < size; ++i) vs[--os[so[i] & mask]]= {so[i], i}; for (int t= 0; t < period; t+= size, y= mul(y, big)) for (int m= (y & mask), i= os[m], ret; i < os[m + 1]; ++i) if (y == vs[i].first) return (ret= vs[i].second - t) < 0 ? ret + period : ret; return -1; } static inline u64 log(u64 A, u64 B) { if (B == 1) return 0; if (!A && !B) return 1; if (!A || !B) return u64(-1); static constexpr int P0= 641, P1= 65535, P2= 65537, P3= 6700417, iv10= 40691, iv21= 32768, iv20= 45242, iv32= 3317441, iv31= 3350208, iv30= 3883315; int a0= bsgs(pow(A, 0x663d80ff99c27f), pow(B, 0x663d80ff99c27f)); if (a0 == -1) return u64(-1); int a1= log16(pow(A, 0x1000100010001), pow(B, 0x1000100010001)); if (a1 == -1) return u64(-1); int a2= bsgs(pow(A, 0xffff0000ffff), pow(B, 0xffff0000ffff)); if (a2 == -1) return u64(-1); int a3= bsgs(pow(A, 0x280fffffd7f), pow(B, 0x280fffffd7f)); if (a3 == -1) return u64(-1); int x1= mmul(mdif(a1, a0), iv10), x2= mdif(mmul(mdif(a2, a0), iv20), mmul(x1, iv21)), x3= mdif(mdif(mmul(mdif(a3, a0), iv30), mmul(x1, iv31)), mmul(x2, iv32)); return u64(P0) * (u64(P1) * (u64(P2) * x3 + x2) + x1) + a0; } u64 x; public: static inline void init(u32 x= 0, u32 y= 0) { constexpr u16 f2n[16]= {0x0001u, 0x2827u, 0x392bu, 0x8000u, 0x20fdu, 0x4d1du, 0xde4au, 0x0a17u, 0x3464u, 0xe3a9u, 0x6d8du, 0x34bcu, 0xa921u, 0xa173u, 0x0ebcu, 0x0e69u}; for (int i= pw[0]= pw[65535]= 1; i < 65535; ++i) pw[i]= (pw[i - 1] << 1) ^ (0x1681fu & (-(pw[i - 1] >= 0x8000u))); for (int i= 1; i < 65535; ln[pw[i]= y]= i, i++) for (x= pw[i], y= 0; x; x&= x - 1) y^= f2n[__builtin_ctz(x)]; } Nimber(u64 x_= 0): x(x_) {} Nimber &operator+=(const Nimber &r) { return x^= r.x, *this; } Nimber &operator-=(const Nimber &r) { return x^= r.x, *this; } Nimber &operator*=(const Nimber &r) { return x= mul(x, r.x), *this; } Nimber &operator/=(const Nimber &r) { return x= mul(x, inv(r.x)), *this; } Nimber operator+(const Nimber &r) const { return Nimber(x ^ r.x); } Nimber operator-(const Nimber &r) const { return Nimber(x ^ r.x); } Nimber operator*(const Nimber &r) const { return Nimber(mul(x, r.x)); } Nimber operator/(const Nimber &r) const { return Nimber(mul(x, inv(r.x))); } Nimber operator-() const { return *this; } Nimber inv() const { return Nimber(inv(x)); } Nimber square() const { return Nimber(square(x)); } Nimber sqrt() const { u16 a0= u16(x), a1= u16(x >> 16), a2= u16(x >> 32), a3= x >> 48; return a1^= half(a3 ^ a2), a2^= half(a3), a0^= half(a1) ^ half<6>(a3), Nimber((u64(sqrt(a3)) << 48) | (u64(sqrt(a2)) << 32) | (u32(sqrt(a1)) << 16) | sqrt(a0)); } u64 val() const { return x; } Nimber pow(u64 k) const { return Nimber(pow(x, k)); } u64 log(const Nimber &r) const { return log(x, r.x); } bool operator==(const Nimber &r) const { return x == r.x; } bool operator!=(const Nimber &r) const { return x != r.x; } bool operator<(const Nimber &r) const { return x < r.x; } bool operator>(const Nimber &r) const { return x > r.x; } bool operator<=(const Nimber &r) const { return x <= r.x; } bool operator>=(const Nimber &r) const { return x >= r.x; } friend std::ostream &operator<<(std::ostream &os, const Nimber &r) { return os << r.x; } friend std::istream &operator>>(std::istream &is, Nimber &r) { return is >> r.x, is; } }; namespace _la_internal { using namespace std; template struct Vector { valarray dat; Vector()= default; Vector(size_t n): dat(n) {} Vector(size_t n, const R &v): dat(v, n) {} Vector(const initializer_list &v): dat(v) {} R &operator[](int i) { return dat[i]; } const R &operator[](int i) const { return dat[i]; } bool operator==(const Vector &r) const { if (dat.size() != r.dat.size()) return false; for (int i= dat.size(); i--;) if (dat[i] != r.dat[i]) return false; return true; } bool operator!=(const Vector &r) const { return !(*this == r); } explicit operator bool() const { return dat.size(); } Vector operator-() const { return Vector(dat.size())-= *this; } Vector &operator+=(const Vector &r) { return dat+= r.dat, *this; } Vector &operator-=(const Vector &r) { return dat-= r.dat, *this; } Vector &operator*=(const R &r) { return dat*= r, *this; } Vector operator+(const Vector &r) const { return Vector(*this)+= r; } Vector operator-(const Vector &r) const { return Vector(*this)-= r; } Vector operator*(const R &r) const { return Vector(*this)*= r; } size_t size() const { return dat.size(); } friend R dot(const Vector &a, const Vector &b) { return assert(a.size() == b.size()), (a.dat * b.dat).sum(); } }; using u128= __uint128_t; using u64= uint64_t; using u8= uint8_t; class Ref { u128 *ref; u8 i; public: Ref(u128 *ref, u8 i): ref(ref), i(i) {} Ref &operator=(const Ref &r) { return *this= bool(r); } Ref &operator=(bool b) { return *ref&= ~(u128(1) << i), *ref|= u128(b) << i, *this; } Ref &operator|=(bool b) { return *ref|= u128(b) << i, *this; } Ref &operator&=(bool b) { return *ref&= ~(u128(!b) << i), *this; } Ref &operator^=(bool b) { return *ref^= u128(b) << i, *this; } operator bool() const { return (*ref >> i) & 1; } }; template <> class Vector { size_t n; public: valarray dat; Vector(): n(0) {} Vector(size_t n): n(n), dat((n + 127) >> 7) {} Vector(size_t n, bool b): n(n), dat(-u128(b), (n + 127) >> 7) { if (int k= n & 127; k) dat[dat.size() - 1]&= (u128(1) << k) - 1; } Vector(const initializer_list &v): n(v.size()), dat((n + 127) >> 7) { int i= 0; for (bool b: v) dat[i >> 7]|= u128(b) << (i & 127), ++i; } Ref operator[](int i) { return {begin(dat) + (i >> 7), u8(i & 127)}; } bool operator[](int i) const { return (dat[i >> 7] >> (i & 127)) & 1; } bool operator==(const Vector &r) const { if (dat.size() != r.dat.size()) return false; for (int i= dat.size(); i--;) if (dat[i] != r.dat[i]) return false; return true; } bool operator!=(const Vector &r) const { return !(*this == r); } explicit operator bool() const { return n; } Vector operator-() const { return Vector(*this); } Vector &operator+=(const Vector &r) { return dat^= r.dat, *this; } Vector &operator-=(const Vector &r) { return dat^= r.dat, *this; } Vector &operator*=(bool b) { return dat*= b, *this; } Vector operator+(const Vector &r) const { return Vector(*this)+= r; } Vector operator-(const Vector &r) const { return Vector(*this)-= r; } Vector operator*(bool b) const { return Vector(*this)*= b; } size_t size() const { return n; } friend bool dot(const Vector &a, const Vector &b) { assert(a.size() == b.size()); u128 v= 0; for (int i= a.dat.size(); i--;) v^= a.dat[i] & b.dat[i]; return __builtin_parityll(v >> 64) ^ __builtin_parityll(u64(v)); } }; template Vector operator*(const R &r, const Vector &v) { return v * r; } template ostream &operator<<(ostream &os, const Vector &v) { os << '['; for (int _= 0, __= v.size(); _ < __; ++_) os << (_ ? ", " : "") << v[_]; return os << ']'; } } using _la_internal::Vector; namespace _la_internal { template struct Mat { Mat(): W(0) {} Mat(size_t h, size_t w): W(w), dat(h * w) {} Mat(size_t h, size_t w, R v): W(w), dat(v, h * w) {} Mat(initializer_list> v): W(v.size() ? v.begin()->size() : 0), dat(v.size() * W) { auto it= begin(dat); for (const auto &r: v) { assert(r.size() == W); for (R x: r) *it++= x; } } size_t width() const { return W; } size_t height() const { return W ? dat.size() / W : 0; } auto operator[](int i) { return begin(dat) + i * W; } auto operator[](int i) const { return begin(dat) + i * W; } protected: size_t W; valarray dat; void add(const Mat &r) { assert(dat.size() == r.dat.size()), assert(W == r.W), dat+= r.dat; } D mul(const Mat &r) const { const size_t h= height(), w= r.W, l= W; assert(l == r.height()); D ret(h, w); auto a= begin(dat); auto c= begin(ret.dat); for (int i= h; i--; c+= w) { auto b= begin(r.dat); for (int k= l; k--; ++a) { auto d= c; auto v= *a; for (int j= w; j--; ++b, ++d) *d+= v * *b; } } return ret; } Vector mul(const Vector &r) const { assert(W == r.size()); const size_t h= height(); Vector ret(h); auto a= begin(dat); for (size_t i= 0; i < h; ++i) for (size_t k= 0; k < W; ++k, ++a) ret[i]+= *a * r[k]; return ret; } }; template struct Mat { struct Array { u128 *bg; Array(u128 *it): bg(it) {} Ref operator[](int i) { return Ref{bg + (i >> 7), u8(i & 127)}; } bool operator[](int i) const { return (bg[i >> 7] >> (i & 127)) & 1; } }; struct ConstArray { const u128 *bg; ConstArray(const u128 *it): bg(it) {} bool operator[](int i) const { return (bg[i >> 7] >> (i & 127)) & 1; } }; Mat(): H(0), W(0), m(0) {} Mat(size_t h, size_t w): H(h), W(w), m((w + 127) >> 7), dat(h * m) {} Mat(size_t h, size_t w, bool b): H(h), W(w), m((w + 127) >> 7), dat(-u128(b), h * m) { if (size_t i= h, k= w & 127; k) for (u128 s= (u128(1) << k) - 1; i--;) dat[i * m]&= s; } Mat(const initializer_list> &v): H(v.size()), W(H ? v.begin()->size() : 0), m((W + 127) >> 7), dat(H * m) { auto it= begin(dat); for (const auto &r: v) { assert(r.size() == W); int i= 0; for (bool b: r) it[i >> 7]|= u128(b) << (i & 127), ++i; it+= m; } } size_t width() const { return W; } size_t height() const { return H; } Array operator[](int i) { return {begin(dat) + i * m}; } ConstArray operator[](int i) const { return {begin(dat) + i * m}; } ConstArray get(int i) const { return {begin(dat) + i * m}; } protected: size_t H, W, m; valarray dat; void add(const Mat &r) { assert(H == r.H), assert(W == r.W), dat^= r.dat; } D mul(const Mat &r) const { assert(W == r.H); D ret(H, r.W); valarray tmp(r.m << 8); auto y= begin(r.dat); for (size_t l= 0; l < W; l+= 8) { auto t= begin(tmp) + r.m; for (int i= 0, n= min(8, W - l); i < n; ++i, y+= r.m) { auto u= begin(tmp); for (int s= 1 << i; s--;) { auto z= y; for (int j= r.m; j--; ++u, ++t, ++z) *t= *u ^ *z; } } auto a= begin(dat) + (l >> 7); auto c= begin(ret.dat); for (int i= H; i--; a+= m) { auto u= begin(tmp) + ((*a >> (l & 127)) & 255) * r.m; for (int j= r.m; j--; ++c, ++u) *c^= *u; } } return ret; } Vector mul(const Vector &r) const { assert(W == r.size()); Vector ret(H); auto a= begin(dat); for (size_t i= 0; i < H; ++i) { u128 v= 0; for (size_t j= 0; j < m; ++j, ++a) v^= *a & r.dat[j]; ret[i]= __builtin_parityll(v >> 64) ^ __builtin_parityll(u64(v)); } return ret; } }; template struct Matrix: public Mat> { using Mat>::Mat; explicit operator bool() const { return this->W; } static Matrix identity(int n) { Matrix ret(n, n); for (; n--;) ret[n][n]= R(true); return ret; } Matrix submatrix(const vector &rows, const vector &cols) const { Matrix ret(rows.size(), cols.size()); for (int i= rows.size(); i--;) for (int j= cols.size(); j--;) ret[i][j]= (*this)[rows[i]][cols[j]]; return ret; } Matrix submatrix_rm(vector rows, vector cols) const { sort(begin(rows), end(rows)), sort(begin(cols), end(cols)), rows.erase(unique(begin(rows), end(rows)), end(rows)), cols.erase(unique(begin(cols), end(cols)), end(cols)); const int H= this->height(), W= this->width(), n= rows.size(), m= cols.size(); vector rs(H - n), cs(W - m); for (int i= 0, j= 0, k= 0; i < H; ++i) if (j < n && rows[j] == i) ++j; else rs[k++]= i; for (int i= 0, j= 0, k= 0; i < W; ++i) if (j < m && cols[j] == i) ++j; else cs[k++]= i; return submatrix(rs, cs); } bool operator==(const Matrix &r) const { if (this->width() != r.width() || this->height() != r.height()) return false; for (int i= this->dat.size(); i--;) if (this->dat[i] != r.dat[i]) return false; return true; } bool operator!=(const Matrix &r) const { return !(*this == r); } Matrix &operator*=(const Matrix &r) { return *this= this->mul(r); } Matrix operator*(const Matrix &r) const { return this->mul(r); } Matrix &operator*=(R r) { return this->dat*= r, *this; } template Matrix operator*(T r) const { static_assert(is_convertible_v); return Matrix(*this)*= r; } Matrix &operator+=(const Matrix &r) { return this->add(r), *this; } Matrix operator+(const Matrix &r) const { return Matrix(*this)+= r; } Vector operator*(const Vector &r) const { return this->mul(r); } Vector operator()(const Vector &r) const { return this->mul(r); } Matrix pow(uint64_t k) const { size_t W= this->width(); assert(W == this->height()); for (Matrix ret= identity(W), b= *this;; b*= b) if (k & 1 ? ret*= b, !(k>>= 1) : !(k>>= 1)) return ret; } }; template Matrix operator*(const T &r, const Matrix &m) { return m * r; } template ostream &operator<<(ostream &os, const Matrix &m) { os << "\n["; for (int i= 0, h= m.height(); i < h; os << ']', ++i) { if (i) os << "\n "; os << '['; for (int j= 0, w= m.width(); j < w; ++j) os << (j ? ", " : "") << m[i][j]; } return os << ']'; } template static bool is_zero(K x) { if constexpr (is_floating_point_v) return abs(x) < 1e-8; else return x == K(); } } using _la_internal::Matrix; namespace _la_internal { template class LU_Decomposition { Matrix dat; vector perm, piv; bool sgn; size_t psz; public: LU_Decomposition(const Matrix &A): dat(A), perm(A.height()), sgn(false), psz(0) { const size_t h= A.height(), w= A.width(); iota(perm.begin(), perm.end(), 0), piv.resize(min(w, h)); for (size_t c= 0, pos; c < w && psz < h; ++c) { pos= psz; if constexpr (is_floating_point_v) { for (size_t r= psz + 1; r < h; ++r) if (abs(dat[perm[pos]][c]) < abs(dat[perm[r]][c])) pos= r; } else if (is_zero(dat[perm[pos]][c])) for (size_t r= psz + 1; r < h; ++r) if (!is_zero(dat[perm[r]][c])) pos= r, r= h; if (is_zero(dat[perm[pos]][c])) continue; if (pos != psz) sgn= !sgn, swap(perm[pos], perm[psz]); const auto b= dat[perm[psz]]; for (size_t r= psz + 1, i; r < h; ++r) { auto a= dat[perm[r]]; K m= a[c] / b[c]; for (a[c]= K(), a[psz]= m, i= c + 1; i < w; ++i) a[i]-= b[i] * m; } piv[psz++]= c; } } size_t rank() const { return psz; } bool is_regular() const { return rank() == dat.height() && rank() == dat.width(); } K det() const { assert(dat.height() == dat.width()); K ret= sgn ? -1 : 1; for (size_t i= dat.width(); i--;) ret*= dat[perm[i]][i]; return ret; } vector> kernel() const { const size_t w= dat.width(), n= rank(); vector ker(w - n, Vector(w)); for (size_t c= 0, i= 0; c < w; ++c) { if (i < n && piv[i] == c) ++i; else { auto &a= ker[c - i]; a[c]= 1; for (size_t r= i; r--;) a[r]= -dat[perm[r]][c]; for (size_t j= i, k, r; j--;) { K x= a[j] / dat[perm[j]][k= piv[j]]; for (a[j]= 0, a[k]= x, r= j; r--;) a[r]-= dat[perm[r]][k] * x; } } } return ker; } Vector linear_equations(const Vector &b) const { const size_t h= dat.height(), w= dat.width(), n= rank(); assert(h == b.size()); Vector y(h), x(w); for (size_t c= 0; c < h; ++c) if (y[c]+= b[perm[c]]; c < w) for (size_t r= c + 1; r < h; ++r) y[r]-= y[c] * dat[perm[r]][c]; for (size_t i= n; i < h; ++i) if (!is_zero(y[i])) return Vector(); // no solution for (size_t i= n, r; i--;) for (x[piv[i]]= y[i] / dat[perm[i]][piv[i]], r= i; r--;) y[r]-= x[piv[i]] * dat[perm[r]][piv[i]]; return x; } Matrix inverse_matrix() const { if (!is_regular()) return Matrix(); // no solution const size_t n= dat.width(); Matrix ret(n, n); for (size_t i= 0; i < n; ++i) { Vector y(n); for (size_t c= 0; c < n; ++c) if (y[c]+= perm[c] == i; !is_zero(y[c])) for (size_t r= c + 1; r < n; ++r) y[r]-= y[c] * dat[perm[r]][c]; for (size_t j= n; j--;) { K m= ret[j][i]= y[j] / dat[perm[j]][j]; for (size_t r= j; r--;) y[r]-= m * dat[perm[r]][j]; } } return ret; } }; void add_upper(u128 *a, const u128 *b, size_t bg, size_t ed) { //[bg,ed) if (bg >= ed) return; size_t s= bg >> 7; a[s]^= b[s] & -(u128(1) << (bg & 127)); for (size_t i= (ed + 127) >> 7; --i > s;) a[i]^= b[i]; } void add_lower(u128 *a, const u128 *b, size_t ed) { //[0,ed) size_t s= ed >> 7; for (a[s]^= b[s] & ((u128(1) << (ed & 127)) - 1); s--;) a[s]^= b[s]; } void subst_lower(u128 *a, const u128 *b, size_t ed) { //[0,ed) size_t s= ed >> 7; for (a[s]= b[s] & ((u128(1) << (ed & 127)) - 1); s--;) a[s]= b[s]; } bool any1_upper(const u128 *a, size_t bg, size_t ed) { //[bg,ed) if (bg >= ed) return false; size_t s= bg >> 7; if (a[s] & -(u128(1) << (bg & 127))) return true; for (size_t i= (ed + 127) >> 7; --i > s;) if (a[i]) return true; return false; } template <> class LU_Decomposition { Matrix dat; vector perm, piv; size_t psz; public: LU_Decomposition(Matrix A): dat(A.width(), A.height()), perm(A.height()), psz(0) { const size_t h= A.height(), w= A.width(); iota(perm.begin(), perm.end(), 0), piv.resize(min(w, h)); for (size_t c= 0, pos; c < w && psz < h; ++c) { for (pos= psz; pos < h; ++pos) if (A.get(perm[pos])[c]) break; if (pos == h) continue; if (pos != psz) swap(perm[pos], perm[psz]); auto b= A.get(perm[psz]); for (size_t r= psz + 1; r < h; ++r) { auto a= A[perm[r]]; if (bool m= a[c]; m) add_upper(a.bg, b.bg, c, w), a[psz]= 1; } piv[psz++]= c; } for (size_t j= w; j--;) for (size_t i= h; i--;) dat[j][i]= A.get(perm[i])[j]; } size_t rank() const { return psz; } bool is_regular() const { return rank() == dat.height() && rank() == dat.width(); } bool det() const { return is_regular(); } vector> kernel() const { const size_t w= dat.height(), n= rank(); vector ker(w - rank(), Vector(w)); for (size_t c= 0, i= 0; c < w; ++c) { if (i < n && piv[i] == c) ++i; else { auto &a= ker[c - i]; subst_lower(begin(a.dat), dat[c].bg, i), a[c]= 1; for (size_t j= i, k; j--;) { bool x= a[j]; if (a[j]= 0, a[k= piv[j]]= x; x) add_lower(begin(a.dat), dat[k].bg, j); } } } return ker; } Vector linear_equations(const Vector &b) const { const size_t h= dat.width(), w= dat.height(), n= rank(); assert(h == b.size()); Vector y(h), x(w); for (size_t c= 0; c < h; ++c) if (y[c]^= b[perm[c]]; c < w && y[c]) add_upper(begin(y.dat), dat[c].bg, c + 1, h); if (any1_upper(begin(y.dat), n, h)) return Vector(); // no solution for (size_t i= n; i--;) if ((x[piv[i]]= y[i])) add_lower(begin(y.dat), dat[piv[i]].bg, i); return x; } Matrix inverse_matrix() const { if (!is_regular()) return Matrix(); // no solution const size_t n= dat.width(); Matrix ret(n, n); for (size_t i= 0; i < n; ++i) { Vector y(n); for (size_t c= 0; c < n; ++c) if (y[c]^= perm[c] == i; y[c]) add_upper(begin(y.dat), dat[c].bg, c + 1, n); for (size_t j= n; j--;) if ((ret[j][i]= y[j])) add_lower(begin(y.dat), dat[j].bg, j); } return ret; } }; } using _la_internal::LU_Decomposition; using namespace std; signed main() { cin.tie(0); ios::sync_with_stdio(0); using Mint= ModInt<998244353>; Nimber::init(); int N, T; cin >> N >> T; Matrix H(T, N); for (int i= 0; i < T; ++i) for (int j= 0; j < N; ++j) { long long x; cin >> x; H[i][j]= x - 1; } Mint ans= 0, pw= Mint(2).pow(64); for (long long s= 1ll << N; s--;) { vector rm; int n= 0; for (int i= N; i--;) if ((s >> i) & 1) rm.push_back(i); else ++n; Mint x= pw.pow(n - LU_Decomposition(H.submatrix_rm({}, rm)).rank()); if ((N - n) & 1) ans-= x; else ans+= x; } cout << ans << '\n'; return 0; }