CP-Algorithms Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub cp-algorithms/cp-algorithms-aux

:heavy_check_mark: cp-algo/linalg/strassen.hpp

Depends on

Required by

Verified with

Code

#ifndef CP_ALGO_LINALG_STRASSEN_HPP
#define CP_ALGO_LINALG_STRASSEN_HPP
#include "vector.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::linalg::impl {
    template<class row>
    constexpr bool use_strassen = false;
    template<auto mod>
    constexpr bool use_strassen<modint_vec<math::modint<mod>>> = mod > 1 && mod % 2 && mod < (1LL << 30);

    // Internal packed storage; the public matrix keeps its usual row representation.
    template<uint32_t mod>
    struct strassen_product {
        struct view {
            uint32_t* data;
            size_t stride;
            uint32_t* operator[](size_t i) const { return data + i * stride; }
        };
        static u64x4 mul(u64x4 a, u64x4 b) {
#ifdef __AVX2__
            return u64x4(_mm256_mul_epu32(__m256i(a), __m256i(b)));
#else
            return low32(a) * low32(b);
#endif
        }
        static u64x4 shrink(u64x4 x) {
            // x < 4*mod*2^32; reduce the high word modulo 2*mod.
            auto words = u32x8(x);
            auto bound = u32x8(u64x4() + (uint64_t(2) * mod << 32));
#ifdef __AVX2__
            return u64x4(_mm256_min_epu32(__m256i(words), __m256i(words - bound)));
#else
            return u64x4(words < words - bound ? words : words - bound);
#endif
        }
        template<size_t dim = 0>
        [[gnu::noinline]] static void leaf(view a, view b, view c, size_t n, size_t m, size_t k) {
            if constexpr(dim) {
                n = m = k = dim;
                a.stride = b.stride = c.stride = dim;
            }
            for(size_t i = 0; i < n; i += 4) {
                for(size_t j = 0; j < k; j += 8) {
                    u64x4 acc[4][2]{};
                    for(size_t first = 0; first < m; first += 8) {
#pragma GCC unroll 1
                        for(size_t z = first; z < first + 8; z++) {
                            u64x4 x;
                            std::memcpy(&x, b[z] + j, sizeof x);
                            u64x4 scales[4];
                            for(size_t t = 0; t < 4; t++) {
                                scales[t] = u64x4(u32x8() + a[i + t][z]);
                            }
                            for(size_t t = 0; t < 4; t++) {
                                acc[t][0] += mul(scales[t], x);
                            }
                            x >>= 32;
                            for(size_t t = 0; t < 4; t++) {
                                acc[t][1] += mul(scales[t], x);
                            }
                        }
                        for(auto &row: acc) {
                            for(auto &x: row) {
                                x = shrink(x);
                            }
                        }
                    }
                    for(size_t t = 0; t < 4; t++) {
                        constexpr uint32_t inv = math::inv2(uint32_t(-mod));
                        // x < 2*mod*2^32, so Montgomery reduction yields a value below 3*mod.
                        for(auto &x: acc[t]) {
                            x = montgomery_reduce(x, mod, inv);
                        }
                        u32x8 out = u32x8(acc[t][0] | (acc[t][1] << 32));
                        out = out < out - mod ? out : out - mod;
                        out = out < out - mod ? out : out - mod;
                        std::memcpy(c[i + t] + j, &out, sizeof out);
                    }
                }
            }
        }
        // c = a +/- b; c may alias a.
        template<bool subtract = false>
        static void combine(uint32_t* a, uint32_t* b, uint32_t* c, size_t n, size_t m) {
            for(size_t j = 0; j < n * m; j += 8) {
                u32x8 x, y;
                std::memcpy(&x, a + j, sizeof x);
                std::memcpy(&y, b + j, sizeof y);
                u32x8 z = subtract ? x + mod - y : x + y;
                z = z < z - mod ? z : z - mod;
                std::memcpy(c + j, &z, sizeof z);
            }
        }
        static bool can_split(size_t n, size_t m, size_t k) {
            return std::min({n, m, k}) > 64 && n % 16 == 0 && m % 16 == 0 && k % 16 == 0;
        }
        [[gnu::noinline]] static void multiply(uint32_t* a, uint32_t* b, uint32_t* c, size_t n, size_t m, size_t k,
                                               uint32_t* work) {
            // Every leaf dimension must remain a multiple of eight.
            if(!can_split(n, m, k)) {
                if(n == 64 && m == 64 && k == 64) {
                    leaf<64>({a, m}, {b, k}, {c, k}, n, m, k);
                } else {
                    leaf({a, m}, {b, k}, {c, k}, n, m, k);
                }
                return;
            }
            n /= 2;
            m /= 2;
            k /= 2;
            auto s = work, t = s + n * m, p = t + m * k;
            work += n * m + m * k + n * k;
            auto a00 = a, a01 = a + n * m, a10 = a + 2 * n * m, a11 = a + 3 * n * m;
            auto b00 = b, b01 = b + m * k, b10 = b + 2 * m * k, b11 = b + 3 * m * k;
            auto c00 = c, c01 = c + n * k, c10 = c + 2 * n * k, c11 = c + 3 * n * k;

            // Winograd's schedule uses seven products and fifteen additions.
            multiply(a00, b00, c11, n, m, k, work); // P1
            multiply(a01, b10, c00, n, m, k, work); // P2
            combine(c00, c11, c00, n, k);           // C00 = P1 + P2

            combine(a10, a11, s, n, m);
            combine<true>(b01, b00, t, m, k);   // S1, T1
            multiply(s, t, c01, n, m, k, work); // P5
            combine<true>(s, a00, s, n, m);
            combine<true>(b11, t, t, m, k);     // S2, T2
            multiply(s, t, c10, n, m, k, work); // P6
            combine(c11, c10, c10, n, k);       // U2 = P1 + P6

            combine<true>(a01, s, s, n, m);     // S4
            multiply(s, b11, p, n, m, k, work); // P3
            combine(c10, c01, c11, n, k);       // U4 = U2 + P5
            combine(c11, p, c01, n, k);         // C01 = U4 + P3

            combine<true>(t, b10, t, m, k);     // T4
            multiply(a11, t, p, n, m, k, work); // P4
            combine<true>(c10, p, c10, n, k);
            combine<true>(a00, a10, s, n, m);
            combine<true>(b11, b01, t, m, k); // S3, T3
            multiply(s, t, p, n, m, k, work); // P7
            combine(c10, p, c10, n, k);
            combine(c11, p, c11, n, k); // C10, C11
        }
        static u32x8 encode(u32x8 x) {
            // Scale by 2^32 with a fixed reciprocal; its quotient is off by at most one.
            constexpr uint32_t scale = (uint64_t(1) << 32) % mod;
            constexpr uint32_t quotient = (uint64_t(scale) << 32) / mod;
            auto packed = u64x4(x), q = u64x4() + quotient;
            auto lo = mul(packed, q) >> 32, hi = mul(packed >> 32, q);
            auto approx = u32x8(lo | (hi & (~uint64_t(0) << 32)));
            auto out = x * scale - approx * mod;
            return out < out - mod ? out : out - mod;
        }
        // Copy between matrix rows and contiguous recursive quadrants.
        template<bool unpack = false, int add = 0, class matrix>
        static void copy(matrix& a, uint32_t* ptr, size_t n, size_t m, size_t depth, size_t row = 0, size_t col = 0) {
            if(depth) {
                n /= 2;
                m /= 2;
                for(size_t q = 0; q < 4; q++) {
                    copy<unpack, add>(a, ptr + q * n * m, n, m, depth - 1, row + q / 2 * n, col + q % 2 * m);
                }
                return;
            }
            if(col >= a.m()) {
                return;
            }
            size_t width = std::min(m, a.m() - col);
            for(size_t i = row; i < std::min(row + n, a.n()); i++) {
                auto data = a[i].data() + col;
                auto packed = ptr + (i - row) * m;
                for(size_t j = 0; j < width; j++) {
                    if constexpr(unpack && add != 0) {
                        typename matrix::base value;
                        value.setr(packed[j]);
                        if constexpr(add == 1) {
                            data[j] += value;
                        } else {
                            data[j] -= value;
                        }
                    } else if constexpr(unpack) {
                        data[j].setr(packed[j]);
                    } else {
                        packed[j] = uint32_t(data[j].getr());
                    }
                }
            }
        }
        template<int add = 0, class matrix>
        static matrix product(matrix const& a, matrix const& b, matrix res = {}) {
            static_assert(add >= -1 && add <= 1);
            auto pad = [](size_t x) {
                return (x + 31) / 32 * 32;
            };
            size_t n = pad(a.n()), m = pad(a.m()), k = pad(b.m());
            // Each recursion level needs a quarter as much scratch; children reuse it.
            size_t entries = n * m + m * k + n * k;
            // Small products avoid repeated mmap/madvise setup for temporary storage.
            std::vector<uint32_t> small;
            big_vector<uint32_t> large;
            size_t count = entries + entries / 3;
            auto ap = count < (1 << 21) ? (small.resize(count), small.data()) : (large.resize(count), large.data());
            auto bp = ap + n * m, cp = bp + m * k;
            auto scratch = cp + n * k;
            // A, B, and C must use the same depth, including rectangular products.
            size_t depth = 0;
            for(size_t x = n, y = m, z = k; can_split(x, y, z); x /= 2, y /= 2, z /= 2) {
                depth++;
            }
            copy(a, ap, n, m, depth);
            copy(b, bp, m, k, depth);
            // Only A is scaled; each leaf's Montgomery reduction removes the factor.
            for(size_t i = 0; i < n * m; i += 8) {
                u32x8 x;
                std::memcpy(&x, ap + i, sizeof x);
                x = encode(x);
                std::memcpy(ap + i, &x, sizeof x);
            }
            multiply(ap, bp, cp, n, m, k, scratch);
            if constexpr(add == 0) {
                res = matrix(a.n(), b.m());
            }
            assert(res.n() == a.n() && res.m() == b.m());
            copy<true, add>(res, cp, n, k, depth);
            return res;
        }
    };
}
#pragma GCC pop_options
#endif // CP_ALGO_LINALG_STRASSEN_HPP
#line 1 "cp-algo/linalg/strassen.hpp"


#line 1 "cp-algo/linalg/vector.hpp"


#line 1 "cp-algo/random/rng.hpp"


#include <chrono>
#include <random>
namespace cp_algo::random {
    std::mt19937_64 gen(
        std::chrono::steady_clock::now().time_since_epoch().count()
    );
    uint64_t rng() {
        return gen();
    }
}

#line 1 "cp-algo/number_theory/modint.hpp"


#line 1 "cp-algo/math/common.hpp"


#include <functional>
#include <cstdint>
#include <cassert>
#include <bit>
#include <vector>
#include <algorithm>
namespace cp_algo::math {
#ifdef CP_ALGO_MAXN
    const int maxn = CP_ALGO_MAXN;
#else
    const int maxn = 1 << 19;
#endif
    const int magic = 64; // threshold for sizes to run the naive algo

    // Nonnegative 64-bit exponents, with an associative operation and its identity.
    // Windows >1 precompute odd powers only when that saves operations.
    template<int window = 1>
    auto bpow(auto const& x, auto n, auto const& one, auto op) {
        static_assert(window >= 1 && window <= 6);
        if constexpr(window > 1) {
            if(n == 0) {return one;}
            int bits = std::bit_width(uint64_t(n));
            auto low_bit = [&](int high) {
                int low = std::max(0, high - window + 1);
                while(!((n >> low) & 1)) {low++;}
                return low;
            };
            int first = low_bit(bits - 1);
            int cost = (1 << (window - 1)) + first;
            for(int j = first - 1; j >= 0;) {
                if(!((n >> j) & 1)) {j--;}
                else {cost++; j = low_bit(j) - 1;}
            }
            // Do not pay for the table when binary powering uses fewer operations.
            if(cost >= bits + std::popcount(uint64_t(n)) - 2) {return bpow<1>(x, n, one, op);}
            using T = std::decay_t<decltype(x)>;
            std::vector<T> odd;
            odd.reserve(1 << (window - 1));
            odd.push_back(x);
            auto square = op(x, x);
            while(odd.size() < size_t(1 << (window - 1))) {odd.push_back(op(odd.back(), square));}
            auto ans = odd[(n >> first) / 2];
            for(int j = first - 1; j >= 0;) {
                if(!((n >> j) & 1)) {ans = op(ans, ans); j--;}
                else {
                    int low = low_bit(j), length = j - low + 1;
                    auto digit = (n >> low) & ((1u << length) - 1);
                    for(int i = 0; i < length; i++) {ans = op(ans, ans);}
                    ans = op(ans, odd[digit / 2]);
                    j = low - 1;
                }
            }
            return ans;
        } else {
            if (n == 0) {
                return one;
            }
            auto ans = x;
            for(int j = std::bit_width<uint64_t>(n) - 2; ~j; j--) {
                ans = op(ans, ans);
                if((n >> j) & 1) {
                    ans = op(ans, x);
                }
            }
            return ans;
        }
    }
    template<int window = 1>
    auto bpow(auto x, auto n, auto ans) {
        return bpow<window>(x, n, ans, std::multiplies{});
    }
    template<typename T>
    T bpow(T const& x, auto n) {
        return bpow(x, n, T(1));
    }
    inline constexpr auto inv2(auto x) {
        assert(x % 2);
        std::make_unsigned_t<decltype(x)> y = 1;
        while(y * x != 1) {
            y *= 2 - x * y;
        }
        return y;
    }
}

#line 4 "cp-algo/number_theory/modint.hpp"
#include <iostream>
#line 6 "cp-algo/number_theory/modint.hpp"
namespace cp_algo::math {

    template<typename modint, typename _Int>
    struct modint_base {
        using Int = _Int;
        using UInt = std::make_unsigned_t<Int>;
        static constexpr size_t bits = sizeof(Int) * 8;
        using Int2 = std::conditional_t<bits <= 32, int64_t, __int128_t>;
        using UInt2 = std::conditional_t<bits <= 32, uint64_t, __uint128_t>;
        constexpr static Int mod() {
            return modint::mod();
        }
        constexpr static UInt remod() {
            return modint::remod();
        }
        constexpr static UInt2 modmod() {
            return UInt2(mod()) * mod();
        }
        constexpr modint_base() = default;
        constexpr modint_base(Int2 rr) {
            to_modint().setr(UInt((rr + modmod()) % mod()));
        }
        constexpr modint inv() const {
            return bpow(to_modint(), mod() - 2);
        }
        modint operator-() const {
            modint neg;
            neg.r = std::min(-r, remod() - r);
            return neg;
        }
        modint& operator/=(const modint& t) {
            return to_modint() *= t.inv();
        }
        modint& operator*=(const modint& t) {
            r = UInt(UInt2(r) * t.r % mod());
            return to_modint();
        }
        modint& operator+=(const modint& t) {
            r += t.r;
            r = std::min(r, r - remod());
            return to_modint();
        }
        modint& operator-=(const modint& t) {
            r -= t.r;
            r = std::min(r, r + remod());
            return to_modint();
        }
        modint operator+(const modint& t) const { return modint(to_modint()) += t; }
        modint operator-(const modint& t) const { return modint(to_modint()) -= t; }
        modint operator*(const modint& t) const { return modint(to_modint()) *= t; }
        modint operator/(const modint& t) const { return modint(to_modint()) /= t; }
        // Why <=> doesn't work?..
        auto operator==(const modint& t) const { return to_modint().getr() == t.getr(); }
        auto operator!=(const modint& t) const { return to_modint().getr() != t.getr(); }
        auto operator<=(const modint& t) const { return to_modint().getr() <= t.getr(); }
        auto operator>=(const modint& t) const { return to_modint().getr() >= t.getr(); }
        auto operator<(const modint& t) const { return to_modint().getr() < t.getr(); }
        auto operator>(const modint& t) const { return to_modint().getr() > t.getr(); }
        Int rem() const {
            UInt R = to_modint().getr();
            return R - (R > (UInt)mod() / 2) * mod();
        }
        constexpr void setr(UInt rr) {
            r = rr;
        }
        constexpr UInt getr() const {
            return r;
        }

        // Only use these if you really know what you're doing!
        static uint64_t modmod8() { return uint64_t(8 * modmod()); }
        void add_unsafe(UInt t) { r += t; }
        void pseudonormalize() { r = std::min(r, r - modmod8()); }
        modint const& normalize() {
            if(r >= (UInt)mod()) {
                r %= mod();
            }
            return to_modint();
        }
        void setr_direct(UInt rr) { r = rr; }
        UInt getr_direct() const { return r; }

    protected:
        UInt r;

    private:
        constexpr modint& to_modint() { return static_cast<modint&>(*this); }
        constexpr modint const& to_modint() const { return static_cast<modint const&>(*this); }
    };
    template<typename modint>
    concept modint_type = std::is_base_of_v<modint_base<modint, typename modint::Int>, modint>;
    template<modint_type modint>
    decltype(std::cin)& operator>>(decltype(std::cin)& in, modint& x) {
        typename modint::UInt r;
        auto &res = in >> r;
        x.setr(r);
        return res;
    }
    template<modint_type modint>
    decltype(std::cout)& operator<<(decltype(std::cout)& out, modint const& x) {
        return out << x.getr();
    }

    template<auto m>
    struct modint: modint_base<modint<m>, decltype(m)> {
        using Base = modint_base<modint<m>, decltype(m)>;
        using Base::Base;
        static constexpr Base::Int mod() { return m; }
        static constexpr Base::UInt remod() { return m; }
        auto getr() const { return Base::r; }
    };

    // Odd moduli up to a quarter of the unsigned word keep Montgomery residues lazily in
    // [0, 2 mod): remod() = 2 mod, and both 4 mod and ab + q * mod fit. Any other modulus keeps
    // fully reduced residues, remod() = mod, which is what the sums of modint_base need then:
    // an even one without the Montgomery form, a wide odd one with a reduction that subtracts
    // high words instead of adding double words that would overflow.
    template<typename Int = int>
    struct dynamic_modint: modint_base<dynamic_modint<Int>, Int> {
        using Base = modint_base<dynamic_modint<Int>, Int>;
        using Base::Base;

        // Out of line, so that the hot path stays as small as it was.
        [[gnu::noinline, gnu::cold]] static Base::UInt m_reduce_reduced(Base::UInt2 ab) {
            if(mod() % 2 == 0) {
                return typename Base::UInt(ab % mod());
            }
            // q * mod has the low word of ab, so the difference of the high words is exact.
            typename Base::UInt q = -(typename Base::UInt(ab) * inverse);
            auto high = typename Base::UInt(ab >> Base::bits);
            auto low = typename Base::UInt(typename Base::UInt2(q) * typename Base::UInt(mod()) >> Base::bits);
            return high >= low ? high - low : high - low + mod();
        }
        static Base::UInt m_reduce(Base::UInt2 ab) {
            if(imod() == 0) [[unlikely]] {
                return m_reduce_reduced(ab);
            }
            typename Base::UInt2 m = typename Base::UInt(ab) * imod();
            return typename Base::UInt((ab + m * mod()) >> Base::bits);
        }
        static Base::UInt m_transform(Base::UInt a) {
            if(mod() % 2 == 0) [[unlikely]] {
                return a;
            } else {
                return m_reduce(a * pw128());
            }
        }
        dynamic_modint& operator*=(const dynamic_modint& t) {
            Base::r = m_reduce(typename Base::UInt2(Base::r) * t.r);
            return *this;
        }
        void setr(Base::UInt rr) {
            Base::r = m_transform(rr);
        }
        Base::UInt getr() const {
            typename Base::UInt res = m_reduce(Base::r);
            return std::min(res, res - mod());
        }
        static Int mod() { return m; }
        static Base::UInt remod() { return rm; }
        static Base::UInt imod() { return im; }
        static Base::UInt2 pw128() { return r2; }
        static void switch_mod(Int nm) {
            m = nm;
            bool lazy = m % 2 && typename Base::UInt(m) <= typename Base::UInt(-1) / 4;
            rm = typename Base::UInt(m) * (lazy ? 2 : 1);
            inverse = m % 2 ? inv2(-m) : 0;
            im = lazy ? inverse : 0;
            r2 = static_cast<Base::UInt>(static_cast<Base::UInt2>(-1) % m + 1);
        }

        // Wrapper for temp switching
        auto static with_mod(Int tmp, auto callback) {
            struct scoped {
                Int prev = mod();
                ~scoped() { switch_mod(prev); }
            } _;
            switch_mod(tmp);
            return callback();
        }

    private:
        static inline thread_local Int m = 1;
        // im: -1 / mod modulo 2^bits for lazy residues and 0 for reduced ones; inverse: the same
        // for every odd mod; rm: the value of remod().
        static inline thread_local Base::UInt im = -1, r2 = 0, inverse = -1, rm = 2;
    };
}

#line 1 "cp-algo/util/big_alloc.hpp"



#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#line 10 "cp-algo/util/big_alloc.hpp"
#include <string>
#include <cstddef>
#line 14 "cp-algo/util/big_alloc.hpp"
#include <forward_list>

// Single macro to detect POSIX platforms (Linux, Unix, macOS)
#if defined(__linux__) || defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#define CP_ALGO_USE_MMAP 1
#include <sys/mman.h>
#else
#define CP_ALGO_USE_MMAP 0
#endif

namespace cp_algo {
    template<typename T, size_t Align = 32>
    class big_alloc {
        static_assert(Align >= alignof(void*), "Align must be at least pointer-size");
        static_assert(std::has_single_bit(Align), "Align must be a power of two");

    public:
        using value_type = T;
        template<class U>
        struct rebind {
            using other = big_alloc<U, Align>;
        };
        constexpr bool operator==(const big_alloc&) const = default;

        big_alloc() noexcept = default;
        template<typename U, std::size_t A>
        big_alloc(const big_alloc<U, A>&) noexcept {}

        [[nodiscard]] T* allocate(std::size_t n) {
            std::size_t padded = round_up(n * sizeof(T));
            std::size_t align = std::max<std::size_t>(alignof(T), Align);
#if CP_ALGO_USE_MMAP
            if(padded >= MEGABYTE) {
                void* raw = mmap(nullptr, padded, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
                madvise(raw, padded, MADV_HUGEPAGE);
                return static_cast<T*>(raw);
            }
#endif
            return static_cast<T*>(::operator new(padded, std::align_val_t(align)));
        }

        void deallocate(T* p, std::size_t n) noexcept {
            if(!p) {
                return;
            }
            std::size_t padded = round_up(n * sizeof(T));
            std::size_t align = std::max<std::size_t>(alignof(T), Align);
#if CP_ALGO_USE_MMAP
            if(padded >= MEGABYTE) {
                munmap(p, padded);
                return;
            }
#endif
            ::operator delete(p, padded, std::align_val_t(align));
        }

    private:
        static constexpr std::size_t MEGABYTE = 1 << 20;
        static constexpr std::size_t round_up(std::size_t x) noexcept {
            return (x + Align - 1) / Align * Align;
        }
    };

    template<typename T>
    using big_vector = std::vector<T, big_alloc<T>>;
    template<typename T>
    using big_basic_string = std::basic_string<T, std::char_traits<T>, big_alloc<T>>;
    template<typename T>
    using big_deque = std::deque<T, big_alloc<T>>;
    template<typename T>
    using big_stack = std::stack<T, big_deque<T>>;
    template<typename T>
    using big_queue = std::queue<T, big_deque<T>>;
    template<typename T>
    using big_priority_queue = std::priority_queue<T, big_vector<T>>;
    template<typename T>
    using big_forward_list = std::forward_list<T, big_alloc<T>>;
    using big_string = big_basic_string<char>;

    template<typename Key, typename Value, typename Compare = std::less<Key>>
    using big_map = std::map<Key, Value, Compare, big_alloc<std::pair<const Key, Value>>>;
    template<typename T, typename Compare = std::less<T>>
    using big_multiset = std::multiset<T, Compare, big_alloc<T>>;
    template<typename T, typename Compare = std::less<T>>
    using big_set = std::set<T, Compare, big_alloc<T>>;
}


#line 1 "cp-algo/util/simd.hpp"


#include <experimental/simd>
#line 6 "cp-algo/util/simd.hpp"
#include <memory>

#if defined(__x86_64__) && !defined(CP_ALGO_DISABLE_AVX2)
#define CP_ALGO_SIMD_AVX2_TARGET _Pragma("GCC target(\"avx2,fma\")")
#else
#define CP_ALGO_SIMD_AVX2_TARGET
#endif

#define CP_ALGO_SIMD_PRAGMA_PUSH \
    _Pragma("GCC push_options") \
    CP_ALGO_SIMD_AVX2_TARGET

CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo {
    template<typename T, size_t len>
    using simd [[gnu::vector_size(len * sizeof(T))]] = T;
    using u64x8 = simd<uint64_t, 8>;
    using u32x16 = simd<uint32_t, 16>;
    using i64x4 = simd<int64_t, 4>;
    using u64x4 = simd<uint64_t, 4>;
    using u32x8 = simd<uint32_t, 8>;
    using u16x16 = simd<uint16_t, 16>;
    using i32x4 = simd<int32_t, 4>;
    using u32x4 = simd<uint32_t, 4>;
    using u16x8 = simd<uint16_t, 8>;
    using u16x4 = simd<uint16_t, 4>;
    using i16x4 = simd<int16_t, 4>;
    using u8x32 = simd<uint8_t, 32>;
    using u8x16 = simd<uint8_t, 16>;
    using u8x8 = simd<uint8_t, 8>;
    using u8x4 = simd<uint8_t, 4>;
    using dx4 = simd<double, 4>;

    inline dx4 abs(dx4 a) {
        return dx4{
            std::abs(a[0]),
            std::abs(a[1]),
            std::abs(a[2]),
            std::abs(a[3])
        };
    }

    // https://stackoverflow.com/a/77376595
    // works for ints in (-2^51, 2^51)
    static constexpr dx4 magic = dx4() + (3ULL << 51);
    inline i64x4 lround(dx4 x) {
        return i64x4(x + magic) - i64x4(magic);
    }
    inline dx4 to_double(i64x4 x) {
        return dx4(x + i64x4(magic)) - magic;
    }

    inline dx4 round(dx4 a) {
        return dx4{
            std::nearbyint(a[0]),
            std::nearbyint(a[1]),
            std::nearbyint(a[2]),
            std::nearbyint(a[3])
        };
    }

    inline u64x4 low32(u64x4 x) {
        return x & uint32_t(-1);
    }
    inline auto swap_bytes(auto x) {
        return decltype(x)(__builtin_shufflevector(u32x8(x), u32x8(x), 1, 0, 3, 2, 5, 4, 7, 6));
    }
    inline u64x4 montgomery_reduce(u64x4 x, uint32_t mod, uint32_t imod) {
#ifdef __AVX2__
        auto x_ninv = u64x4(_mm256_mul_epu32(__m256i(x), __m256i() + imod));
        x += u64x4(_mm256_mul_epu32(__m256i(x_ninv), __m256i() + mod));
#else
        auto x_ninv = u64x4(u32x8(low32(x)) * imod);
        x += x_ninv * uint64_t(mod);
#endif
        return swap_bytes(x);
    }

    // x - mod where x >= mod, for x in [0, 2 mod). Unsigned, so it holds up to mod < 2^31;
    // zero upper halves of 64-bit lanes stay zero.
    inline u32x8 reduce_once(u32x8 x, uint32_t mod) {
        auto y = x - mod;
        return x < y ? x : y;
    }
    inline u64x4 reduce_once(u64x4 x, uint32_t mod) {
        return u64x4(reduce_once(u32x8(x), mod));
    }

    inline u64x4 montgomery_mul(u64x4 x, u64x4 y, uint32_t mod, uint32_t imod) {
#ifdef __AVX2__
        return montgomery_reduce(u64x4(_mm256_mul_epu32(__m256i(x), __m256i(y))), mod, imod);
#else
        return montgomery_reduce(x * y, mod, imod);
#endif
    }
    inline u32x8 montgomery_mul(u32x8 x, u32x8 y, uint32_t mod, uint32_t imod) {
        return u32x8(montgomery_mul(u64x4(x), u64x4(y), mod, imod)) |
               u32x8(swap_bytes(montgomery_mul(u64x4(swap_bytes(x)), u64x4(swap_bytes(y)), mod, imod)));
    }
    inline dx4 rotate_right(dx4 x) {
        static constexpr u64x4 shuffler = {3, 0, 1, 2};
        return __builtin_shuffle(x, shuffler);
    }

    template<std::size_t Align = 32>
    inline bool is_aligned(const auto *p) noexcept {
        return (reinterpret_cast<std::uintptr_t>(p) % Align) == 0;
    }

    template<class Target>
    inline Target& vector_cast(auto &&p) {
        return *reinterpret_cast<Target*>(std::assume_aligned<alignof(Target)>(&p));
    }
}
#pragma GCC pop_options

#line 1 "cp-algo/util/checkpoint.hpp"


#line 8 "cp-algo/util/checkpoint.hpp"
namespace cp_algo {
#ifdef CP_ALGO_CHECKPOINT
    big_map<big_string, double> checkpoints;
    double last;
#endif
    template<bool final = false>
    void checkpoint([[maybe_unused]] auto const& _msg) {
#ifdef CP_ALGO_CHECKPOINT
        big_string msg = _msg;
        double now = (double)clock() / CLOCKS_PER_SEC;
        double delta = now - last;
        last = now;
        if(msg.size() && !final) {
            checkpoints[msg] += delta;
        }
        if(final) {
            for(auto const& [key, value] : checkpoints) {
                std::cerr << key << ": " << value * 1000 << " ms\n";
            }
            std::cerr << "Total: " << now * 1000 << " ms\n";
        }
#endif
    }
    template<bool final = false>
    void checkpoint() {
        checkpoint<final>("");
    }
}

#line 10 "cp-algo/linalg/vector.hpp"
#include <valarray>
#line 12 "cp-algo/linalg/vector.hpp"
#include <iterator>
#line 14 "cp-algo/linalg/vector.hpp"
#include <ranges>
#include <array>
#include <cstring>
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::linalg {
    template<typename base, class Alloc = big_alloc<base>>
    struct vec: std::basic_string<base, std::char_traits<base>, Alloc> {
        using Base = std::basic_string<base, std::char_traits<base>, Alloc>;
        using Base::Base;

        vec(Base const& t): Base(t) {}
        vec(Base&& t): Base(std::move(t)) {}
        vec(size_t n): Base(n, base()) {}
        vec(auto &&r): Base(r.begin(), r.end()) {}

        static vec ei(size_t n, size_t i) {
            vec res(n);
            res[i] = 1;
            return res;
        }

        auto operator-() const {
            return *this | std::views::transform([](auto x) { return -x; });
        }
        auto operator*(base t) const {
            return *this | std::views::transform([t](auto x) { return x * t; });
        }
        vec& operator*=(base t) {
            for(auto &it: *this) {
                it *= t;
            }
            return *this;
        }

        virtual void add_scaled(vec const& b, base scale, size_t i = 0) {
            if(scale != base(0)) {
                for(; i < size(*this); i++) {
                    (*this)[i] += scale * b[i];
                }
            }
        }
        virtual vec const& normalize() {
            return static_cast<vec&>(*this);
        }
        virtual base normalize(size_t i) {
            return (*this)[i];
        }
        void read() {
            for(auto &it: *this) {
                std::cin >> it;
            }
        }
        void print() const {
            for(auto &it: *this) {
                std::cout << it << ' ';
            }
            std::cout << '\n';
        }
        static vec random(size_t n) {
            vec res(n);
            std::ranges::generate(res, random::rng);
            return res;
        }
        // Concatenate vectors
        vec operator|(vec const& t) const {
            return std::views::join(std::array{std::views::all(*this), std::views::all(t)});
        }

        // Generally, vec shouldn't be modified
        // after its pivot index is set
        std::pair<size_t, base> find_pivot() {
            if(pivot == size_t(-1)) {
                pivot = 0;
                while(pivot < size(*this) && normalize(pivot) == base(0)) {
                    pivot++;
                }
                if(pivot < size(*this)) {
                    pivot_inv = base(1) / (*this)[pivot];
                }
            }
            return {pivot, pivot_inv};
        }
        void reduce_by(vec& t) {
            auto [pivot, pinv] = t.find_pivot();
            if(pivot < size(*this)) {
                add_scaled(t, -normalize(pivot) * pinv, pivot);
            }
        }

    private:
        size_t pivot = -1;
        base pivot_inv;
    };

    template<math::modint_type base, class Alloc = big_alloc<base>>
    struct modint_vec: vec<base, Alloc> {
        using Base = vec<base, Alloc>;
        using Base::Base;

        modint_vec(Base const& t): Base(t) {}
        modint_vec(Base&& t): Base(std::move(t)) {}

        void add_scaled(Base const& b, base scale, size_t i = 0) override {
            static_assert(base::bits >= 64, "Only wide modint types for linalg");
            if(scale != base(0)) {
                assert(Base::size() == b.size());
                size_t n = size(*this);
                u64x4 scaler = u64x4() + scale.getr();
                bool aligned = is_aligned(&(*this)[0]) && is_aligned(&b[0]);
                if(aligned) {
                    i -= i % 4;
                }
                bool reduce = ++counter == accumulation_period();
                if(reduce) {
                    counter = 0;
                    for(size_t j = 0; j < i; j++) {
                        (*this)[j].pseudonormalize();
                    }
                }
                if(aligned) {
                    for(; i + 3 < n; i += 4) {
                        auto &ai = vector_cast<u64x4>((*this)[i]);
                        auto bi = vector_cast<u64x4 const>(b[i]);
#ifdef __AVX2__
                        ai += u64x4(_mm256_mul_epu32(__m256i(scaler), __m256i(bi)));
#else
                        ai += scaler * bi;
#endif
                        if(reduce) {
                            ai = shrink(ai);
                        }
                    }
                }
                for(; i < n; i++) {
                    (*this)[i].add_unsafe(b[i].getr_direct() * scale.getr());
                    if(reduce) {
                        (*this)[i].pseudonormalize();
                    }
                }
            }
        }
        Base const& normalize() override {
            for(auto &it: *this) {
                it.normalize();
            }
            return *this;
        }
        base normalize(size_t i) override {
            return (*this)[i].normalize();
        }

    private:
        template<typename, typename>
        friend struct matrix;
        static size_t accumulation_period() {
            // Eight canonical products keep the accumulator below 16 * mod^2.
            // Montgomery residues can be wider, so retain four updates there.
            return base::remod() == typename base::UInt(base::mod()) && base::mod() < (1LL << 30) ? 8 : 4;
        }
        static u64x4 mul(u64x4 a, u64x4 b) {
#ifdef __AVX2__
            return u64x4(_mm256_mul_epu32(__m256i(a), __m256i(b)));
#else
            return a * b;
#endif
        }
        static u64x4 shrink(u64x4 a) {
            auto b = a - (u64x4() + base::modmod8());
            return a < b ? a : b;
        }
        // Two source contributions to two distinct rows; sources must be normalized.
        static void add_scaled_pair(modint_vec& x, modint_vec& y, Base const& p, Base const& q, std::array<base, 4> c,
                                    size_t first = 0) {
            if(std::ranges::find(c, base(0)) != c.end()) {
                x.add_scaled(p, c[0], first);
                x.add_scaled(q, c[1], first);
                y.add_scaled(p, c[2], first);
                y.add_scaled(q, c[3], first);
                return;
            }
            size_t n = x.size();
            assert(y.size() == n && p.size() == n && q.size() == n && first <= n);
            // Before first, sources are zero for elimination or destinations are unused for Pfaffians.
            if(is_aligned(x.data()) && is_aligned(y.data()) && is_aligned(p.data()) && is_aligned(q.data())) {
                first -= first % 4;
            }
            size_t period = accumulation_period();
            auto prepare = [&](modint_vec& a) {
                // A pair must not cross the accumulator's reduction boundary.
                if(a.counter + 2 > period) {
                    for(auto &v: a) {
                        v.pseudonormalize();
                    }
                    a.counter = 0;
                }
                a.counter += 2;
                if(a.counter != period) {
                    return false;
                }
                a.counter = 0;
                for(size_t i = 0; i < first; i++) {
                    a[i].pseudonormalize();
                }
                return true;
            };
            bool nx = prepare(x), ny = prepare(y);
            auto *__restrict__ dx = x.data();
            auto *__restrict__ dy = y.data();
            auto const* __restrict__ sp = p.data();
            auto const* __restrict__ sq = q.data();
            uint64_t xp = c[0].getr(), xq = c[1].getr(), yp = c[2].getr(), yq = c[3].getr();
            u64x4 xp4 = u64x4() + xp, xq4 = u64x4() + xq;
            u64x4 yp4 = u64x4() + yp, yq4 = u64x4() + yq;
            size_t i = first;
            for(; i + 4 <= n; i += 4) {
                u64x4 vx, vy, vp, vq;
                std::memcpy(&vx, dx + i, sizeof vx);
                std::memcpy(&vy, dy + i, sizeof vy);
                std::memcpy(&vp, sp + i, sizeof vp);
                std::memcpy(&vq, sq + i, sizeof vq);
                vx += mul(xp4, vp) + mul(xq4, vq);
                vy += mul(yp4, vp) + mul(yq4, vq);
                if(nx) {
                    vx = shrink(vx);
                }
                if(ny) {
                    vy = shrink(vy);
                }
                std::memcpy(dx + i, &vx, sizeof vx);
                std::memcpy(dy + i, &vy, sizeof vy);
            }
            for(; i < n; i++) {
                dx[i].add_unsafe(xp * sp[i].getr_direct() + xq * sq[i].getr_direct());
                dy[i].add_unsafe(yp * sp[i].getr_direct() + yq * sq[i].getr_direct());
                if(nx) {
                    dx[i].pseudonormalize();
                }
                if(ny) {
                    dy[i].pseudonormalize();
                }
            }
        }
        size_t counter = 0;
    };

    // Narrow rows keep canonical residues; small batches accumulate in wide registers.
    template<typename base>
        requires(base::bits <= 32)
    struct modint_vec<base>: vec<base> {
        using Base = vec<base>;
        using Base::Base;
        modint_vec(Base const& t): Base(t) {}
        modint_vec(Base&& t): Base(std::move(t)) {}

        void add_scaled(Base const& b, base scale, size_t first = 0) override {
            if(scale == base(0)) {
                return;
            }
            if(&b == this) {
                Base::add_scaled(b, scale, first);
            } else {
                add_scaled_batch<1, 1>({this}, {&b}, {scale}, first);
            }
        }

    private:
        template<typename, typename>
        friend struct matrix;
        static constexpr size_t batch_size = 8;
        static constexpr bool use_simd = [] {
            if constexpr(requires {
                std::integral_constant<uint32_t, base::mod()>{};
                std::integral_constant<uint32_t, base::remod()>{};
            }) {
                return sizeof(base) == sizeof(uint32_t) && base::mod() > 1 && base::mod() % 2 &&
                       base::mod() < (1U << 30) && base::remod() == typename base::UInt(base::mod());
            } else {
                return false;
            }
        }();
        static u64x4 mul(u64x4 a, u64x4 b) {
#ifdef __AVX2__
            return u64x4(_mm256_mul_epu32(__m256i(a), __m256i(b)));
#else
            return low32(a) * low32(b);
#endif
        }
        // Sources are normalized and do not alias the distinct destination rows.
        template<size_t count, size_t rows>
        static void add_scaled_batch(std::array<modint_vec*, rows> const& dst,
                                     std::array<Base const*, count> const& src, std::array<base, rows * count> const& c,
                                     size_t first = 0) {
            static_assert(count <= batch_size && (rows == 1 || rows == 2));
            if(std::ranges::all_of(c, [](base value) { return value == base(0); })) {
                return;
            }
            if constexpr(use_simd) {
                constexpr uint32_t mod = base::mod(), inv = math::inv2(uint32_t(-mod));
                std::array<uint32_t, rows * count> scale;
                for(size_t t = 0; t < rows * count; t++) {
                    scale[t] = uint32_t((uint64_t(c[t].getr()) << 32) % mod);
                }
                auto *__restrict__ dx = dst[0]->data();
                auto *__restrict__ dy = rows == 2 ? dst[1]->data() : nullptr;
                size_t n = dst[0]->size();
                for(; first + 8 <= n; first += 8) {
                    u64x4 acc[rows][2]{};
#pragma GCC unroll 1
                    for(size_t t = 0; t < count; t++) {
                        u64x4 p;
                        std::memcpy(&p, src[t]->data() + first, sizeof p);
                        auto q = p >> 32;
                        for(size_t row = 0; row < rows; row++) {
                            auto v = u64x4(u32x8() + scale[row * count + t]);
                            acc[row][0] += mul(p, v);
                            acc[row][1] += mul(q, v);
                        }
                    }
                    for(size_t row = 0; row < rows; row++) {
                        // At most eight products: reduction gives <3*mod, then add the old residue.
                        for(auto &v: acc[row]) {
                            v = montgomery_reduce(v, mod, inv);
                        }
                        auto *out = (row ? dy : dx) + first;
                        u32x8 old;
                        std::memcpy(&old, out, sizeof old);
                        auto z = old + u32x8(acc[row][0] | (acc[row][1] << 32));
                        z = z < z - 2 * mod ? z : z - 2 * mod;
                        z = z < z - mod ? z : z - mod;
                        std::memcpy(out, &z, sizeof z);
                    }
                }
            }
            for(size_t t = 0; t < count; t++) {
                for(size_t row = 0; row < rows; row++) {
                    dst[row]->Base::add_scaled(*src[t], c[row * count + t], first);
                }
            }
        }
        static void add_scaled_pair(modint_vec& x, modint_vec& y, Base const& p, Base const& q, std::array<base, 4> c,
                                    size_t first = 0) {
            add_scaled_batch<2, 2>({&x, &y}, {&p, &q}, c, first);
        }
    };
}
#pragma GCC pop_options

#line 4 "cp-algo/linalg/strassen.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::linalg::impl {
    template<class row>
    constexpr bool use_strassen = false;
    template<auto mod>
    constexpr bool use_strassen<modint_vec<math::modint<mod>>> = mod > 1 && mod % 2 && mod < (1LL << 30);

    // Internal packed storage; the public matrix keeps its usual row representation.
    template<uint32_t mod>
    struct strassen_product {
        struct view {
            uint32_t* data;
            size_t stride;
            uint32_t* operator[](size_t i) const { return data + i * stride; }
        };
        static u64x4 mul(u64x4 a, u64x4 b) {
#ifdef __AVX2__
            return u64x4(_mm256_mul_epu32(__m256i(a), __m256i(b)));
#else
            return low32(a) * low32(b);
#endif
        }
        static u64x4 shrink(u64x4 x) {
            // x < 4*mod*2^32; reduce the high word modulo 2*mod.
            auto words = u32x8(x);
            auto bound = u32x8(u64x4() + (uint64_t(2) * mod << 32));
#ifdef __AVX2__
            return u64x4(_mm256_min_epu32(__m256i(words), __m256i(words - bound)));
#else
            return u64x4(words < words - bound ? words : words - bound);
#endif
        }
        template<size_t dim = 0>
        [[gnu::noinline]] static void leaf(view a, view b, view c, size_t n, size_t m, size_t k) {
            if constexpr(dim) {
                n = m = k = dim;
                a.stride = b.stride = c.stride = dim;
            }
            for(size_t i = 0; i < n; i += 4) {
                for(size_t j = 0; j < k; j += 8) {
                    u64x4 acc[4][2]{};
                    for(size_t first = 0; first < m; first += 8) {
#pragma GCC unroll 1
                        for(size_t z = first; z < first + 8; z++) {
                            u64x4 x;
                            std::memcpy(&x, b[z] + j, sizeof x);
                            u64x4 scales[4];
                            for(size_t t = 0; t < 4; t++) {
                                scales[t] = u64x4(u32x8() + a[i + t][z]);
                            }
                            for(size_t t = 0; t < 4; t++) {
                                acc[t][0] += mul(scales[t], x);
                            }
                            x >>= 32;
                            for(size_t t = 0; t < 4; t++) {
                                acc[t][1] += mul(scales[t], x);
                            }
                        }
                        for(auto &row: acc) {
                            for(auto &x: row) {
                                x = shrink(x);
                            }
                        }
                    }
                    for(size_t t = 0; t < 4; t++) {
                        constexpr uint32_t inv = math::inv2(uint32_t(-mod));
                        // x < 2*mod*2^32, so Montgomery reduction yields a value below 3*mod.
                        for(auto &x: acc[t]) {
                            x = montgomery_reduce(x, mod, inv);
                        }
                        u32x8 out = u32x8(acc[t][0] | (acc[t][1] << 32));
                        out = out < out - mod ? out : out - mod;
                        out = out < out - mod ? out : out - mod;
                        std::memcpy(c[i + t] + j, &out, sizeof out);
                    }
                }
            }
        }
        // c = a +/- b; c may alias a.
        template<bool subtract = false>
        static void combine(uint32_t* a, uint32_t* b, uint32_t* c, size_t n, size_t m) {
            for(size_t j = 0; j < n * m; j += 8) {
                u32x8 x, y;
                std::memcpy(&x, a + j, sizeof x);
                std::memcpy(&y, b + j, sizeof y);
                u32x8 z = subtract ? x + mod - y : x + y;
                z = z < z - mod ? z : z - mod;
                std::memcpy(c + j, &z, sizeof z);
            }
        }
        static bool can_split(size_t n, size_t m, size_t k) {
            return std::min({n, m, k}) > 64 && n % 16 == 0 && m % 16 == 0 && k % 16 == 0;
        }
        [[gnu::noinline]] static void multiply(uint32_t* a, uint32_t* b, uint32_t* c, size_t n, size_t m, size_t k,
                                               uint32_t* work) {
            // Every leaf dimension must remain a multiple of eight.
            if(!can_split(n, m, k)) {
                if(n == 64 && m == 64 && k == 64) {
                    leaf<64>({a, m}, {b, k}, {c, k}, n, m, k);
                } else {
                    leaf({a, m}, {b, k}, {c, k}, n, m, k);
                }
                return;
            }
            n /= 2;
            m /= 2;
            k /= 2;
            auto s = work, t = s + n * m, p = t + m * k;
            work += n * m + m * k + n * k;
            auto a00 = a, a01 = a + n * m, a10 = a + 2 * n * m, a11 = a + 3 * n * m;
            auto b00 = b, b01 = b + m * k, b10 = b + 2 * m * k, b11 = b + 3 * m * k;
            auto c00 = c, c01 = c + n * k, c10 = c + 2 * n * k, c11 = c + 3 * n * k;

            // Winograd's schedule uses seven products and fifteen additions.
            multiply(a00, b00, c11, n, m, k, work); // P1
            multiply(a01, b10, c00, n, m, k, work); // P2
            combine(c00, c11, c00, n, k);           // C00 = P1 + P2

            combine(a10, a11, s, n, m);
            combine<true>(b01, b00, t, m, k);   // S1, T1
            multiply(s, t, c01, n, m, k, work); // P5
            combine<true>(s, a00, s, n, m);
            combine<true>(b11, t, t, m, k);     // S2, T2
            multiply(s, t, c10, n, m, k, work); // P6
            combine(c11, c10, c10, n, k);       // U2 = P1 + P6

            combine<true>(a01, s, s, n, m);     // S4
            multiply(s, b11, p, n, m, k, work); // P3
            combine(c10, c01, c11, n, k);       // U4 = U2 + P5
            combine(c11, p, c01, n, k);         // C01 = U4 + P3

            combine<true>(t, b10, t, m, k);     // T4
            multiply(a11, t, p, n, m, k, work); // P4
            combine<true>(c10, p, c10, n, k);
            combine<true>(a00, a10, s, n, m);
            combine<true>(b11, b01, t, m, k); // S3, T3
            multiply(s, t, p, n, m, k, work); // P7
            combine(c10, p, c10, n, k);
            combine(c11, p, c11, n, k); // C10, C11
        }
        static u32x8 encode(u32x8 x) {
            // Scale by 2^32 with a fixed reciprocal; its quotient is off by at most one.
            constexpr uint32_t scale = (uint64_t(1) << 32) % mod;
            constexpr uint32_t quotient = (uint64_t(scale) << 32) / mod;
            auto packed = u64x4(x), q = u64x4() + quotient;
            auto lo = mul(packed, q) >> 32, hi = mul(packed >> 32, q);
            auto approx = u32x8(lo | (hi & (~uint64_t(0) << 32)));
            auto out = x * scale - approx * mod;
            return out < out - mod ? out : out - mod;
        }
        // Copy between matrix rows and contiguous recursive quadrants.
        template<bool unpack = false, int add = 0, class matrix>
        static void copy(matrix& a, uint32_t* ptr, size_t n, size_t m, size_t depth, size_t row = 0, size_t col = 0) {
            if(depth) {
                n /= 2;
                m /= 2;
                for(size_t q = 0; q < 4; q++) {
                    copy<unpack, add>(a, ptr + q * n * m, n, m, depth - 1, row + q / 2 * n, col + q % 2 * m);
                }
                return;
            }
            if(col >= a.m()) {
                return;
            }
            size_t width = std::min(m, a.m() - col);
            for(size_t i = row; i < std::min(row + n, a.n()); i++) {
                auto data = a[i].data() + col;
                auto packed = ptr + (i - row) * m;
                for(size_t j = 0; j < width; j++) {
                    if constexpr(unpack && add != 0) {
                        typename matrix::base value;
                        value.setr(packed[j]);
                        if constexpr(add == 1) {
                            data[j] += value;
                        } else {
                            data[j] -= value;
                        }
                    } else if constexpr(unpack) {
                        data[j].setr(packed[j]);
                    } else {
                        packed[j] = uint32_t(data[j].getr());
                    }
                }
            }
        }
        template<int add = 0, class matrix>
        static matrix product(matrix const& a, matrix const& b, matrix res = {}) {
            static_assert(add >= -1 && add <= 1);
            auto pad = [](size_t x) {
                return (x + 31) / 32 * 32;
            };
            size_t n = pad(a.n()), m = pad(a.m()), k = pad(b.m());
            // Each recursion level needs a quarter as much scratch; children reuse it.
            size_t entries = n * m + m * k + n * k;
            // Small products avoid repeated mmap/madvise setup for temporary storage.
            std::vector<uint32_t> small;
            big_vector<uint32_t> large;
            size_t count = entries + entries / 3;
            auto ap = count < (1 << 21) ? (small.resize(count), small.data()) : (large.resize(count), large.data());
            auto bp = ap + n * m, cp = bp + m * k;
            auto scratch = cp + n * k;
            // A, B, and C must use the same depth, including rectangular products.
            size_t depth = 0;
            for(size_t x = n, y = m, z = k; can_split(x, y, z); x /= 2, y /= 2, z /= 2) {
                depth++;
            }
            copy(a, ap, n, m, depth);
            copy(b, bp, m, k, depth);
            // Only A is scaled; each leaf's Montgomery reduction removes the factor.
            for(size_t i = 0; i < n * m; i += 8) {
                u32x8 x;
                std::memcpy(&x, ap + i, sizeof x);
                x = encode(x);
                std::memcpy(ap + i, &x, sizeof x);
            }
            multiply(ap, bp, cp, n, m, k, scratch);
            if constexpr(add == 0) {
                res = matrix(a.n(), b.m());
            }
            assert(res.n() == a.n() && res.m() == b.m());
            copy<true, add>(res, cp, n, k, depth);
            return res;
        }
    };
}
#pragma GCC pop_options

#ifndef CP_ALGO_LINALG_STRASSEN_HPP
#define CP_ALGO_LINALG_STRASSEN_HPP
#include "vector.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::linalg::impl{ template<class row> constexpr bool use_strassen=false; template<auto mod> constexpr bool use_strassen<modint_vec<math::modint<mod>>> =mod>1&&mod%2&&mod<(1LL<<30); template<uint32_t mod> struct strassen_product{ struct view{ uint32_t*data; size_t stride; uint32_t*operator[](size_t i)const{return data+i*stride;} }; static u64x4 mul(u64x4 a,u64x4 b){
#ifdef __AVX2__
return u64x4(_mm256_mul_epu32(__m256i(a),__m256i(b)));
#else
return low32(a)*low32(b);
#endif
} static u64x4 shrink(u64x4 x){ auto words=u32x8(x); auto bound=u32x8(u64x4()+(uint64_t(2)*mod<<32));
#ifdef __AVX2__
return u64x4(_mm256_min_epu32(__m256i(words),__m256i(words-bound)));
#else
return u64x4(words<words-bound?words:words-bound);
#endif
} template<size_t dim=0> [[gnu::noinline]]static void leaf(view a,view b,view c,size_t n,size_t m,size_t k){ if constexpr(dim){ n=m=k=dim; a.stride=b.stride=c.stride=dim; } for(size_t i=0;i<n;i+=4){ for(size_t j=0;j<k;j+=8){ u64x4 acc[4][2]{}; for(size_t first=0;first<m;first+=8){
#pragma GCC unroll 1
for(size_t z=first;z<first+8;z++){ u64x4 x; std::memcpy(&x,b[z] +j,sizeof x); u64x4 scales[4]; for(size_t t=0;t<4;t++){ scales[t] =u64x4(u32x8()+a[i+t][z]); } for(size_t t=0;t<4;t++){ acc[t][0] +=mul(scales[t],x); } x>>=32; for(size_t t=0;t<4;t++){ acc[t][1] +=mul(scales[t],x); } } for(auto&row:acc){ for(auto&x:row){ x=shrink(x); } } } for(size_t t=0;t<4;t++){ constexpr uint32_t inv=math::inv2(uint32_t(-mod)); for(auto&x:acc[t]){ x=montgomery_reduce(x,mod,inv); } u32x8 out=u32x8(acc[t][0] |(acc[t][1] <<32)); out=out<out-mod?out:out-mod; out=out<out-mod?out:out-mod; std::memcpy(c[i+t] +j,&out,sizeof out); } } } } template<bool subtract=false> static void combine(uint32_t*a,uint32_t*b,uint32_t*c,size_t n,size_t m){ for(size_t j=0;j<n*m;j+=8){ u32x8 x,y; std::memcpy(&x,a+j,sizeof x); std::memcpy(&y,b+j,sizeof y); u32x8 z=subtract?x+mod-y:x+y; z=z<z-mod?z:z-mod; std::memcpy(c+j,&z,sizeof z); } } static bool can_split(size_t n,size_t m,size_t k){ return std::min({n,m,k})>64&&n%16==0&&m%16==0&&k%16==0; } [[gnu::noinline]]static void multiply(uint32_t*a,uint32_t*b,uint32_t*c,size_t n,size_t m,size_t k, uint32_t*work){ if(!can_split(n,m,k)){ if(n==64&&m==64&&k==64){ leaf<64>({a,m},{b,k},{c,k},n,m,k); }else{ leaf({a,m},{b,k},{c,k},n,m,k); } return; } n/=2; m/=2; k/=2; auto s=work,t=s+n*m,p=t+m*k; work+=n*m+m*k+n*k; auto a00=a,a01=a+n*m,a10=a+2*n*m,a11=a+3*n*m; auto b00=b,b01=b+m*k,b10=b+2*m*k,b11=b+3*m*k; auto c00=c,c01=c+n*k,c10=c+2*n*k,c11=c+3*n*k; multiply(a00,b00,c11,n,m,k,work); multiply(a01,b10,c00,n,m,k,work); combine(c00,c11,c00,n,k); combine(a10,a11,s,n,m); combine<true>(b01,b00,t,m,k); multiply(s,t,c01,n,m,k,work); combine<true>(s,a00,s,n,m); combine<true>(b11,t,t,m,k); multiply(s,t,c10,n,m,k,work); combine(c11,c10,c10,n,k); combine<true>(a01,s,s,n,m); multiply(s,b11,p,n,m,k,work); combine(c10,c01,c11,n,k); combine(c11,p,c01,n,k); combine<true>(t,b10,t,m,k); multiply(a11,t,p,n,m,k,work); combine<true>(c10,p,c10,n,k); combine<true>(a00,a10,s,n,m); combine<true>(b11,b01,t,m,k); multiply(s,t,p,n,m,k,work); combine(c10,p,c10,n,k); combine(c11,p,c11,n,k); } static u32x8 encode(u32x8 x){ constexpr uint32_t scale=(uint64_t(1)<<32)%mod; constexpr uint32_t quotient=(uint64_t(scale)<<32)/mod; auto packed=u64x4(x),q=u64x4()+quotient; auto lo=mul(packed,q)>>32,hi=mul(packed>>32,q); auto approx=u32x8(lo|(hi&(~uint64_t(0)<<32))); auto out=x*scale-approx*mod; return out<out-mod?out:out-mod; } template<bool unpack=false,int add=0,class matrix> static void copy(matrix&a,uint32_t*ptr,size_t n,size_t m,size_t depth,size_t row=0,size_t col=0){ if(depth){ n/=2; m/=2; for(size_t q=0;q<4;q++){ copy<unpack,add>(a,ptr+q*n*m,n,m,depth-1,row+q/2*n,col+q%2*m); } return; } if(col>=a.m()){ return; } size_t width=std::min(m,a.m()-col); for(size_t i=row;i<std::min(row+n,a.n());i++){ auto data=a[i].data()+col; auto packed=ptr+(i-row)*m; for(size_t j=0;j<width;j++){ if constexpr(unpack&&add!=0){ typename matrix::base value; value.setr(packed[j]); if constexpr(add==1){ data[j] +=value; }else{ data[j] -=value; } }else if constexpr(unpack){ data[j].setr(packed[j]); }else{ packed[j] =uint32_t(data[j].getr()); } } } } template<int add=0,class matrix> static matrix product(matrix const&a,matrix const&b,matrix res={}){ static_assert(add>= -1&&add<=1); auto pad= [](size_t x){ return(x+31)/32*32; }; size_t n=pad(a.n()),m=pad(a.m()),k=pad(b.m()); size_t entries=n*m+m*k+n*k; std::vector<uint32_t>small; big_vector<uint32_t>large; size_t count=entries+entries/3; auto ap=count<(1<<21)?(small.resize(count),small.data()):(large.resize(count),large.data()); auto bp=ap+n*m,cp=bp+m*k; auto scratch=cp+n*k; size_t depth=0; for(size_t x=n,y=m,z=k;can_split(x,y,z);x/=2,y/=2,z/=2){ depth++; } copy(a,ap,n,m,depth); copy(b,bp,m,k,depth); for(size_t i=0;i<n*m;i+=8){ u32x8 x; std::memcpy(&x,ap+i,sizeof x); x=encode(x); std::memcpy(ap+i,&x,sizeof x); } multiply(ap,bp,cp,n,m,k,scratch); if constexpr(add==0){ res=matrix(a.n(),b.m()); } assert(res.n()==a.n()&&res.m()==b.m()); copy<true,add>(res,cp,n,k,depth); return res; } }; }
#pragma GCC pop_options
#endif
#line 1 "cp-algo/linalg/strassen.hpp"
#line 1 "cp-algo/linalg/vector.hpp"
#line 1 "cp-algo/random/rng.hpp"
#include <chrono>
#include <random>
namespace cp_algo::random{ std::mt19937_64 gen( std::chrono::steady_clock::now().time_since_epoch().count() ); uint64_t rng(){ return gen(); } }
#line 1 "cp-algo/number_theory/modint.hpp"
#line 1 "cp-algo/math/common.hpp"
#include <functional>
#include <cstdint>
#include <cassert>
#include <bit>
#include <vector>
#include <algorithm>
namespace cp_algo::math{
#ifdef CP_ALGO_MAXN
const int maxn=CP_ALGO_MAXN;
#else
const int maxn=1<<19;
#endif
const int magic=64; template<int window=1> auto bpow(auto const&x,auto n,auto const&one,auto op){ static_assert(window>=1&&window<=6); if constexpr(window>1){ if(n==0){return one;} int bits=std::bit_width(uint64_t(n)); auto low_bit= [&](int high){ int low=std::max(0,high-window+1); while(!((n>>low)&1)){low++;} return low; }; int first=low_bit(bits-1); int cost=(1<<(window-1))+first; for(int j=first-1;j>=0;){ if(!((n>>j)&1)){j--;} else{cost++;j=low_bit(j)-1;} } if(cost>=bits+std::popcount(uint64_t(n))-2){return bpow<1>(x,n,one,op);} using T=std::decay_t<decltype(x)>; std::vector<T>odd; odd.reserve(1<<(window-1)); odd.push_back(x); auto square=op(x,x); while(odd.size()<size_t(1<<(window-1))){odd.push_back(op(odd.back(),square));} auto ans=odd[(n>>first)/2]; for(int j=first-1;j>=0;){ if(!((n>>j)&1)){ans=op(ans,ans);j--;} else{ int low=low_bit(j),length=j-low+1; auto digit=(n>>low)&((1u<<length)-1); for(int i=0;i<length;i++){ans=op(ans,ans);} ans=op(ans,odd[digit/2]); j=low-1; } } return ans; }else{ if(n==0){ return one; } auto ans=x; for(int j=std::bit_width<uint64_t>(n)-2;~j;j--){ ans=op(ans,ans); if((n>>j)&1){ ans=op(ans,x); } } return ans; } } template<int window=1> auto bpow(auto x,auto n,auto ans){ return bpow<window>(x,n,ans,std::multiplies{}); } template<typename T> T bpow(T const&x,auto n){ return bpow(x,n,T(1)); } inline constexpr auto inv2(auto x){ assert(x%2); std::make_unsigned_t<decltype(x)>y=1; while(y*x!=1){ y*=2-x*y; } return y; } }
#line 4 "cp-algo/number_theory/modint.hpp"
#include <iostream>
#line 6 "cp-algo/number_theory/modint.hpp"
namespace cp_algo::math{ template<typename modint,typename _Int> struct modint_base{ using Int=_Int; using UInt=std::make_unsigned_t<Int>; static constexpr size_t bits=sizeof(Int)*8; using Int2=std::conditional_t<bits<=32,int64_t,__int128_t>; using UInt2=std::conditional_t<bits<=32,uint64_t,__uint128_t>; constexpr static Int mod(){ return modint::mod(); } constexpr static UInt remod(){ return modint::remod(); } constexpr static UInt2 modmod(){ return UInt2(mod())*mod(); } constexpr modint_base()=default; constexpr modint_base(Int2 rr){ to_modint().setr(UInt((rr+modmod())%mod())); } constexpr modint inv()const{ return bpow(to_modint(),mod()-2); } modint operator-()const{ modint neg; neg.r=std::min(-r,remod()-r); return neg; } modint&operator/=(const modint&t){ return to_modint()*=t.inv(); } modint&operator*=(const modint&t){ r=UInt(UInt2(r)*t.r%mod()); return to_modint(); } modint&operator+=(const modint&t){ r+=t.r; r=std::min(r,r-remod()); return to_modint(); } modint&operator-=(const modint&t){ r-=t.r; r=std::min(r,r+remod()); return to_modint(); } modint operator+(const modint&t)const{return modint(to_modint())+=t;} modint operator-(const modint&t)const{return modint(to_modint())-=t;} modint operator*(const modint&t)const{return modint(to_modint())*=t;} modint operator/(const modint&t)const{return modint(to_modint())/=t;} auto operator==(const modint&t)const{return to_modint().getr()==t.getr();} auto operator!=(const modint&t)const{return to_modint().getr()!=t.getr();} auto operator<=(const modint&t)const{return to_modint().getr()<=t.getr();} auto operator>=(const modint&t)const{return to_modint().getr()>=t.getr();} auto operator<(const modint&t)const{return to_modint().getr()<t.getr();} auto operator>(const modint&t)const{return to_modint().getr()>t.getr();} Int rem()const{ UInt R=to_modint().getr(); return R-(R>(UInt)mod()/2)*mod(); } constexpr void setr(UInt rr){ r=rr; } constexpr UInt getr()const{ return r; } static uint64_t modmod8(){return uint64_t(8*modmod());} void add_unsafe(UInt t){r+=t;} void pseudonormalize(){r=std::min(r,r-modmod8());} modint const&normalize(){ if(r>=(UInt)mod()){ r%=mod(); } return to_modint(); } void setr_direct(UInt rr){r=rr;} UInt getr_direct()const{return r;} protected: UInt r; private: constexpr modint&to_modint(){return static_cast<modint&>(*this);} constexpr modint const&to_modint()const{return static_cast<modint const&>(*this);} }; template<typename modint> concept modint_type=std::is_base_of_v<modint_base<modint,typename modint::Int>,modint>; template<modint_type modint> decltype(std::cin)&operator>>(decltype(std::cin)&in,modint&x){ typename modint::UInt r; auto&res=in>>r; x.setr(r); return res; } template<modint_type modint> decltype(std::cout)&operator<<(decltype(std::cout)&out,modint const&x){ return out<<x.getr(); } template<auto m> struct modint:modint_base<modint<m>,decltype(m)>{ using Base=modint_base<modint<m>,decltype(m)>; using Base::Base; static constexpr Base::Int mod(){return m;} static constexpr Base::UInt remod(){return m;} auto getr()const{return Base::r;} }; template<typename Int=int> struct dynamic_modint:modint_base<dynamic_modint<Int>,Int>{ using Base=modint_base<dynamic_modint<Int>,Int>; using Base::Base; [[gnu::noinline,gnu::cold]]static Base::UInt m_reduce_reduced(Base::UInt2 ab){ if(mod()%2==0){ return typename Base::UInt(ab%mod()); } typename Base::UInt q= -(typename Base::UInt(ab)*inverse); auto high=typename Base::UInt(ab>>Base::bits); auto low=typename Base::UInt(typename Base::UInt2(q)*typename Base::UInt(mod())>>Base::bits); return high>=low?high-low:high-low+mod(); } static Base::UInt m_reduce(Base::UInt2 ab){ if(imod()==0)[[unlikely]]{ return m_reduce_reduced(ab); } typename Base::UInt2 m=typename Base::UInt(ab)*imod(); return typename Base::UInt((ab+m*mod())>>Base::bits); } static Base::UInt m_transform(Base::UInt a){ if(mod()%2==0)[[unlikely]]{ return a; }else{ return m_reduce(a*pw128()); } } dynamic_modint&operator*=(const dynamic_modint&t){ Base::r=m_reduce(typename Base::UInt2(Base::r)*t.r); return*this; } void setr(Base::UInt rr){ Base::r=m_transform(rr); } Base::UInt getr()const{ typename Base::UInt res=m_reduce(Base::r); return std::min(res,res-mod()); } static Int mod(){return m;} static Base::UInt remod(){return rm;} static Base::UInt imod(){return im;} static Base::UInt2 pw128(){return r2;} static void switch_mod(Int nm){ m=nm; bool lazy=m%2&&typename Base::UInt(m)<=typename Base::UInt(-1)/4; rm=typename Base::UInt(m)*(lazy?2:1); inverse=m%2?inv2(-m):0; im=lazy?inverse:0; r2=static_cast<Base::UInt>(static_cast<Base::UInt2>(-1)%m+1); } auto static with_mod(Int tmp,auto callback){ struct scoped{ Int prev=mod(); ~scoped(){switch_mod(prev);} }_; switch_mod(tmp); return callback(); } private: static inline thread_local Int m=1; static inline thread_local Base::UInt im= -1,r2=0,inverse= -1,rm=2; }; }
#line 1 "cp-algo/util/big_alloc.hpp"
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#line 10 "cp-algo/util/big_alloc.hpp"
#include <string>
#include <cstddef>
#line 14 "cp-algo/util/big_alloc.hpp"
#include <forward_list>
#if defined(__linux__) || defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#define CP_ALGO_USE_MMAP 1
#include <sys/mman.h>
#else
#define CP_ALGO_USE_MMAP 0
#endif
namespace cp_algo{ template<typename T,size_t Align=32> class big_alloc{ static_assert(Align>=alignof(void*),"Align must be at least pointer-size"); static_assert(std::has_single_bit(Align),"Align must be a power of two"); public: using value_type=T; template<class U> struct rebind{ using other=big_alloc<U,Align>; }; constexpr bool operator==(const big_alloc&)const=default; big_alloc()noexcept=default; template<typename U,std::size_t A> big_alloc(const big_alloc<U,A>&)noexcept{} [[nodiscard]]T*allocate(std::size_t n){ std::size_t padded=round_up(n*sizeof(T)); std::size_t align=std::max<std::size_t>(alignof(T),Align);
#if CP_ALGO_USE_MMAP
if(padded>=MEGABYTE){ void*raw=mmap(nullptr,padded,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0); madvise(raw,padded,MADV_HUGEPAGE); return static_cast<T*>(raw); }
#endif
return static_cast<T*>(::operator new(padded,std::align_val_t(align))); } void deallocate(T*p,std::size_t n)noexcept{ if(!p){ return; } std::size_t padded=round_up(n*sizeof(T)); std::size_t align=std::max<std::size_t>(alignof(T),Align);
#if CP_ALGO_USE_MMAP
if(padded>=MEGABYTE){ munmap(p,padded); return; }
#endif
::operator delete(p,padded,std::align_val_t(align)); } private: static constexpr std::size_t MEGABYTE=1<<20; static constexpr std::size_t round_up(std::size_t x)noexcept{ return(x+Align-1)/Align*Align; } }; template<typename T> using big_vector=std::vector<T,big_alloc<T>>; template<typename T> using big_basic_string=std::basic_string<T,std::char_traits<T>,big_alloc<T>>; template<typename T> using big_deque=std::deque<T,big_alloc<T>>; template<typename T> using big_stack=std::stack<T,big_deque<T>>; template<typename T> using big_queue=std::queue<T,big_deque<T>>; template<typename T> using big_priority_queue=std::priority_queue<T,big_vector<T>>; template<typename T> using big_forward_list=std::forward_list<T,big_alloc<T>>; using big_string=big_basic_string<char>; template<typename Key,typename Value,typename Compare=std::less<Key>> using big_map=std::map<Key,Value,Compare,big_alloc<std::pair<const Key,Value>>>; template<typename T,typename Compare=std::less<T>> using big_multiset=std::multiset<T,Compare,big_alloc<T>>; template<typename T,typename Compare=std::less<T>> using big_set=std::set<T,Compare,big_alloc<T>>; }
#line 1 "cp-algo/util/simd.hpp"
#include <experimental/simd>
#line 6 "cp-algo/util/simd.hpp"
#include <memory>
#if defined(__x86_64__) && !defined(CP_ALGO_DISABLE_AVX2)
#define CP_ALGO_SIMD_AVX2_TARGET _Pragma("GCC target(\"avx2,fma\")")
#else
#define CP_ALGO_SIMD_AVX2_TARGET
#endif
#define CP_ALGO_SIMD_PRAGMA_PUSH  _Pragma("GCC push_options")  CP_ALGO_SIMD_AVX2_TARGET
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo{ template<typename T,size_t len> using simd[[gnu::vector_size(len*sizeof(T))]] =T; using u64x8=simd<uint64_t,8>; using u32x16=simd<uint32_t,16>; using i64x4=simd<int64_t,4>; using u64x4=simd<uint64_t,4>; using u32x8=simd<uint32_t,8>; using u16x16=simd<uint16_t,16>; using i32x4=simd<int32_t,4>; using u32x4=simd<uint32_t,4>; using u16x8=simd<uint16_t,8>; using u16x4=simd<uint16_t,4>; using i16x4=simd<int16_t,4>; using u8x32=simd<uint8_t,32>; using u8x16=simd<uint8_t,16>; using u8x8=simd<uint8_t,8>; using u8x4=simd<uint8_t,4>; using dx4=simd<double,4>; inline dx4 abs(dx4 a){ return dx4{ std::abs(a[0]), std::abs(a[1]), std::abs(a[2]), std::abs(a[3]) }; } static constexpr dx4 magic=dx4()+(3ULL<<51); inline i64x4 lround(dx4 x){ return i64x4(x+magic)-i64x4(magic); } inline dx4 to_double(i64x4 x){ return dx4(x+i64x4(magic))-magic; } inline dx4 round(dx4 a){ return dx4{ std::nearbyint(a[0]), std::nearbyint(a[1]), std::nearbyint(a[2]), std::nearbyint(a[3]) }; } inline u64x4 low32(u64x4 x){ return x&uint32_t(-1); } inline auto swap_bytes(auto x){ return decltype(x)(__builtin_shufflevector(u32x8(x),u32x8(x),1,0,3,2,5,4,7,6)); } inline u64x4 montgomery_reduce(u64x4 x,uint32_t mod,uint32_t imod){
#ifdef __AVX2__
auto x_ninv=u64x4(_mm256_mul_epu32(__m256i(x),__m256i()+imod)); x+=u64x4(_mm256_mul_epu32(__m256i(x_ninv),__m256i()+mod));
#else
auto x_ninv=u64x4(u32x8(low32(x))*imod); x+=x_ninv*uint64_t(mod);
#endif
return swap_bytes(x); } inline u32x8 reduce_once(u32x8 x,uint32_t mod){ auto y=x-mod; return x<y?x:y; } inline u64x4 reduce_once(u64x4 x,uint32_t mod){ return u64x4(reduce_once(u32x8(x),mod)); } inline u64x4 montgomery_mul(u64x4 x,u64x4 y,uint32_t mod,uint32_t imod){
#ifdef __AVX2__
return montgomery_reduce(u64x4(_mm256_mul_epu32(__m256i(x),__m256i(y))),mod,imod);
#else
return montgomery_reduce(x*y,mod,imod);
#endif
} inline u32x8 montgomery_mul(u32x8 x,u32x8 y,uint32_t mod,uint32_t imod){ return u32x8(montgomery_mul(u64x4(x),u64x4(y),mod,imod))| u32x8(swap_bytes(montgomery_mul(u64x4(swap_bytes(x)),u64x4(swap_bytes(y)),mod,imod))); } inline dx4 rotate_right(dx4 x){ static constexpr u64x4 shuffler={3,0,1,2}; return __builtin_shuffle(x,shuffler); } template<std::size_t Align=32> inline bool is_aligned(const auto*p)noexcept{ return(reinterpret_cast<std::uintptr_t>(p)%Align)==0; } template<class Target> inline Target&vector_cast(auto&&p){ return*reinterpret_cast<Target*>(std::assume_aligned<alignof(Target)>(&p)); } }
#pragma GCC pop_options
#line 1 "cp-algo/util/checkpoint.hpp"
#line 8 "cp-algo/util/checkpoint.hpp"
namespace cp_algo{
#ifdef CP_ALGO_CHECKPOINT
big_map<big_string,double>checkpoints; double last;
#endif
template<bool final=false> void checkpoint([[maybe_unused]]auto const&_msg){
#ifdef CP_ALGO_CHECKPOINT
big_string msg=_msg; double now=(double)clock()/CLOCKS_PER_SEC; double delta=now-last; last=now; if(msg.size()&& !final){ checkpoints[msg] +=delta; } if(final){ for(auto const& [key,value] :checkpoints){ std::cerr<<key<<": "<<value*1000<<" ms\n"; } std::cerr<<"Total: "<<now*1000<<" ms\n"; }
#endif
} template<bool final=false> void checkpoint(){ checkpoint<final>(""); } }
#line 10 "cp-algo/linalg/vector.hpp"
#include <valarray>
#line 12 "cp-algo/linalg/vector.hpp"
#include <iterator>
#line 14 "cp-algo/linalg/vector.hpp"
#include <ranges>
#include <array>
#include <cstring>
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::linalg{ template<typename base,class Alloc=big_alloc<base>> struct vec:std::basic_string<base,std::char_traits<base>,Alloc>{ using Base=std::basic_string<base,std::char_traits<base>,Alloc>; using Base::Base; vec(Base const&t):Base(t){} vec(Base&&t):Base(std::move(t)){} vec(size_t n):Base(n,base()){} vec(auto&&r):Base(r.begin(),r.end()){} static vec ei(size_t n,size_t i){ vec res(n); res[i] =1; return res; } auto operator-()const{ return*this|std::views::transform([](auto x){return-x;}); } auto operator*(base t)const{ return*this|std::views::transform([t](auto x){return x*t;}); } vec&operator*=(base t){ for(auto&it: *this){ it*=t; } return*this; } virtual void add_scaled(vec const&b,base scale,size_t i=0){ if(scale!=base(0)){ for(;i<size(*this);i++){ (*this)[i] +=scale*b[i]; } } } virtual vec const&normalize(){ return static_cast<vec&>(*this); } virtual base normalize(size_t i){ return(*this)[i]; } void read(){ for(auto&it: *this){ std::cin>>it; } } void print()const{ for(auto&it: *this){ std::cout<<it<<' '; } std::cout<<'\n'; } static vec random(size_t n){ vec res(n); std::ranges::generate(res,random::rng); return res; } vec operator|(vec const&t)const{ return std::views::join(std::array{std::views::all(*this),std::views::all(t)}); } std::pair<size_t,base>find_pivot(){ if(pivot==size_t(-1)){ pivot=0; while(pivot<size(*this)&&normalize(pivot)==base(0)){ pivot++; } if(pivot<size(*this)){ pivot_inv=base(1)/(*this)[pivot]; } } return{pivot,pivot_inv}; } void reduce_by(vec&t){ auto[pivot,pinv] =t.find_pivot(); if(pivot<size(*this)){ add_scaled(t,-normalize(pivot)*pinv,pivot); } } private: size_t pivot= -1; base pivot_inv; }; template<math::modint_type base,class Alloc=big_alloc<base>> struct modint_vec:vec<base,Alloc>{ using Base=vec<base,Alloc>; using Base::Base; modint_vec(Base const&t):Base(t){} modint_vec(Base&&t):Base(std::move(t)){} void add_scaled(Base const&b,base scale,size_t i=0)override{ static_assert(base::bits>=64,"Only wide modint types for linalg"); if(scale!=base(0)){ assert(Base::size()==b.size()); size_t n=size(*this); u64x4 scaler=u64x4()+scale.getr(); bool aligned=is_aligned(&(*this)[0])&&is_aligned(&b[0]); if(aligned){ i-=i%4; } bool reduce= ++counter==accumulation_period(); if(reduce){ counter=0; for(size_t j=0;j<i;j++){ (*this)[j].pseudonormalize(); } } if(aligned){ for(;i+3<n;i+=4){ auto&ai=vector_cast<u64x4>((*this)[i]); auto bi=vector_cast<u64x4 const>(b[i]);
#ifdef __AVX2__
ai+=u64x4(_mm256_mul_epu32(__m256i(scaler),__m256i(bi)));
#else
ai+=scaler*bi;
#endif
if(reduce){ ai=shrink(ai); } } } for(;i<n;i++){ (*this)[i].add_unsafe(b[i].getr_direct()*scale.getr()); if(reduce){ (*this)[i].pseudonormalize(); } } } } Base const&normalize()override{ for(auto&it: *this){ it.normalize(); } return*this; } base normalize(size_t i)override{ return(*this)[i].normalize(); } private: template<typename,typename> friend struct matrix; static size_t accumulation_period(){ return base::remod()==typename base::UInt(base::mod())&&base::mod()<(1LL<<30)?8:4; } static u64x4 mul(u64x4 a,u64x4 b){
#ifdef __AVX2__
return u64x4(_mm256_mul_epu32(__m256i(a),__m256i(b)));
#else
return a*b;
#endif
} static u64x4 shrink(u64x4 a){ auto b=a-(u64x4()+base::modmod8()); return a<b?a:b; } static void add_scaled_pair(modint_vec&x,modint_vec&y,Base const&p,Base const&q,std::array<base,4>c, size_t first=0){ if(std::ranges::find(c,base(0))!=c.end()){ x.add_scaled(p,c[0],first); x.add_scaled(q,c[1],first); y.add_scaled(p,c[2],first); y.add_scaled(q,c[3],first); return; } size_t n=x.size(); assert(y.size()==n&&p.size()==n&&q.size()==n&&first<=n); if(is_aligned(x.data())&&is_aligned(y.data())&&is_aligned(p.data())&&is_aligned(q.data())){ first-=first%4; } size_t period=accumulation_period(); auto prepare= [&](modint_vec&a){ if(a.counter+2>period){ for(auto&v:a){ v.pseudonormalize(); } a.counter=0; } a.counter+=2; if(a.counter!=period){ return false; } a.counter=0; for(size_t i=0;i<first;i++){ a[i].pseudonormalize(); } return true; }; bool nx=prepare(x),ny=prepare(y); auto*__restrict__ dx=x.data(); auto*__restrict__ dy=y.data(); auto const*__restrict__ sp=p.data(); auto const*__restrict__ sq=q.data(); uint64_t xp=c[0].getr(),xq=c[1].getr(),yp=c[2].getr(),yq=c[3].getr(); u64x4 xp4=u64x4()+xp,xq4=u64x4()+xq; u64x4 yp4=u64x4()+yp,yq4=u64x4()+yq; size_t i=first; for(;i+4<=n;i+=4){ u64x4 vx,vy,vp,vq; std::memcpy(&vx,dx+i,sizeof vx); std::memcpy(&vy,dy+i,sizeof vy); std::memcpy(&vp,sp+i,sizeof vp); std::memcpy(&vq,sq+i,sizeof vq); vx+=mul(xp4,vp)+mul(xq4,vq); vy+=mul(yp4,vp)+mul(yq4,vq); if(nx){ vx=shrink(vx); } if(ny){ vy=shrink(vy); } std::memcpy(dx+i,&vx,sizeof vx); std::memcpy(dy+i,&vy,sizeof vy); } for(;i<n;i++){ dx[i].add_unsafe(xp*sp[i].getr_direct()+xq*sq[i].getr_direct()); dy[i].add_unsafe(yp*sp[i].getr_direct()+yq*sq[i].getr_direct()); if(nx){ dx[i].pseudonormalize(); } if(ny){ dy[i].pseudonormalize(); } } } size_t counter=0; }; template<typename base> requires(base::bits<=32) struct modint_vec<base>:vec<base>{ using Base=vec<base>; using Base::Base; modint_vec(Base const&t):Base(t){} modint_vec(Base&&t):Base(std::move(t)){} void add_scaled(Base const&b,base scale,size_t first=0)override{ if(scale==base(0)){ return; } if(&b==this){ Base::add_scaled(b,scale,first); }else{ add_scaled_batch<1,1>({this},{&b},{scale},first); } } private: template<typename,typename> friend struct matrix; static constexpr size_t batch_size=8; static constexpr bool use_simd= []{ if constexpr(requires{ std::integral_constant<uint32_t,base::mod()>{}; std::integral_constant<uint32_t,base::remod()>{}; }){ return sizeof(base)==sizeof(uint32_t)&&base::mod()>1&&base::mod()%2&& base::mod()<(1U<<30)&&base::remod()==typename base::UInt(base::mod()); }else{ return false; } }(); static u64x4 mul(u64x4 a,u64x4 b){
#ifdef __AVX2__
return u64x4(_mm256_mul_epu32(__m256i(a),__m256i(b)));
#else
return low32(a)*low32(b);
#endif
} template<size_t count,size_t rows> static void add_scaled_batch(std::array<modint_vec*,rows>const&dst, std::array<Base const*,count>const&src,std::array<base,rows*count>const&c, size_t first=0){ static_assert(count<=batch_size&&(rows==1||rows==2)); if(std::ranges::all_of(c,[](base value){return value==base(0);})){ return; } if constexpr(use_simd){ constexpr uint32_t mod=base::mod(),inv=math::inv2(uint32_t(-mod)); std::array<uint32_t,rows*count>scale; for(size_t t=0;t<rows*count;t++){ scale[t] =uint32_t((uint64_t(c[t].getr())<<32)%mod); } auto*__restrict__ dx=dst[0]->data(); auto*__restrict__ dy=rows==2?dst[1]->data():nullptr; size_t n=dst[0]->size(); for(;first+8<=n;first+=8){ u64x4 acc[rows][2]{};
#pragma GCC unroll 1
for(size_t t=0;t<count;t++){ u64x4 p; std::memcpy(&p,src[t]->data()+first,sizeof p); auto q=p>>32; for(size_t row=0;row<rows;row++){ auto v=u64x4(u32x8()+scale[row*count+t]); acc[row][0] +=mul(p,v); acc[row][1] +=mul(q,v); } } for(size_t row=0;row<rows;row++){ for(auto&v:acc[row]){ v=montgomery_reduce(v,mod,inv); } auto*out=(row?dy:dx)+first; u32x8 old; std::memcpy(&old,out,sizeof old); auto z=old+u32x8(acc[row][0] |(acc[row][1] <<32)); z=z<z-2*mod?z:z-2*mod; z=z<z-mod?z:z-mod; std::memcpy(out,&z,sizeof z); } } } for(size_t t=0;t<count;t++){ for(size_t row=0;row<rows;row++){ dst[row]->Base::add_scaled(*src[t],c[row*count+t],first); } } } static void add_scaled_pair(modint_vec&x,modint_vec&y,Base const&p,Base const&q,std::array<base,4>c, size_t first=0){ add_scaled_batch<2,2>({&x,&y},{&p,&q},c,first); } }; }
#pragma GCC pop_options
#line 4 "cp-algo/linalg/strassen.hpp"
CP_ALGO_SIMD_PRAGMA_PUSH
namespace cp_algo::linalg::impl{ template<class row> constexpr bool use_strassen=false; template<auto mod> constexpr bool use_strassen<modint_vec<math::modint<mod>>> =mod>1&&mod%2&&mod<(1LL<<30); template<uint32_t mod> struct strassen_product{ struct view{ uint32_t*data; size_t stride; uint32_t*operator[](size_t i)const{return data+i*stride;} }; static u64x4 mul(u64x4 a,u64x4 b){
#ifdef __AVX2__
return u64x4(_mm256_mul_epu32(__m256i(a),__m256i(b)));
#else
return low32(a)*low32(b);
#endif
} static u64x4 shrink(u64x4 x){ auto words=u32x8(x); auto bound=u32x8(u64x4()+(uint64_t(2)*mod<<32));
#ifdef __AVX2__
return u64x4(_mm256_min_epu32(__m256i(words),__m256i(words-bound)));
#else
return u64x4(words<words-bound?words:words-bound);
#endif
} template<size_t dim=0> [[gnu::noinline]]static void leaf(view a,view b,view c,size_t n,size_t m,size_t k){ if constexpr(dim){ n=m=k=dim; a.stride=b.stride=c.stride=dim; } for(size_t i=0;i<n;i+=4){ for(size_t j=0;j<k;j+=8){ u64x4 acc[4][2]{}; for(size_t first=0;first<m;first+=8){
#pragma GCC unroll 1
for(size_t z=first;z<first+8;z++){ u64x4 x; std::memcpy(&x,b[z] +j,sizeof x); u64x4 scales[4]; for(size_t t=0;t<4;t++){ scales[t] =u64x4(u32x8()+a[i+t][z]); } for(size_t t=0;t<4;t++){ acc[t][0] +=mul(scales[t],x); } x>>=32; for(size_t t=0;t<4;t++){ acc[t][1] +=mul(scales[t],x); } } for(auto&row:acc){ for(auto&x:row){ x=shrink(x); } } } for(size_t t=0;t<4;t++){ constexpr uint32_t inv=math::inv2(uint32_t(-mod)); for(auto&x:acc[t]){ x=montgomery_reduce(x,mod,inv); } u32x8 out=u32x8(acc[t][0] |(acc[t][1] <<32)); out=out<out-mod?out:out-mod; out=out<out-mod?out:out-mod; std::memcpy(c[i+t] +j,&out,sizeof out); } } } } template<bool subtract=false> static void combine(uint32_t*a,uint32_t*b,uint32_t*c,size_t n,size_t m){ for(size_t j=0;j<n*m;j+=8){ u32x8 x,y; std::memcpy(&x,a+j,sizeof x); std::memcpy(&y,b+j,sizeof y); u32x8 z=subtract?x+mod-y:x+y; z=z<z-mod?z:z-mod; std::memcpy(c+j,&z,sizeof z); } } static bool can_split(size_t n,size_t m,size_t k){ return std::min({n,m,k})>64&&n%16==0&&m%16==0&&k%16==0; } [[gnu::noinline]]static void multiply(uint32_t*a,uint32_t*b,uint32_t*c,size_t n,size_t m,size_t k, uint32_t*work){ if(!can_split(n,m,k)){ if(n==64&&m==64&&k==64){ leaf<64>({a,m},{b,k},{c,k},n,m,k); }else{ leaf({a,m},{b,k},{c,k},n,m,k); } return; } n/=2; m/=2; k/=2; auto s=work,t=s+n*m,p=t+m*k; work+=n*m+m*k+n*k; auto a00=a,a01=a+n*m,a10=a+2*n*m,a11=a+3*n*m; auto b00=b,b01=b+m*k,b10=b+2*m*k,b11=b+3*m*k; auto c00=c,c01=c+n*k,c10=c+2*n*k,c11=c+3*n*k; multiply(a00,b00,c11,n,m,k,work); multiply(a01,b10,c00,n,m,k,work); combine(c00,c11,c00,n,k); combine(a10,a11,s,n,m); combine<true>(b01,b00,t,m,k); multiply(s,t,c01,n,m,k,work); combine<true>(s,a00,s,n,m); combine<true>(b11,t,t,m,k); multiply(s,t,c10,n,m,k,work); combine(c11,c10,c10,n,k); combine<true>(a01,s,s,n,m); multiply(s,b11,p,n,m,k,work); combine(c10,c01,c11,n,k); combine(c11,p,c01,n,k); combine<true>(t,b10,t,m,k); multiply(a11,t,p,n,m,k,work); combine<true>(c10,p,c10,n,k); combine<true>(a00,a10,s,n,m); combine<true>(b11,b01,t,m,k); multiply(s,t,p,n,m,k,work); combine(c10,p,c10,n,k); combine(c11,p,c11,n,k); } static u32x8 encode(u32x8 x){ constexpr uint32_t scale=(uint64_t(1)<<32)%mod; constexpr uint32_t quotient=(uint64_t(scale)<<32)/mod; auto packed=u64x4(x),q=u64x4()+quotient; auto lo=mul(packed,q)>>32,hi=mul(packed>>32,q); auto approx=u32x8(lo|(hi&(~uint64_t(0)<<32))); auto out=x*scale-approx*mod; return out<out-mod?out:out-mod; } template<bool unpack=false,int add=0,class matrix> static void copy(matrix&a,uint32_t*ptr,size_t n,size_t m,size_t depth,size_t row=0,size_t col=0){ if(depth){ n/=2; m/=2; for(size_t q=0;q<4;q++){ copy<unpack,add>(a,ptr+q*n*m,n,m,depth-1,row+q/2*n,col+q%2*m); } return; } if(col>=a.m()){ return; } size_t width=std::min(m,a.m()-col); for(size_t i=row;i<std::min(row+n,a.n());i++){ auto data=a[i].data()+col; auto packed=ptr+(i-row)*m; for(size_t j=0;j<width;j++){ if constexpr(unpack&&add!=0){ typename matrix::base value; value.setr(packed[j]); if constexpr(add==1){ data[j] +=value; }else{ data[j] -=value; } }else if constexpr(unpack){ data[j].setr(packed[j]); }else{ packed[j] =uint32_t(data[j].getr()); } } } } template<int add=0,class matrix> static matrix product(matrix const&a,matrix const&b,matrix res={}){ static_assert(add>= -1&&add<=1); auto pad= [](size_t x){ return(x+31)/32*32; }; size_t n=pad(a.n()),m=pad(a.m()),k=pad(b.m()); size_t entries=n*m+m*k+n*k; std::vector<uint32_t>small; big_vector<uint32_t>large; size_t count=entries+entries/3; auto ap=count<(1<<21)?(small.resize(count),small.data()):(large.resize(count),large.data()); auto bp=ap+n*m,cp=bp+m*k; auto scratch=cp+n*k; size_t depth=0; for(size_t x=n,y=m,z=k;can_split(x,y,z);x/=2,y/=2,z/=2){ depth++; } copy(a,ap,n,m,depth); copy(b,bp,m,k,depth); for(size_t i=0;i<n*m;i+=8){ u32x8 x; std::memcpy(&x,ap+i,sizeof x); x=encode(x); std::memcpy(ap+i,&x,sizeof x); } multiply(ap,bp,cp,n,m,k,scratch); if constexpr(add==0){ res=matrix(a.n(),b.m()); } assert(res.n()==a.n()&&res.m()==b.m()); copy<true,add>(res,cp,n,k,depth); return res; } }; }
#pragma GCC pop_options
Back to top page