proconlib

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

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: tests/twelvefold_way/labeled_ball_labeled_box_at_most_1.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/DPL_5_B

#include <iostream>
#include "atcoder/modint.hpp"
#include "tools/twelvefold_way.hpp"

using mint = atcoder::modint1000000007;

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  int n, k;
  std::cin >> n >> k;
  std::cout << tools::twelvefold_way<true, true>::at_most_1<mint>(n, k).val() << '\n';

  return 0;
}
#line 1 "tests/twelvefold_way/labeled_ball_labeled_box_at_most_1.test.cpp"
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/DPL_5_B

#include <iostream>
#line 1 "lib/ac-library/atcoder/modint.hpp"



#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

#line 1 "lib/ac-library/atcoder/internal_math.hpp"



#include <utility>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m`
    explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned long long y = x * _m;
        return (unsigned int)(z - y + (z < y ? _m : 0));
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        // y_max < m * (n + 1)
        // floor(y_max / m) <= n
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

}  // namespace internal

}  // namespace atcoder


#line 1 "lib/ac-library/atcoder/internal_type_traits.hpp"



#line 7 "lib/ac-library/atcoder/internal_type_traits.hpp"

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder


#line 14 "lib/ac-library/atcoder/modint.hpp"

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder


#line 1 "tools/twelvefold_way.hpp"



#line 5 "tools/twelvefold_way.hpp"
#include <algorithm>
#line 1 "tools/is_prime.hpp"



#include <array>
#line 1 "tools/prod_mod.hpp"



#line 1 "tools/uint128_t.hpp"



#line 1 "tools/detail/int128_t.hpp"



#line 6 "tools/detail/int128_t.hpp"
#include <cstddef>
#include <cstdint>
#include <functional>
#line 10 "tools/detail/int128_t.hpp"
#include <limits>
#include <string>
#include <string_view>
#line 1 "tools/abs.hpp"



namespace tools {
  constexpr float abs(const float x) {
    return x < 0 ? -x : x;
  }
  constexpr double abs(const double x) {
    return x < 0 ? -x : x;
  }
  constexpr long double abs(const long double x) {
    return x < 0 ? -x : x;
  }
  constexpr int abs(const int x) {
    return x < 0 ? -x : x;
  }
  constexpr long abs(const long x) {
    return x < 0 ? -x : x;
  }
  constexpr long long abs(const long long x) {
    return x < 0 ? -x : x;
  }
  constexpr unsigned int abs(const unsigned int x) {
    return x;
  }
  constexpr unsigned long abs(const unsigned long x) {
    return x;
  }
  constexpr unsigned long long abs(const unsigned long long x) {
    return x;
  }
}


#line 1 "tools/bit_ceil.hpp"



#include <bit>
#line 1 "tools/is_integral.hpp"



#line 5 "tools/is_integral.hpp"

namespace tools {
  template <typename T>
  struct is_integral : ::std::is_integral<T> {};

  template <typename T>
  inline constexpr bool is_integral_v = ::tools::is_integral<T>::value;
}


#line 1 "tools/is_signed.hpp"



#line 5 "tools/is_signed.hpp"

namespace tools {
  template <typename T>
  struct is_signed : ::std::is_signed<T> {};

  template <typename T>
  inline constexpr bool is_signed_v = ::tools::is_signed<T>::value;
}


#line 1 "tools/make_unsigned.hpp"



#line 5 "tools/make_unsigned.hpp"

namespace tools {
  template <typename T>
  struct make_unsigned : ::std::make_unsigned<T> {};

  template <typename T>
  using make_unsigned_t = typename ::tools::make_unsigned<T>::type;
}


#line 10 "tools/bit_ceil.hpp"

namespace tools {
  template <typename T>
  constexpr T bit_ceil(T) noexcept;

  template <typename T>
  constexpr T bit_ceil(const T x) noexcept {
    static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
    if constexpr (::tools::is_signed_v<T>) {
      assert(x >= 0);
      return ::tools::bit_ceil<::tools::make_unsigned_t<T>>(x);
    } else {
      return ::std::bit_ceil(x);
    }
  }
}


#line 1 "tools/bit_floor.hpp"



#line 10 "tools/bit_floor.hpp"

namespace tools {
  template <typename T>
  constexpr T bit_floor(T) noexcept;

  template <typename T>
  constexpr T bit_floor(const T x) noexcept {
    static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
    if constexpr (::tools::is_signed_v<T>) {
      assert(x >= 0);
      return ::tools::bit_floor<::tools::make_unsigned_t<T>>(x);
    } else {
      return ::std::bit_floor(x);
    }
  }
}


#line 1 "tools/bit_width.hpp"



#line 10 "tools/bit_width.hpp"

namespace tools {
  template <typename T>
  constexpr int bit_width(T) noexcept;

  template <typename T>
  constexpr int bit_width(const T x) noexcept {
    static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
    if constexpr (::tools::is_signed_v<T>) {
      assert(x >= 0);
      return ::tools::bit_width<::tools::make_unsigned_t<T>>(x);
    } else {
      return ::std::bit_width(x);
    }
  }
}


#line 1 "tools/countr_zero.hpp"



#line 12 "tools/countr_zero.hpp"

namespace tools {
  template <typename T>
  constexpr int countr_zero(const T x) noexcept {
    static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
    if constexpr (::tools::is_signed_v<T>) {
      assert(x >= 0);
      return ::std::min(::tools::countr_zero<::tools::make_unsigned_t<T>>(x), ::std::numeric_limits<T>::digits);
    } else {
      return ::std::countr_zero(x);
    }
  }
}


#line 1 "tools/is_unsigned.hpp"



#line 5 "tools/is_unsigned.hpp"

namespace tools {
  template <typename T>
  struct is_unsigned : ::std::is_unsigned<T> {};

  template <typename T>
  inline constexpr bool is_unsigned_v = ::tools::is_unsigned<T>::value;
}


#line 1 "tools/hash_combine.hpp"



#line 6 "tools/hash_combine.hpp"

// Source: https://github.com/google/cityhash/blob/f5dc54147fcce12cefd16548c8e760d68ac04226/src/city.h
// License: MIT
// Author: Google Inc.

// Copyright (c) 2011 Google, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

namespace tools {
  template <typename T>
  void hash_combine(::std::size_t& seed, const T& v) {
    static const ::std::hash<T> hasher;
    static constexpr ::std::size_t k_mul = 0x9ddfea08eb382d69ULL;
    ::std::size_t a = (hasher(v) ^ seed) * k_mul;
    a ^= (a >> 47);
    ::std::size_t b = (seed ^ a) * k_mul;
    b ^= (b >> 47);
    seed = b * k_mul;
  }
}


#line 1 "tools/make_signed.hpp"



#line 5 "tools/make_signed.hpp"

namespace tools {
  template <typename T>
  struct make_signed : ::std::make_signed<T> {};

  template <typename T>
  using make_signed_t = typename ::tools::make_signed<T>::type;
}


#line 1 "tools/now.hpp"



#include <chrono>

namespace tools {
  inline long long now() {
    return ::std::chrono::duration_cast<::std::chrono::nanoseconds>(::std::chrono::high_resolution_clock::now().time_since_epoch()).count();
  }
}


#line 25 "tools/detail/int128_t.hpp"

namespace tools {
  using uint128_t = unsigned __int128;
  using int128_t = __int128;

  namespace detail {
    namespace int128_t {
      constexpr ::tools::uint128_t parse_unsigned(const ::std::string_view s) noexcept {
        assert(!s.empty());
        ::tools::uint128_t x = 0;
        ::std::size_t i = s[0] == '+';
        if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
          for (i += 2; i < s.size(); ++i) {
            assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
            x <<= 4;
            if ('0' <= s[i] && s[i] <= '9') {
              x |= s[i] - '0';
            } else if ('a' <= s[i] && s[i] <= 'f') {
              x |= s[i] - 'a' + 10;
            } else {
              x |= s[i] - 'A' + 10;
            }
          }
        } else {
          for (; i < s.size(); ++i) {
            assert('0' <= s[i] && s[i] <= '9');
            x *= 10;
            x += s[i] - '0';
          }
        }
        return x;
      }

      constexpr ::tools::int128_t parse_signed(const ::std::string_view s) noexcept {
        assert(!s.empty());
        ::tools::int128_t x = 0;
        if (s[0] == '-') {
          ::std::size_t i = 1;
          if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
            for (i += 2; i < s.size(); ++i) {
              assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
              x *= 16;
              if ('0' <= s[i] && s[i] <= '9') {
                x -= s[i] - '0';
              } else if ('a' <= s[i] && s[i] <= 'f') {
                x -= s[i] - 'a' + 10;
              } else {
                x -= s[i] - 'A' + 10;
              }
            }
          } else {
            for (; i < s.size(); ++i) {
              assert('0' <= s[i] && s[i] <= '9');
              x *= 10;
              x -= s[i] - '0';
            }
          }
        } else {
          ::std::size_t i = s[0] == '+';
          if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
            for (i += 2; i < s.size(); ++i) {
              assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
              x <<= 4;
              if ('0' <= s[i] && s[i] <= '9') {
                x |= s[i] - '0';
              } else if ('a' <= s[i] && s[i] <= 'f') {
                x |= s[i] - 'a' + 10;
              } else {
                x |= s[i] - 'A' + 10;
              }
            }
          } else {
            for (; i < s.size(); ++i) {
              assert('0' <= s[i] && s[i] <= '9');
              x *= 10;
              x += s[i] - '0';
            }
          }
        }
        return x;
      }
    }
  }

  constexpr ::tools::uint128_t abs(const ::tools::uint128_t& x) noexcept {
    return x;
  }
  constexpr ::tools::int128_t abs(const ::tools::int128_t& x) {
    return x >= 0 ? x : -x;
  }
}

#define UINT128_C(c) ::tools::detail::int128_t::parse_unsigned(#c)
#define INT128_C(c) ::tools::detail::int128_t::parse_signed(#c)

inline ::std::istream& operator>>(::std::istream& is, ::tools::uint128_t& x) {
  ::std::string s;
  is >> s;
  x = ::tools::detail::int128_t::parse_unsigned(s);
  return is;
}
inline ::std::istream& operator>>(::std::istream& is, ::tools::int128_t& x) {
  ::std::string s;
  is >> s;
  x = ::tools::detail::int128_t::parse_signed(s);
  return is;
}

inline ::std::ostream& operator<<(::std::ostream& os, ::tools::uint128_t x) {
  ::std::string s;
  if (x > 0) {
    while (x > 0) {
      s.push_back('0' + x % 10);
      x /= 10;
    }
  } else {
    s.push_back('0');
  }

  ::std::ranges::reverse(s);
  return os << s;
}
inline ::std::ostream& operator<<(::std::ostream& os, ::tools::int128_t x) {
  ::std::string s;
  if (x > 0) {
    while (x > 0) {
      s.push_back('0' + x % 10);
      x /= 10;
    }
  } else if (x < 0) {
    while (x < 0) {
      s.push_back('0' + (-(x % 10)));
      x /= 10;
    }
    s.push_back('-');
  } else {
    s.push_back('0');
  }

  ::std::ranges::reverse(s);
  return os << s;
}

#if defined(__GLIBCXX__) && defined(__STRICT_ANSI__)
namespace std {
  template <>
  struct hash<::tools::uint128_t> {
    ::std::size_t operator()(const ::tools::uint128_t& x) const {
      static const ::std::size_t seed = ::tools::now();

      ::std::size_t hash = seed;
      ::tools::hash_combine(hash, static_cast<::std::uint64_t>(x >> 64));
      ::tools::hash_combine(hash, static_cast<::std::uint64_t>(x & ((UINT128_C(1) << 64) - 1)));
      return hash;
    }
  };
  template <>
  struct hash<::tools::int128_t> {
    ::std::size_t operator()(const ::tools::int128_t& x) const {
      static ::std::hash<::tools::uint128_t> hasher;
      return hasher(static_cast<::tools::uint128_t>(x));
    }
  };
}
#endif

namespace tools {
  template <>
  struct is_integral<::tools::int128_t> : ::std::true_type {};
  template <>
  struct is_integral<::tools::uint128_t> : ::std::true_type {};
  template <>
  struct is_integral<const ::tools::int128_t> : ::std::true_type {};
  template <>
  struct is_integral<const ::tools::uint128_t> : ::std::true_type {};
  template <>
  struct is_integral<volatile ::tools::int128_t> : ::std::true_type {};
  template <>
  struct is_integral<volatile ::tools::uint128_t> : ::std::true_type {};
  template <>
  struct is_integral<const volatile ::tools::int128_t> : ::std::true_type {};
  template <>
  struct is_integral<const volatile ::tools::uint128_t> : ::std::true_type {};

  template <>
  struct is_signed<::tools::int128_t> : ::std::true_type {};
  template <>
  struct is_signed<::tools::uint128_t> : ::std::false_type {};
  template <>
  struct is_signed<const ::tools::int128_t> : ::std::true_type {};
  template <>
  struct is_signed<const ::tools::uint128_t> : ::std::false_type {};
  template <>
  struct is_signed<volatile ::tools::int128_t> : ::std::true_type {};
  template <>
  struct is_signed<volatile ::tools::uint128_t> : ::std::false_type {};
  template <>
  struct is_signed<const volatile ::tools::int128_t> : ::std::true_type {};
  template <>
  struct is_signed<const volatile ::tools::uint128_t> : ::std::false_type {};

  template <>
  struct is_unsigned<::tools::int128_t> : ::std::false_type {};
  template <>
  struct is_unsigned<::tools::uint128_t> : ::std::true_type {};
  template <>
  struct is_unsigned<const ::tools::int128_t> : ::std::false_type {};
  template <>
  struct is_unsigned<const ::tools::uint128_t> : ::std::true_type {};
  template <>
  struct is_unsigned<volatile ::tools::int128_t> : ::std::false_type {};
  template <>
  struct is_unsigned<volatile ::tools::uint128_t> : ::std::true_type {};
  template <>
  struct is_unsigned<const volatile ::tools::int128_t> : ::std::false_type {};
  template <>
  struct is_unsigned<const volatile ::tools::uint128_t> : ::std::true_type {};

  template <>
  struct make_signed<::tools::int128_t> {
    using type = ::tools::int128_t;
  };
  template <>
  struct make_signed<::tools::uint128_t> {
    using type = ::tools::int128_t;
  };
  template <>
  struct make_signed<const ::tools::int128_t> {
    using type = const ::tools::int128_t;
  };
  template <>
  struct make_signed<const ::tools::uint128_t> {
    using type = const ::tools::int128_t;
  };
  template <>
  struct make_signed<volatile ::tools::int128_t> {
    using type = volatile ::tools::int128_t;
  };
  template <>
  struct make_signed<volatile ::tools::uint128_t> {
    using type = volatile ::tools::int128_t;
  };
  template <>
  struct make_signed<const volatile ::tools::int128_t> {
    using type = const volatile ::tools::int128_t;
  };
  template <>
  struct make_signed<const volatile ::tools::uint128_t> {
    using type = const volatile ::tools::int128_t;
  };

  template <>
  struct make_unsigned<::tools::int128_t> {
    using type = ::tools::uint128_t;
  };
  template <>
  struct make_unsigned<::tools::uint128_t> {
    using type = ::tools::uint128_t;
  };
  template <>
  struct make_unsigned<const ::tools::int128_t> {
    using type = const ::tools::uint128_t;
  };
  template <>
  struct make_unsigned<const ::tools::uint128_t> {
    using type = const ::tools::uint128_t;
  };
  template <>
  struct make_unsigned<volatile ::tools::int128_t> {
    using type = volatile ::tools::uint128_t;
  };
  template <>
  struct make_unsigned<volatile ::tools::uint128_t> {
    using type = volatile ::tools::uint128_t;
  };
  template <>
  struct make_unsigned<const volatile ::tools::int128_t> {
    using type = const volatile ::tools::uint128_t;
  };
  template <>
  struct make_unsigned<const volatile ::tools::uint128_t> {
    using type = const volatile ::tools::uint128_t;
  };

#if defined(__GLIBCXX__) && defined(__STRICT_ANSI__)
  template <>
  constexpr ::tools::uint128_t bit_ceil<::tools::uint128_t>(::tools::uint128_t x) noexcept {
    if (x <= 1) return 1;
    --x;
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;
    x |= x >> 32;
    x |= x >> 64;
    return ++x;
  }

  template <>
  constexpr ::tools::uint128_t bit_floor<::tools::uint128_t>(::tools::uint128_t x) noexcept {
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;
    x |= x >> 32;
    x |= x >> 64;
    return x & ~(x >> 1);
  }

  template <>
  constexpr int bit_width<::tools::uint128_t>(::tools::uint128_t x) noexcept {
    int w = 0;
    if (x & UINT128_C(0xffffffffffffffff0000000000000000)) {
      x >>= 64;
      w += 64;
    }
    if (x & UINT128_C(0xffffffff00000000)) {
      x >>= 32;
      w += 32;
    }
    if (x & UINT128_C(0xffff0000)) {
      x >>= 16;
      w += 16;
    }
    if (x & UINT128_C(0xff00)) {
      x >>= 8;
      w += 8;
    }
    if (x & UINT128_C(0xf0)) {
      x >>= 4;
      w += 4;
    }
    if (x & UINT128_C(0xc)) {
      x >>= 2;
      w += 2;
    }
    if (x & UINT128_C(0x2)) {
      x >>= 1;
      w += 1;
    }
    w += x;
    return w;
  }

  namespace detail {
    namespace countr_zero {
      template <::std::size_t N>
      struct ntz_traits;

      template <>
      struct ntz_traits<128> {
        using type = ::tools::uint128_t;
        static constexpr int shift = 120;
        static constexpr type magic = UINT128_C(0x01061438916347932a5cd9d3ead7b77f);
        static constexpr int ntz_table[255] = {
          128,   0,   1,  -1,   2,  -1,   8,  -1,   3,  -1,  15,  -1,   9,  -1,  22,  -1,
            4,  -1,  29,  -1,  16,  -1,  36,  -1,  10,  -1,  43,  -1,  23,  -1,  50,  -1,
            5,  -1,  33,  -1,  30,  -1,  57,  -1,  17,  -1,  64,  -1,  37,  -1,  71,  -1,
           11,  -1,  60,  -1,  44,  -1,  78,  -1,  24,  -1,  85,  -1,  51,  -1,  92,  -1,
           -1,   6,  -1,  20,  -1,  34,  -1,  48,  31,  -1,  -1,  69,  58,  -1,  -1,  90,
           18,  -1,  67,  -1,  65,  -1,  99,  -1,  38,  -1, 101,  -1,  72,  -1, 106,  -1,
           -1,  12,  -1,  40,  -1,  61,  -1,  82,  45,  -1,  -1, 103,  79,  -1, 113,  -1,
           -1,  25,  -1,  74,  86,  -1,  -1, 116,  -1,  52,  -1, 108,  -1,  93,  -1, 120,
          127,  -1,  -1,   7,  -1,  14,  -1,  21,  -1,  28,  -1,  35,  -1,  42,  -1,  49,
           -1,  32,  -1,  56,  -1,  63,  -1,  70,  -1,  59,  -1,  77,  -1,  84,  -1,  91,
           -1,  19,  -1,  47,  -1,  68,  -1,  89,  -1,  66,  -1,  98,  -1, 100,  -1, 105,
           -1,  39,  -1,  81,  -1, 102,  -1, 112,  -1,  73,  -1, 115,  -1, 107,  -1, 119,
          126,  -1,  13,  -1,  27,  -1,  41,  -1,  -1,  55,  62,  -1,  -1,  76,  83,  -1,
           -1,  46,  -1,  88,  -1,  97,  -1, 104,  -1,  80,  -1, 111,  -1, 114,  -1, 118,
          125,  -1,  26,  -1,  54,  -1,  75,  -1,  -1,  87,  96,  -1,  -1, 110,  -1, 117,
          124,  -1,  53,  -1,  -1,  95, 109,  -1, 123,  -1,  94,  -1, 122,  -1, 121
        };
      };

      template <typename T>
      constexpr int impl(const T x) noexcept {
        using tr = ::tools::detail::countr_zero::ntz_traits<::std::numeric_limits<T>::digits>;
        using type = typename tr::type;
        return tr::ntz_table[static_cast<type>(tr::magic * static_cast<type>(x & -x)) >> tr::shift];
      }
    }
  }

  template <>
  constexpr int countr_zero<::tools::uint128_t>(const ::tools::uint128_t x) noexcept {
    return ::tools::detail::countr_zero::impl(x);
  }
#endif
}


#line 5 "tools/uint128_t.hpp"


#line 5 "tools/prod_mod.hpp"

namespace tools {

  template <typename T1, typename T2, typename T3>
  constexpr T3 prod_mod(const T1 x, const T2 y, const T3 m) {
    using u128 = ::tools::uint128_t;
    u128 prod_mod = u128(x >= 0 ? x : -x) * u128(y >= 0 ? y : -y) % u128(m);
    if ((x >= 0) ^ (y >= 0)) prod_mod = u128(m) - prod_mod;
    return prod_mod;
  }
}


#line 1 "tools/pow_mod.hpp"



#line 1 "tools/mod.hpp"



#line 7 "tools/mod.hpp"

namespace tools {

  template <typename M, typename N> requires (
    ::tools::is_integral_v<M> && !::std::is_same_v<::std::remove_cv_t<M>, bool> &&
    ::tools::is_integral_v<N> && !::std::is_same_v<::std::remove_cv_t<N>, bool>)
  constexpr ::std::common_type_t<M, N> mod(const M a, const N b) noexcept {
    assert(b != 0);

    using UM = ::std::make_unsigned_t<M>;
    using UN = ::std::make_unsigned_t<N>;
    const UM ua = a >= 0 ? a : static_cast<UM>(-(a + 1)) + 1;
    const UN ub = b >= 0 ? b : static_cast<UN>(-(b + 1)) + 1;
    auto r = ua % ub;
    if (a < 0 && r > 0) {
      r = ub - r;
    }
    return r;
  }
}


#line 6 "tools/pow_mod.hpp"

namespace tools {

  template <typename T1, typename T2, typename T3>
  constexpr T3 pow_mod(const T1 x, T2 n, const T3 m) {
    if (m == 1) return 0;
    T3 r = 1;
    T3 y = ::tools::mod(x, m);
    while (n > 0) {
      if ((n & 1) > 0) {
        r = ::tools::prod_mod(r, y, m);
      }
      y = ::tools::prod_mod(y, y, m);
      n /= 2;
    }
    return r;
  }
}


#line 7 "tools/is_prime.hpp"

namespace tools {

  constexpr bool is_prime(const unsigned long long n) {
    constexpr ::std::array<unsigned long long, 7> bases = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};

    if (n <= 1) return false;
    if (n == 2) return true;
    if (n % 2 == 0) return false;

    auto d = n - 1;
    for (; d % 2 == 0; d /= 2);

    for (const auto a : bases) {
      if (a % n == 0) return true;

      auto power = d;
      auto target = ::tools::pow_mod(a, power, n);

      bool is_composite = true;
      if (target == 1) is_composite = false;
      for (; is_composite && power != n - 1; power *= 2, target = ::tools::prod_mod(target, target, n)) {
        if (target == n - 1) is_composite = false;
      }

      if (is_composite) {
        return false;
      }
    }

    return true;
  }
}


#line 1 "tools/fact_mod_cache.hpp"



#include <vector>
#line 6 "tools/fact_mod_cache.hpp"
#include <iterator>
#line 8 "tools/fact_mod_cache.hpp"
#include <cmath>
#line 10 "tools/fact_mod_cache.hpp"

namespace tools {

  template <class M>
  class fact_mod_cache {
    ::std::vector<M> m_inv;
    ::std::vector<M> m_fact;
    ::std::vector<M> m_fact_inv;

  public:
    fact_mod_cache() : m_inv({M::raw(0), M::raw(1)}), m_fact({M::raw(1), M::raw(1)}), m_fact_inv({M::raw(1), M::raw(1)}) {
      assert(::tools::is_prime(M::mod()));
    }
    explicit fact_mod_cache(const long long max) : fact_mod_cache() {
      this->fact(::std::min<long long>(max, M::mod() - 1));
      this->fact_inv(::std::min<long long>(max, M::mod() - 1));
    }

    M inv(const long long n) {
      assert(n % M::mod() != 0);
      const long long size = ::std::ssize(this->m_inv);
      this->m_inv.resize(::std::clamp<long long>(::std::abs(n) + 1, size, M::mod()));
      for (long long i = size; i < ::std::ssize(this->m_inv); ++i) {
        this->m_inv[i] = -this->m_inv[M::mod() % i] * M::raw(M::mod() / i);
      }
      M result = this->m_inv[::std::abs(n) % M::mod()];
      if (n < 0) result = -result;
      return result;
    }
    M fact(const long long n) {
      assert(n >= 0);
      const long long size = ::std::ssize(this->m_fact);
      this->m_fact.resize(::std::clamp<long long>(n + 1, size, M::mod()));
      for (long long i = size; i < ::std::ssize(this->m_fact); ++i) {
        this->m_fact[i] = this->m_fact[i - 1] * M::raw(i);
      }
      return n < M::mod() ? this->m_fact[n] : M::raw(0);
    }
    M fact_inv(const long long n) {
      assert(0 <= n && n < M::mod());
      const long long size = ::std::ssize(this->m_fact_inv);
      this->m_fact_inv.resize(::std::max<long long>(size, n + 1));
      this->inv(this->m_fact_inv.size() - 1);
      for (long long i = size; i < ::std::ssize(this->m_fact_inv); ++i) {
        this->m_fact_inv[i] = this->m_fact_inv[i - 1] * this->m_inv[i];
      }
      return this->m_fact_inv[n];
    }

    M binomial(long long n, long long r) {
      if (r < 0) return M::raw(0);
      if (0 <= n && n < r) return M::raw(0);
      if (n < 0) return M(1 - ((r & 1) << 1)) * this->binomial(-n + r - 1, r);

      this->fact(::std::min<long long>(n, M::mod() - 1));
      this->fact_inv(::std::min<long long>(n, M::mod() - 1));
      const auto c = [&](const long long nn, const long long rr) {
        return 0 <= rr && rr <= nn ? this->m_fact[nn] * this->m_fact_inv[nn - rr] * this->m_fact_inv[rr] : M::raw(0);
      };

      M result(1);
      while (n > 0 || r > 0) {
        result *= c(n % M::mod(), r % M::mod());
        n /= M::mod();
        r /= M::mod();
      }

      return result;
    }
    M combination(const long long n, const long long r) {
      if (!(0 <= r && r <= n)) return M::raw(0);
      return this->binomial(n, r);
    }
    M permutation(const long long n, const long long r) {
      if (!(0 <= r && r <= n)) return M::raw(0);
      return this->binomial(n, r) * this->fact(r);
    }
    M combination_with_repetition(const long long n, const long long r) {
      if (n < 0 || r < 0) return M::raw(0);
      return this->binomial(n + r - 1, r);
    }
  };
}


#line 1 "tools/large_fact_mod_cache.hpp"



#line 1 "tools/pow_mod_cache.hpp"



#line 5 "tools/pow_mod_cache.hpp"
#include <optional>
#line 1 "tools/find_cycle.hpp"



#line 5 "tools/find_cycle.hpp"

namespace tools {

  template <typename T, typename F>
  ::std::pair<long long, long long> find_cycle(const T& seed, const F& f) {
    auto i = 1LL;
    auto j = 2LL;
    T x = f(seed);
    T y = f(f(seed));
    for (; x != y; ++i, j += 2, x = f(x), y = f(f(y)));

    i = 0;
    x = seed;
    for (; x != y; ++i, ++j, x = f(x), y = f(y));

    const auto head = i;

    ++i;
    j = i + 1;
    x = f(x);
    y = f(f(y));
    for (; x != y; ++i, j += 2, x = f(x), y = f(f(y)));

    const auto cycle = j - i;

    return ::std::make_pair(head, cycle);
  }
}


#line 1 "tools/floor.hpp"



#line 7 "tools/floor.hpp"

namespace tools {

  template <typename M, typename N> requires (
    ::tools::is_integral_v<M> && !::std::is_same_v<::std::remove_cv_t<M>, bool> &&
    ::tools::is_integral_v<N> && !::std::is_same_v<::std::remove_cv_t<N>, bool>)
  constexpr ::std::common_type_t<M, N> floor(const M x, const N y) noexcept {
    assert(y != 0);
    if (y >= 0) {
      if (x >= 0) {
        return x / y;
      } else {
        return (x + 1) / y - 1;
      }
    } else {
      if (x > 0) {
        return (x - 1) / y - 1;
      } else {
        return x / y;
      }
    }
  }
}


#line 1 "tools/ceil.hpp"



#line 8 "tools/ceil.hpp"

namespace tools {
  template <typename M, typename N> requires (
    ::tools::is_integral_v<M> && !::std::is_same_v<::std::remove_cv_t<M>, bool> &&
    ::tools::is_integral_v<N> && !::std::is_same_v<::std::remove_cv_t<N>, bool>)
  constexpr ::std::common_type_t<M, N> ceil(const M x, const N y) noexcept {
    assert(y != 0);
    if (y >= 0) {
      if (x > 0) {
        return (x - 1) / y + 1;
      } else {
        if constexpr (::tools::is_unsigned_v<::std::common_type_t<M, N>>) {
          return 0;
        } else {
          return x / y;
        }
      }
    } else {
      if (x >= 0) {
        if constexpr (::tools::is_unsigned_v<::std::common_type_t<M, N>>) {
          return 0;
        } else {
          return x / y;
        }
      } else {
        return (x + 1) / y + 1;
      }
    }
  }
}


#line 16 "tools/pow_mod_cache.hpp"

namespace tools {

  template <class M>
  class pow_mod_cache {
    ::std::vector<M> m_pow;
    ::std::vector<M> m_cumsum;
    ::std::vector<M> m_inv_pow;
    ::std::vector<M> m_inv_cumsum;
    ::std::optional<::std::pair<long long, long long>> m_period;

  public:
    pow_mod_cache() = default;
    explicit pow_mod_cache(const M base) : m_pow({M(1), base}), m_cumsum({M::raw(0)}), m_inv_pow({M(1)}), m_inv_cumsum({M::raw(0)}) {
      if (base == M(-1)) {
        if (M::mod() > 2) {
          this->m_period = ::std::make_pair(0LL, 2LL);
        } else {
          this->m_period = ::std::make_pair(0LL, 1LL);
          this->m_pow.resize(1);
        }
        this->m_inv_pow.clear();
        this->m_inv_cumsum.clear();
      }
    }
    template <typename Z, ::std::enable_if_t<::std::is_integral_v<Z>, ::std::nullptr_t> = nullptr>
    explicit pow_mod_cache(const Z base) : pow_mod_cache(M(base)) {
    }

    M operator[](const long long n) {
      if (!this->m_period) {
        if (::std::max<long long>(::std::ssize(this->m_pow) - 1, n) - ::std::min<long long>(n, -(::std::ssize(this->m_inv_pow) - 1)) + 1 < M::mod() - 1) {
          if (n >= 0) {
            const long long size = ::std::ssize(this->m_pow);
            this->m_pow.resize(::std::max(size, n + 1));
            for (long long i = size; i < ::std::ssize(this->m_pow); ++i) {
              this->m_pow[i] = this->m_pow[i - 1] * this->m_pow[1];
            }
            return this->m_pow[n];
          } else {
            if (this->m_inv_pow.size() == 1) {
              this->m_inv_pow.push_back(this->m_pow[1].inv());
            }
            const long long size = ::std::ssize(this->m_inv_pow);
            this->m_inv_pow.resize(::std::max(size, -n + 1));
            for (long long i = size; i < ::std::ssize(this->m_inv_pow); ++i) {
              this->m_inv_pow[i] = this->m_inv_pow[i - 1] * this->m_inv_pow[1];
            }
            return this->m_inv_pow[-n];
          }
        }

        this->m_period = ::tools::find_cycle(this->m_pow[0], [&](const M& prev) { return prev * this->m_pow[1]; });
        const long long size = ::std::ssize(this->m_pow);
        this->m_pow.resize(this->m_period->first + this->m_period->second);
        for (long long i = size; i < ::std::ssize(this->m_pow); ++i) {
          this->m_pow[i] = this->m_pow[i - 1] * this->m_pow[1];
        }
        this->m_inv_pow.clear();
        this->m_inv_cumsum.clear();
      }

      if (this->m_period->first == 0) {
        return this->m_pow[::tools::mod(n, this->m_period->second)];
      } else {
        assert(n >= 0);
        if (n < this->m_period->first + this->m_period->second) {
          return this->m_pow[n];
        } else {
          return this->m_pow[(n - this->m_period->first) % this->m_period->second + this->m_period->first];
        }
      }
    }

    M sum(const long long l, const long long r) {
      if (l >= r) return M::raw(0);

      (*this)[r - 1];
      (*this)[l];

      {
        const long long size = ::std::ssize(this->m_cumsum);
        this->m_cumsum.resize(this->m_pow.size() + 1);
        for (long long i = size; i < ::std::ssize(this->m_cumsum); ++i) {
          this->m_cumsum[i] = this->m_cumsum[i - 1] + this->m_pow[i - 1];
        }
      }

      if (!this->m_period) {
        const long long size = ::std::ssize(this->m_inv_cumsum);
        this->m_inv_cumsum.resize(this->m_inv_pow.size() + 1);
        for (long long i = size; i < ::std::ssize(this->m_inv_cumsum); ++i) {
          this->m_inv_cumsum[i] = this->m_inv_cumsum[i - 1] + this->m_pow[i - 1];
        }

        if (l >= 0) {
          return this->m_cumsum[r] - this->m_cumsum[l];
        } else if (r <= 0) {
          return this->m_inv_cumsum[-l] - this->m_inv_cumsum[-r];
        } else {
          return (this->m_inv_cumsum[-l] - this->m_inv_cumsum[1]) + (this->m_cumsum[r] - this->m_cumsum[0]);
        }
      }

      static const auto cumsum = [&](const long long ll, const long long rr) {
        return this->m_cumsum[rr] - this->m_cumsum[ll];
      };

      if (l >= 0) {
        static const auto f = [&](const long long x) {
          if (x <= this->m_period->first + this->m_period->second) {
            return cumsum(0, x);
          } else {
            return cumsum(0, this->m_period->first) +
              cumsum(this->m_period->first, this->m_period->first + this->m_period->second) * ((x - this->m_period->first) / this->m_period->second) +
              cumsum(this->m_period->first, (x - this->m_period->first) % this->m_period->second + this->m_period->first);
          }
        };
        return f(r) - f(l);
      } else {
        const auto& n = this->m_period->second;
        return cumsum(::tools::mod(l, n), n) + cumsum(0, ::tools::mod(r, n)) + cumsum(0, n) * M(::tools::floor(r, n) - ::tools::ceil(l, n));
      }
    }
  };
}


#line 1 "tools/ceil_log2.hpp"



#line 6 "tools/ceil_log2.hpp"

namespace tools {
  template <typename T>
  constexpr T ceil_log2(T x) noexcept {
    assert(x > 0);
    return ::tools::bit_width(x - 1);
  }
}


#line 1 "tools/pow2.hpp"



#line 6 "tools/pow2.hpp"

namespace tools {

  template <typename T, typename ::std::enable_if<::std::is_unsigned<T>::value, ::std::nullptr_t>::type = nullptr>
  constexpr T pow2(const T x) {
    return static_cast<T>(1) << x;
  }

  template <typename T, typename ::std::enable_if<::std::is_signed<T>::value, ::std::nullptr_t>::type = nullptr>
  constexpr T pow2(const T x) {
    return static_cast<T>(static_cast<typename ::std::make_unsigned<T>::type>(1) << static_cast<typename ::std::make_unsigned<T>::type>(x));
  }
}


#line 1 "tools/sample_point_shift.hpp"



#line 9 "tools/sample_point_shift.hpp"
#include <initializer_list>
#line 1 "tools/online_cumsum.hpp"



#line 1 "tools/group.hpp"



namespace tools {
  namespace group {
    template <typename G>
    struct plus {
      using T = G;
      static T op(const T& lhs, const T& rhs) {
        return lhs + rhs;
      }
      static T e() {
        return T(0);
      }
      static T inv(const T& v) {
        return -v;
      }
    };

    template <typename G>
    struct multiplies {
      using T = G;
      static T op(const T& lhs, const T& rhs) {
        return lhs * rhs;
      }
      static T e() {
        return T(1);
      }
      static T inv(const T& v) {
        return e() / v;
      }
    };

    template <typename G>
    struct bit_xor {
      using T = G;
      static T op(const T& lhs, const T& rhs) {
        return lhs ^ rhs;
      }
      static T e() {
        return T(0);
      }
      static T inv(const T& v) {
        return v;
      }
    };
  }
}


#line 1 "tools/is_monoid.hpp"



#line 6 "tools/is_monoid.hpp"

namespace tools {

  template <typename M, typename = void>
  struct is_monoid : ::std::false_type {};

  template <typename M>
  struct is_monoid<M, ::std::enable_if_t<
    ::std::is_same_v<typename M::T, decltype(M::op(::std::declval<typename M::T>(), ::std::declval<typename M::T>()))> &&
    ::std::is_same_v<typename M::T, decltype(M::e())>
  , void>> : ::std::true_type {};

  template <typename M>
  inline constexpr bool is_monoid_v = ::tools::is_monoid<M>::value;
}


#line 1 "tools/is_group.hpp"



#line 6 "tools/is_group.hpp"

namespace tools {

  template <typename G, typename = void>
  struct is_group : ::std::false_type {};

  template <typename G>
  struct is_group<G, ::std::enable_if_t<
    ::std::is_same_v<typename G::T, decltype(G::op(::std::declval<typename G::T>(), ::std::declval<typename G::T>()))> &&
    ::std::is_same_v<typename G::T, decltype(G::e())> &&
    ::std::is_same_v<typename G::T, decltype(G::inv(::std::declval<typename G::T>()))>
  , void>> : ::std::true_type {};

  template <typename G>
  inline constexpr bool is_group_v = ::tools::is_group<G>::value;
}


#line 10 "tools/online_cumsum.hpp"

namespace tools {
  template <typename X, bool Forward = true>
  class online_cumsum {
    using M = ::std::conditional_t<::tools::is_monoid_v<X>, X, ::tools::group::plus<X>>;
    using T = typename M::T;
    ::std::vector<T> m_vector;
    ::std::vector<T> m_cumsum;
    int m_processed;

  public:
    online_cumsum() : online_cumsum(0) {
    }
    online_cumsum(const int n) : m_vector(n, M::e()), m_cumsum(n + 1, M::e()), m_processed(Forward ? 0 : n) {
    }

    int size() const {
      return this->m_vector.size();
    }
    T& operator[](const int i) {
      assert(0 <= i && i < this->size());
      return this->m_vector[i];
    }

    auto begin() { return this->m_vector.begin(); }
    auto begin() const { return this->m_vector.begin(); }
    auto cbegin() const { return this->m_vector.cbegin(); }
    auto end() { return this->m_vector.end(); }
    auto end() const { return this->m_vector.end(); }
    auto cend() const { return this->m_vector.cend(); }
    auto rbegin() { return this->m_vector.rbegin(); }
    auto rbegin() const { return this->m_vector.rbegin(); }
    auto crbegin() const { return this->m_vector.crbegin(); }
    auto rend() { return this->m_vector.rend(); }
    auto rend() const { return this->m_vector.rend(); }
    auto crend() const { return this->m_vector.crend(); }

    T prod(const int l, const int r) {
      assert(0 <= l && l <= r && r <= this->size());
      if constexpr (Forward) {
        for (; this->m_processed < r; ++this->m_processed) {
          this->m_cumsum[this->m_processed + 1] = M::op(this->m_cumsum[this->m_processed], this->m_vector[this->m_processed]);
        }
        if constexpr (::tools::is_group_v<M>) {
          return M::op(M::inv(this->m_cumsum[l]), this->m_cumsum[r]);
        } else {
          assert(l == 0);
          return this->m_cumsum[r];
        }
      } else {
        for (; this->m_processed > l; --this->m_processed) {
          this->m_cumsum[this->m_processed - 1] = M::op(this->m_vector[this->m_processed - 1], this->m_cumsum[this->m_processed]);
        }
        if constexpr (::tools::is_group_v<M>) {
          return M::op(this->m_cumsum[l], M::inv(this->m_cumsum[r]));
        } else {
          assert(r == this->size());
          return this->m_cumsum[l];
        }
      }
    }
    template <typename Y = X> requires (!::tools::is_monoid_v<Y>)
    T sum(const int l, const int r) {
      return this->prod(l, r);
    }
  };
}


#line 1 "tools/monoid.hpp"



#line 1 "tools/gcd.hpp"



#line 6 "tools/gcd.hpp"

namespace tools {
  template <typename M, typename N>
  constexpr ::std::common_type_t<M, N> gcd(const M m, const N n) {
    return ::std::gcd(m, n);
  }
}


#line 9 "tools/monoid.hpp"

namespace tools {
  namespace monoid {
    template <typename M, M ...dummy>
    struct max;

    template <typename M>
    struct max<M> {
      static_assert(::std::is_arithmetic_v<M>, "M must be a built-in arithmetic type.");

      using T = M;
      static T op(const T lhs, const T rhs) {
        return ::std::max(lhs, rhs);
      }
      static T e() {
        if constexpr (::std::is_integral_v<M>) {
          return ::std::numeric_limits<M>::min();
        } else {
          return -::std::numeric_limits<M>::infinity();
        }
      }
    };

    template <typename M, M E>
    struct max<M, E> {
      static_assert(::std::is_integral_v<M>, "M must be a built-in integral type.");

      using T = M;
      static T op(const T lhs, const T rhs) {
        assert(E <= lhs);
        assert(E <= rhs);
        return ::std::max(lhs, rhs);
      }
      static T e() {
        return E;
      }
    };

    template <typename M, M ...dummy>
    struct min;

    template <typename M>
    struct min<M> {
      static_assert(::std::is_arithmetic_v<M>, "M must be a built-in arithmetic type.");

      using T = M;
      static T op(const T lhs, const T rhs) {
        return ::std::min(lhs, rhs);
      }
      static T e() {
        if constexpr (::std::is_integral_v<M>) {
          return ::std::numeric_limits<M>::max();
        } else {
          return ::std::numeric_limits<M>::infinity();
        }
      }
    };

    template <typename M, M E>
    struct min<M, E> {
      static_assert(::std::is_integral_v<M>, "M must be a built-in integral type.");

      using T = M;
      static T op(const T lhs, const T rhs) {
        assert(lhs <= E);
        assert(rhs <= E);
        return ::std::min(lhs, rhs);
      }
      static T e() {
        return E;
      }
    };

    template <typename M>
    struct multiplies {
    private:
      using VR = ::std::conditional_t<::std::is_arithmetic_v<M>, const M, const M&>;

    public:
      using T = M;
      static T op(VR lhs, VR rhs) {
        return lhs * rhs;
      }
      static T e() {
        return T(1);
      }
    };

    template <>
    struct multiplies<bool> {
      using T = bool;
      static T op(const bool lhs, const bool rhs) {
        return lhs && rhs;
      }
      static T e() {
        return true;
      }
    };

    template <typename M>
    struct gcd {
    private:
      static_assert(!::std::is_arithmetic_v<M> || (::std::is_integral_v<M> && !::std::is_same_v<M, bool>), "If M is a built-in arithmetic type, it must be integral except for bool.");
      using VR = ::std::conditional_t<::std::is_arithmetic_v<M>, const M, const M&>;

    public:
      using T = M;
      static T op(VR lhs, VR rhs) {
        return ::tools::gcd(lhs, rhs);
      }
      static T e() {
        return T(0);
      }
    };

    template <typename M, M E>
    struct update {
      static_assert(::std::is_integral_v<M>, "M must be a built-in integral type.");

      using T = M;
      static T op(const T lhs, const T rhs) {
        return lhs == E ? rhs : lhs;
      }
      static T e() {
        return E;
      }
    };
  }
}


#line 1 "tools/convolution.hpp"



#line 5 "tools/convolution.hpp"
#include <complex>
#line 1 "lib/ac-library/atcoder/convolution.hpp"



#line 9 "lib/ac-library/atcoder/convolution.hpp"

#line 1 "lib/ac-library/atcoder/internal_bit.hpp"



#ifdef _MSC_VER
#include <intrin.h>
#endif

#if __cplusplus >= 202002L
#line 10 "lib/ac-library/atcoder/internal_bit.hpp"
#endif

namespace atcoder {

namespace internal {

#if __cplusplus >= 202002L

using std::bit_ceil;

#else

// @return same with std::bit::bit_ceil
unsigned int bit_ceil(unsigned int n) {
    unsigned int x = 1;
    while (x < (unsigned int)(n)) x *= 2;
    return x;
}

#endif

// @param n `1 <= n`
// @return same with std::bit::countr_zero
int countr_zero(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

// @param n `1 <= n`
// @return same with std::bit::countr_zero
constexpr int countr_zero_constexpr(unsigned int n) {
    int x = 0;
    while (!(n & (1 << x))) x++;
    return x;
}

}  // namespace internal

}  // namespace atcoder


#line 12 "lib/ac-library/atcoder/convolution.hpp"

namespace atcoder {

namespace internal {

template <class mint,
          int g = internal::primitive_root<mint::mod()>,
          internal::is_static_modint_t<mint>* = nullptr>
struct fft_info {
    static constexpr int rank2 = countr_zero_constexpr(mint::mod() - 1);
    std::array<mint, rank2 + 1> root;   // root[i]^(2^i) == 1
    std::array<mint, rank2 + 1> iroot;  // root[i] * iroot[i] == 1

    std::array<mint, std::max(0, rank2 - 2 + 1)> rate2;
    std::array<mint, std::max(0, rank2 - 2 + 1)> irate2;

    std::array<mint, std::max(0, rank2 - 3 + 1)> rate3;
    std::array<mint, std::max(0, rank2 - 3 + 1)> irate3;

    fft_info() {
        root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2);
        iroot[rank2] = root[rank2].inv();
        for (int i = rank2 - 1; i >= 0; i--) {
            root[i] = root[i + 1] * root[i + 1];
            iroot[i] = iroot[i + 1] * iroot[i + 1];
        }

        {
            mint prod = 1, iprod = 1;
            for (int i = 0; i <= rank2 - 2; i++) {
                rate2[i] = root[i + 2] * prod;
                irate2[i] = iroot[i + 2] * iprod;
                prod *= iroot[i + 2];
                iprod *= root[i + 2];
            }
        }
        {
            mint prod = 1, iprod = 1;
            for (int i = 0; i <= rank2 - 3; i++) {
                rate3[i] = root[i + 3] * prod;
                irate3[i] = iroot[i + 3] * iprod;
                prod *= iroot[i + 3];
                iprod *= root[i + 3];
            }
        }
    }
};

template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly(std::vector<mint>& a) {
    int n = int(a.size());
    int h = internal::countr_zero((unsigned int)n);

    static const fft_info<mint> info;

    int len = 0;  // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed
    while (len < h) {
        if (h - len == 1) {
            int p = 1 << (h - len - 1);
            mint rot = 1;
            for (int s = 0; s < (1 << len); s++) {
                int offset = s << (h - len);
                for (int i = 0; i < p; i++) {
                    auto l = a[i + offset];
                    auto r = a[i + offset + p] * rot;
                    a[i + offset] = l + r;
                    a[i + offset + p] = l - r;
                }
                if (s + 1 != (1 << len))
                    rot *= info.rate2[countr_zero(~(unsigned int)(s))];
            }
            len++;
        } else {
            // 4-base
            int p = 1 << (h - len - 2);
            mint rot = 1, imag = info.root[2];
            for (int s = 0; s < (1 << len); s++) {
                mint rot2 = rot * rot;
                mint rot3 = rot2 * rot;
                int offset = s << (h - len);
                for (int i = 0; i < p; i++) {
                    auto mod2 = 1ULL * mint::mod() * mint::mod();
                    auto a0 = 1ULL * a[i + offset].val();
                    auto a1 = 1ULL * a[i + offset + p].val() * rot.val();
                    auto a2 = 1ULL * a[i + offset + 2 * p].val() * rot2.val();
                    auto a3 = 1ULL * a[i + offset + 3 * p].val() * rot3.val();
                    auto a1na3imag =
                        1ULL * mint(a1 + mod2 - a3).val() * imag.val();
                    auto na2 = mod2 - a2;
                    a[i + offset] = a0 + a2 + a1 + a3;
                    a[i + offset + 1 * p] = a0 + a2 + (2 * mod2 - (a1 + a3));
                    a[i + offset + 2 * p] = a0 + na2 + a1na3imag;
                    a[i + offset + 3 * p] = a0 + na2 + (mod2 - a1na3imag);
                }
                if (s + 1 != (1 << len))
                    rot *= info.rate3[countr_zero(~(unsigned int)(s))];
            }
            len += 2;
        }
    }
}

template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly_inv(std::vector<mint>& a) {
    int n = int(a.size());
    int h = internal::countr_zero((unsigned int)n);

    static const fft_info<mint> info;

    int len = h;  // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed
    while (len) {
        if (len == 1) {
            int p = 1 << (h - len);
            mint irot = 1;
            for (int s = 0; s < (1 << (len - 1)); s++) {
                int offset = s << (h - len + 1);
                for (int i = 0; i < p; i++) {
                    auto l = a[i + offset];
                    auto r = a[i + offset + p];
                    a[i + offset] = l + r;
                    a[i + offset + p] =
                        (unsigned long long)(mint::mod() + l.val() - r.val()) *
                        irot.val();
                    ;
                }
                if (s + 1 != (1 << (len - 1)))
                    irot *= info.irate2[countr_zero(~(unsigned int)(s))];
            }
            len--;
        } else {
            // 4-base
            int p = 1 << (h - len);
            mint irot = 1, iimag = info.iroot[2];
            for (int s = 0; s < (1 << (len - 2)); s++) {
                mint irot2 = irot * irot;
                mint irot3 = irot2 * irot;
                int offset = s << (h - len + 2);
                for (int i = 0; i < p; i++) {
                    auto a0 = 1ULL * a[i + offset + 0 * p].val();
                    auto a1 = 1ULL * a[i + offset + 1 * p].val();
                    auto a2 = 1ULL * a[i + offset + 2 * p].val();
                    auto a3 = 1ULL * a[i + offset + 3 * p].val();

                    auto a2na3iimag =
                        1ULL *
                        mint((mint::mod() + a2 - a3) * iimag.val()).val();

                    a[i + offset] = a0 + a1 + a2 + a3;
                    a[i + offset + 1 * p] =
                        (a0 + (mint::mod() - a1) + a2na3iimag) * irot.val();
                    a[i + offset + 2 * p] =
                        (a0 + a1 + (mint::mod() - a2) + (mint::mod() - a3)) *
                        irot2.val();
                    a[i + offset + 3 * p] =
                        (a0 + (mint::mod() - a1) + (mint::mod() - a2na3iimag)) *
                        irot3.val();
                }
                if (s + 1 != (1 << (len - 2)))
                    irot *= info.irate3[countr_zero(~(unsigned int)(s))];
            }
            len -= 2;
        }
    }
}

template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution_naive(const std::vector<mint>& a,
                                    const std::vector<mint>& b) {
    int n = int(a.size()), m = int(b.size());
    std::vector<mint> ans(n + m - 1);
    if (n < m) {
        for (int j = 0; j < m; j++) {
            for (int i = 0; i < n; i++) {
                ans[i + j] += a[i] * b[j];
            }
        }
    } else {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                ans[i + j] += a[i] * b[j];
            }
        }
    }
    return ans;
}

template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution_fft(std::vector<mint> a, std::vector<mint> b) {
    int n = int(a.size()), m = int(b.size());
    int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
    a.resize(z);
    internal::butterfly(a);
    b.resize(z);
    internal::butterfly(b);
    for (int i = 0; i < z; i++) {
        a[i] *= b[i];
    }
    internal::butterfly_inv(a);
    a.resize(n + m - 1);
    mint iz = mint(z).inv();
    for (int i = 0; i < n + m - 1; i++) a[i] *= iz;
    return a;
}

}  // namespace internal

template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution(std::vector<mint>&& a, std::vector<mint>&& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};

    int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
    assert((mint::mod() - 1) % z == 0);

    if (std::min(n, m) <= 60) return convolution_naive(a, b);
    return internal::convolution_fft(a, b);
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution(const std::vector<mint>& a,
                              const std::vector<mint>& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};

    int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
    assert((mint::mod() - 1) % z == 0);

    if (std::min(n, m) <= 60) return convolution_naive(a, b);
    return internal::convolution_fft(a, b);
}

template <unsigned int mod = 998244353,
          class T,
          std::enable_if_t<internal::is_integral<T>::value>* = nullptr>
std::vector<T> convolution(const std::vector<T>& a, const std::vector<T>& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};

    using mint = static_modint<mod>;

    int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
    assert((mint::mod() - 1) % z == 0);

    std::vector<mint> a2(n), b2(m);
    for (int i = 0; i < n; i++) {
        a2[i] = mint(a[i]);
    }
    for (int i = 0; i < m; i++) {
        b2[i] = mint(b[i]);
    }
    auto c2 = convolution(std::move(a2), std::move(b2));
    std::vector<T> c(n + m - 1);
    for (int i = 0; i < n + m - 1; i++) {
        c[i] = c2[i].val();
    }
    return c;
}

std::vector<long long> convolution_ll(const std::vector<long long>& a,
                                      const std::vector<long long>& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};

    static constexpr unsigned long long MOD1 = 754974721;  // 2^24
    static constexpr unsigned long long MOD2 = 167772161;  // 2^25
    static constexpr unsigned long long MOD3 = 469762049;  // 2^26
    static constexpr unsigned long long M2M3 = MOD2 * MOD3;
    static constexpr unsigned long long M1M3 = MOD1 * MOD3;
    static constexpr unsigned long long M1M2 = MOD1 * MOD2;
    static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3;

    static constexpr unsigned long long i1 =
        internal::inv_gcd(MOD2 * MOD3, MOD1).second;
    static constexpr unsigned long long i2 =
        internal::inv_gcd(MOD1 * MOD3, MOD2).second;
    static constexpr unsigned long long i3 =
        internal::inv_gcd(MOD1 * MOD2, MOD3).second;
        
    static constexpr int MAX_AB_BIT = 24;
    static_assert(MOD1 % (1ull << MAX_AB_BIT) == 1, "MOD1 isn't enough to support an array length of 2^24.");
    static_assert(MOD2 % (1ull << MAX_AB_BIT) == 1, "MOD2 isn't enough to support an array length of 2^24.");
    static_assert(MOD3 % (1ull << MAX_AB_BIT) == 1, "MOD3 isn't enough to support an array length of 2^24.");
    assert(n + m - 1 <= (1 << MAX_AB_BIT));

    auto c1 = convolution<MOD1>(a, b);
    auto c2 = convolution<MOD2>(a, b);
    auto c3 = convolution<MOD3>(a, b);

    std::vector<long long> c(n + m - 1);
    for (int i = 0; i < n + m - 1; i++) {
        unsigned long long x = 0;
        x += (c1[i] * i1) % MOD1 * M2M3;
        x += (c2[i] * i2) % MOD2 * M1M3;
        x += (c3[i] * i3) % MOD3 * M1M2;
        // B = 2^63, -B <= x, r(real value) < B
        // (x, x - M, x - 2M, or x - 3M) = r (mod 2B)
        // r = c1[i] (mod MOD1)
        // focus on MOD1
        // r = x, x - M', x - 2M', x - 3M' (M' = M % 2^64) (mod 2B)
        // r = x,
        //     x - M' + (0 or 2B),
        //     x - 2M' + (0, 2B or 4B),
        //     x - 3M' + (0, 2B, 4B or 6B) (without mod!)
        // (r - x) = 0, (0)
        //           - M' + (0 or 2B), (1)
        //           -2M' + (0 or 2B or 4B), (2)
        //           -3M' + (0 or 2B or 4B or 6B) (3) (mod MOD1)
        // we checked that
        //   ((1) mod MOD1) mod 5 = 2
        //   ((2) mod MOD1) mod 5 = 3
        //   ((3) mod MOD1) mod 5 = 4
        long long diff =
            c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1));
        if (diff < 0) diff += MOD1;
        static constexpr unsigned long long offset[5] = {
            0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3};
        x -= offset[diff % 5];
        c[i] = x;
    }

    return c;
}

}  // namespace atcoder


#line 1 "tools/garner3.hpp"



#line 7 "tools/garner3.hpp"

namespace tools {

  template <typename M, typename M1, typename M2, typename M3>
  M garner3(const M1& a, const M2& b, const M3& c, const M m) {
    using ull = unsigned long long;
    static const M2 m1_inv_mod_m2 = M2::raw(M1::mod()).inv();
    static const M3 m1_m2_inv_mod_m3 = (M3::raw(M1::mod()) * M3::raw(M2::mod())).inv();

    static const auto plus_mod = [](ull x, const ull y, const ull mod) {
      assert(x < mod);
      assert(y < mod);

      x += y;
      if (x >= mod) x -= mod;
      return x; 
    };

    assert(m >= 1);
    assert(M1::mod() < M2::mod());
    assert(M2::mod() < M3::mod());
    assert(::tools::is_prime(M1::mod()));
    assert(::tools::is_prime(M2::mod()));
    assert(::tools::is_prime(M3::mod()));

    // t1 = (b - a) / M1; (mod M2)
    // t2 = (c - a - t1 * M1) / M1 / M2; (mod M3)
    // return a + t1 * M1 + t2 * M1 * M2; (mod m)
    const M2 t1 = (b - M2::raw(a.val())) * m1_inv_mod_m2;
    const M3 t2 = (c - M3::raw(a.val()) - M3::raw(t1.val()) * M3::raw(M1::mod())) * m1_m2_inv_mod_m3;
    ull r = ::tools::prod_mod(t2.val(), ull(M1::mod()) * ull(M2::mod()), m);
    assert(r < ull(m));
    r = plus_mod(r, ull(t1.val()) * ull(M1::mod()) % m, m);
    assert(r < ull(m));
    r = plus_mod(r, a.val() % m, m);
    assert(r < ull(m));
    return r;
  }
}


#line 22 "tools/convolution.hpp"

namespace tools {
  namespace detail {
    namespace convolution {
      template <typename T, typename = void>
      struct make_complex {
        using type = T;
      };

      template <typename T>
      struct make_complex<T, ::std::enable_if_t<::std::is_floating_point_v<T>, void>> {
        using type = ::std::complex<T>;
      };

      template <typename T>
      using make_complex_t = typename ::tools::detail::convolution::make_complex<T>::type;

      template <typename AG, typename MM, typename InputIterator1, typename InputIterator2, typename OutputIterator>
      void naive(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
        static_assert(::std::is_same_v<typename AG::T, typename MM::T>);
        assert(a_begin != a_end);
        assert(b_begin != b_end);

        using T = typename AG::T;

        const auto n = ::std::distance(a_begin, a_end);
        const auto m = ::std::distance(b_begin, b_end);

        ::std::vector<T> c(n + m - 1, AG::e());
        if (n < m) {
          auto c_begin = c.begin();
          for (auto b_it = b_begin; b_it != b_end; ++b_it, ++c_begin) {
            auto c_it = c_begin;
            for (auto a_it = a_begin; a_it != a_end; ++a_it, ++c_it) {
              *c_it = AG::op(*c_it, MM::op(*a_it, *b_it));
            }
          }
        } else {
          auto c_begin = c.begin();
          for (auto a_it = a_begin; a_it != a_end; ++a_it, ++c_begin) {
            auto c_it = c_begin;
            for (auto b_it = b_begin; b_it != b_end; ++b_it, ++c_it) {
              *c_it = AG::op(*c_it, MM::op(*a_it, *b_it));
            }
          }
        }

        ::std::move(c.begin(), c.end(), result);
      }

      template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
      void fft(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
        using T = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
        static_assert(::std::is_same_v<T, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
        using C = ::tools::detail::convolution::make_complex_t<T>;
        static_assert(::std::is_same_v<C, ::std::complex<float>> || ::std::is_same_v<C, ::std::complex<double>> || ::std::is_same_v<C, ::std::complex<long double>>);
        using R = typename C::value_type;

        assert(a_begin != a_end);
        assert(b_begin != b_end);

        ::std::vector<C> a, b;
        if constexpr (::std::is_same_v<T, R>) {
          for (auto it = a_begin; it != a_end; ++it) {
            a.emplace_back(*it, 0);
          }
          for (auto it = b_begin; it != b_end; ++it) {
            b.emplace_back(*it, 0);
          }
        } else if constexpr (::std::is_same_v<T, C>) {
          a.assign(a_begin, a_end);
          b.assign(b_begin, b_end);
        }
        const auto n = a.size() + b.size() - 1;
        const auto z = ::tools::pow2(::tools::ceil_log2(n));
        a.resize(z);
        b.resize(z);

        ::std::vector<C> pow_root;
        pow_root.reserve(z);
        pow_root.emplace_back(1, 0);
        if (z > 1) pow_root.push_back(::std::polar<R>(1, R(2) * ::std::acos(R(-1)) / z));
        for (::std::size_t p = 2; p < z; p *= 2) {
          pow_root.push_back(pow_root[p / 2] * pow_root[p / 2]);
          for (::std::size_t i = p + 1; i < p * 2; ++i) {
            pow_root.push_back(pow_root[p] * pow_root[i - p]);
          }
        }

        const auto butterfly = [&](::std::vector<C>& f) {
          ::std::vector<C> prev(z);
          for (::std::size_t p = z / 2; p >= 1; p /= 2) {
            prev.swap(f);
            for (::std::size_t qp = 0; qp < z; qp += p) {
              for (::std::size_t r = 0; r < p; ++r) {
                f[qp + r] = prev[qp * 2 % z + r] + pow_root[qp] * prev[qp * 2 % z + p + r];
              }
            }
          }
        };

        butterfly(a);
        butterfly(b);

        for (::std::size_t i = 0; i < z; ++i) {
          a[i] *= b[i];
        }

        ::std::reverse(::std::next(pow_root.begin()), pow_root.end());
        butterfly(a);

        for (::std::size_t i = 0; i < n; ++i) {
          if constexpr (::std::is_same_v<T, R>) {
            *result = a[i].real() / z;
          } else {
            *result = a[i] / z;
          }
          ++result;
        }
      }

      template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
      void ntt(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
        using M = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
        static_assert(::std::is_same_v<M, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);

        static_assert(::atcoder::internal::is_static_modint<M>::value);
        static_assert(2 <= M::mod() && M::mod() <= 2000000000);
        static_assert(::tools::is_prime(M::mod()));
        assert(a_begin != a_end);
        assert(b_begin != b_end);

        ::std::vector<M> a(a_begin, a_end);
        ::std::vector<M> b(b_begin, b_end);
        const auto n = a.size();
        const auto m = b.size();
        const auto z = ::tools::pow2(::tools::ceil_log2(n + m - 1));
        assert((M::mod() - 1) % z == 0);

        if (n == m && 4 * n == z + 4) {

          const auto afbf = a.front() * b.front();
          const auto abbb = a.back() * b.back();

          a.resize(z / 2);
          ::atcoder::internal::butterfly(a);

          b.resize(z / 2);
          ::atcoder::internal::butterfly(b);

          for (::std::size_t i = 0; i < z / 2; ++i) {
            a[i] *= b[i];
          }

          ::atcoder::internal::butterfly_inv(a);
          const auto iz = M(z / 2).inv();

          *result = afbf;
          ++result;
          for (::std::size_t i = 1; i < n + m - 2; ++i) {
            *result = a[i] * iz;
            ++result;
          }
          *result = abbb;
          ++result;

        } else {

          a.resize(z);
          ::atcoder::internal::butterfly(a);

          b.resize(z);
          ::atcoder::internal::butterfly(b);

          for (::std::size_t i = 0; i < z; ++i) {
            a[i] *= b[i];
          }

          ::atcoder::internal::butterfly_inv(a);
          const auto iz = M(z).inv();

          for (::std::size_t i = 0; i < n + m - 1; ++i) {
            *result = a[i] * iz;
            ++result;
          }

        }
      }

      template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
      void ntt_and_garner(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
        using M = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
        static_assert(::std::is_same_v<M, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
        using M1 = ::atcoder::static_modint<1107296257>; // 33 * 2^25 + 1
        using M2 = ::atcoder::static_modint<1711276033>; // 51 * 2^25 + 1
        using M3 = ::atcoder::static_modint<1811939329>; // 27 * 2^26 + 1

        static_assert(::atcoder::internal::is_static_modint<M>::value || ::atcoder::internal::is_dynamic_modint<M>::value);
        assert(a_begin != a_end);
        assert(b_begin != b_end);

        const auto n = ::std::distance(a_begin, a_end);
        const auto m = ::std::distance(b_begin, b_end);
        const auto z = ::tools::pow2(::tools::ceil_log2(n + m - 1));

        assert((M1::mod() - 1) % z == 0);
        assert((M2::mod() - 1) % z == 0);
        assert((M3::mod() - 1) % z == 0);

        // No need for the following assertion because the condition always holds.
        // assert(std::min(a.size(), b.size()) * tools::square(M::mod() - 1) < M1::mod() * M2::mod() * M3::mod());

        ::std::vector<M1> c1;
        c1.reserve(n + m - 1);
        {
          ::std::vector<M1> a1;
          a1.reserve(n);
          for (auto it = a_begin; it != a_end; ++it) {
            a1.emplace_back(it->val());
          }

          ::std::vector<M1> b1;
          b1.reserve(m);
          for (auto it = b_begin; it != b_end; ++it) {
            b1.emplace_back(it->val());
          }

          ::tools::detail::convolution::ntt(a1.begin(), a1.end(), b1.begin(), b1.end(), ::std::back_inserter(c1));
        }

        ::std::vector<M2> c2;
        c2.reserve(n + m - 1);
        {
          ::std::vector<M2> a2;
          a2.reserve(n);
          for (auto it = a_begin; it != a_end; ++it) {
            a2.emplace_back(it->val());
          }

          ::std::vector<M2> b2;
          b2.reserve(m);
          for (auto it = b_begin; it != b_end; ++it) {
            b2.emplace_back(it->val());
          }

          ::tools::detail::convolution::ntt(a2.begin(), a2.end(), b2.begin(), b2.end(), ::std::back_inserter(c2));
        }

        ::std::vector<M3> c3;
        c3.reserve(n + m - 1);
        {
          ::std::vector<M3> a3;
          a3.reserve(n);
          for (auto it = a_begin; it != a_end; ++it) {
            a3.emplace_back(it->val());
          }

          ::std::vector<M3> b3;
          b3.reserve(m);
          for (auto it = b_begin; it != b_end; ++it) {
            b3.emplace_back(it->val());
          }

          ::tools::detail::convolution::ntt(a3.begin(), a3.end(), b3.begin(), b3.end(), ::std::back_inserter(c3));
        }

        for (::std::size_t i = 0; i < c1.size(); ++i) {
          *result = M::raw(::tools::garner3(c1[i], c2[i], c3[i], M::mod()));
          ++result;
        }
      }

      template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
      void ntt_and_garner_for_ll(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
        using Z = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
        static_assert(::std::is_same_v<Z, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
        using ll = long long;

        static_assert(::std::is_integral_v<Z>);
        assert(a_begin != a_end);
        assert(b_begin != b_end);

        const auto n = ::std::distance(a_begin, a_end);
        const auto m = ::std::distance(b_begin, b_end);
        assert(n + m - 1 <= ::tools::pow2(24));

        ::std::vector<ll> a, b;
        a.reserve(n);
        b.reserve(m);
        ::std::copy(a_begin, a_end, ::std::back_inserter(a));
        ::std::copy(b_begin, b_end, ::std::back_inserter(b));

        for (const auto c_i : ::atcoder::convolution_ll(a, b)) {
          *result = c_i;
          ++result;
        }
      }
    }
  }

  template <typename AG, typename MM, typename InputIterator1, typename InputIterator2, typename OutputIterator>
  void convolution(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
    using T = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
    static_assert(::std::is_same_v<T, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);

    if (a_begin == a_end || b_begin == b_end) return;

    const auto n = ::std::distance(a_begin, a_end);
    const auto m = ::std::distance(b_begin, b_end);
    if (::std::min(n, m) <= 60) {
      ::tools::detail::convolution::naive<AG, MM>(a_begin, a_end, b_begin, b_end, result);
      return;
    }

    if constexpr (::std::is_same_v<AG, ::tools::group::plus<T>> && (::std::is_same_v<MM, ::tools::monoid::multiplies<T>> || ::std::is_same_v<MM, ::tools::group::multiplies<T>>)) {
      if constexpr (::std::is_floating_point_v<T> || ::std::is_same_v<T, ::std::complex<float>> || ::std::is_same_v<T, ::std::complex<double>> || ::std::is_same_v<T, ::std::complex<long double>>) {
        ::tools::detail::convolution::fft(a_begin, a_end, b_begin, b_end, result);
      } else if constexpr (::std::is_integral_v<T>) {
        ::tools::detail::convolution::ntt_and_garner_for_ll(a_begin, a_end, b_begin, b_end, result);
      } else if constexpr (::atcoder::internal::is_static_modint<T>::value || ::atcoder::internal::is_dynamic_modint<T>::value) {
        if constexpr (::atcoder::internal::is_static_modint<T>::value && T::mod() <= 2000000000 && ::tools::is_prime(T::mod())) {
          if ((T::mod() - 1) % ::tools::pow2(::tools::ceil_log2(n + m - 1)) == 0) {
            ::tools::detail::convolution::ntt(a_begin, a_end, b_begin, b_end, result);
          } else {
            ::tools::detail::convolution::ntt_and_garner(a_begin, a_end, b_begin, b_end, result);
          }
        } else {
          ::tools::detail::convolution::ntt_and_garner(a_begin, a_end, b_begin, b_end, result);
        }
      } else {
        ::tools::detail::convolution::naive<AG, MM>(a_begin, a_end, b_begin, b_end, result);
      }
    } else {
      ::tools::detail::convolution::naive<AG, MM>(a_begin, a_end, b_begin, b_end, result);
    }
  }

  template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
  void convolution(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, const OutputIterator result) {
    using T = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
    static_assert(::std::is_same_v<T, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
    ::tools::convolution<::tools::group::plus<T>, ::tools::monoid::multiplies<T>>(a_begin, a_end, b_begin, b_end, result);
  }
}


#line 16 "tools/sample_point_shift.hpp"

namespace tools {

  template <typename RandomAccessIterator>
  ::std::enable_if_t<
    ::std::is_base_of_v<
      ::std::random_access_iterator_tag,
      typename ::std::iterator_traits<RandomAccessIterator>::iterator_category
    >,
    typename ::std::iterator_traits<RandomAccessIterator>::value_type
  > sample_point_shift(const RandomAccessIterator begin, const RandomAccessIterator end, const typename ::std::iterator_traits<RandomAccessIterator>::value_type c) {
    using T = typename ::std::iterator_traits<RandomAccessIterator>::value_type;
    assert(::tools::is_prime(T::mod()));
    const int N = ::std::distance(begin, end);
    assert(1 <= N && N <= T::mod());
    ::tools::fact_mod_cache<T> cache;
    const ::std::array<T, 2> minus_1_pow = {T(1), T(-1)};

    ::tools::online_cumsum<::tools::monoid::multiplies<T>, true> nl(N);
    ::tools::online_cumsum<::tools::monoid::multiplies<T>, false> nr(N);
    {
      T last = c;
      for (int i = 0; i < N; ++i, --last) {
        nl[i] = nr[i] = last;
      }
    }

    T answer(0);
    for (int i = 0; i < N; ++i) {
      answer += nl.prod(0, i) * nr.prod(i + 1, N) * minus_1_pow[(N - 1 - i) & 1] * cache.fact_inv(N - 1 - i) * cache.fact_inv(i) * begin[i];
    }

    return answer;
  }

  template <typename InputIterator>
  ::std::enable_if_t<
    !::std::is_base_of_v<
      ::std::random_access_iterator_tag,
      typename ::std::iterator_traits<InputIterator>::iterator_category
    >,
    typename ::std::iterator_traits<InputIterator>::value_type
  > sample_point_shift(const InputIterator begin, const InputIterator end, const typename ::std::iterator_traits<InputIterator>::value_type c) {
    using T = typename ::std::iterator_traits<InputIterator>::value_type;
    const ::std::vector<T> samples(begin, end);
    return ::tools::sample_point_shift(samples.begin(), samples.end(), c);
  }

  template <typename T>
  T sample_point_shift(const ::std::initializer_list<T> il, const T c) {
    return ::tools::sample_point_shift(il.begin(), il.end(), c);
  }

  template <typename RandomAccessIterator, typename OutputIterator>
  ::std::enable_if_t<
    ::std::is_base_of_v<
      ::std::random_access_iterator_tag,
      typename ::std::iterator_traits<RandomAccessIterator>::iterator_category
    >,
    void
  > sample_point_shift(const RandomAccessIterator begin, const RandomAccessIterator end, const typename ::std::iterator_traits<RandomAccessIterator>::value_type c, const int M, OutputIterator result) {
    using T = typename ::std::iterator_traits<RandomAccessIterator>::value_type;
    assert(::tools::is_prime(T::mod()));
    const int N = ::std::distance(begin, end);
    assert(1 <= N && N <= T::mod());
    assert(0 <= M);
    if (M == 1) {
      result = ::tools::sample_point_shift(begin, end, c);
      ++result;
    }
    if (M <= 1) return;
    ::tools::fact_mod_cache<T> cache;
    const ::std::array<T, 2> minus_1_pow = {T(1), T(-1)};

    ::std::vector<T> c1;
    {
      ::std::vector<T> a1(N);
      for (int i = 0; i < N; ++i) {
        a1[i] = begin[i] * cache.fact_inv(i);
      }

      ::std::vector<T> b1(N);
      for (int i = 0; i < N; ++i) {
        b1[i] = minus_1_pow[i & 1] * cache.fact_inv(i);
      }

      ::tools::convolution(a1.begin(), a1.end(), b1.begin(), b1.end(), ::std::back_inserter(c1));
      c1.resize(N);
    }

    ::std::vector<T> c2;
    {
      ::std::vector<T> a2(N);
      for (int i = 0; i < N; ++i) {
        a2[i] = c1[N - 1 - i] * cache.fact(N - 1 - i);
      }

      ::std::vector<T> b2(N);
      b2[0] = T(1);
      T b = c;
      for (int i = 1; i < N; ++i, --b) {
        b2[i] = b2[i - 1] * b;
      }
      for (int i = 0; i < N; ++i) {
        b2[i] *= cache.fact_inv(i);
      }

      ::tools::convolution(a2.begin(), a2.end(), b2.begin(), b2.end(), ::std::back_inserter(c2));
      c2.resize(N);
      ::std::reverse(c2.begin(), c2.end());
      for (int i = 0; i < N; ++i) {
        c2[i] *= cache.fact_inv(i);
      }
    }

    ::std::vector<T> c3;
    const int m = ::std::min(M, T::mod());
    {
      const auto& a3 = c2;

      ::std::vector<T> b3(m);
      for (int i = 0; i < m; ++i) {
        b3[i] = cache.fact_inv(i);
      }

      ::tools::convolution(a3.begin(), a3.end(), b3.begin(), b3.end(), ::std::back_inserter(c3));
      c3.resize(m);
      for (int i = 0; i < m; ++i) {
        c3[i] *= cache.fact(i);
      }
    }

    c3.resize(M);
    for (int i = m; i < M; ++i) {
      c3[i] = c3[i % T::mod()];
    }

    ::std::copy(c3.begin(), c3.end(), result);
  }

  template <typename InputIterator, typename OutputIterator>
  ::std::enable_if_t<
    !::std::is_base_of_v<
      ::std::random_access_iterator_tag,
      typename ::std::iterator_traits<InputIterator>::iterator_category
    >,
    void
  > sample_point_shift(const InputIterator begin, const InputIterator end, const typename ::std::iterator_traits<InputIterator>::value_type c, const int M, const OutputIterator result) {
    using T = typename ::std::iterator_traits<InputIterator>::value_type;
    const ::std::vector<T> samples(begin, end);
    ::tools::sample_point_shift(samples.begin(), samples.end(), c, M, result);
  }

  template <typename InputIterator>
  ::std::vector<typename ::std::iterator_traits<InputIterator>::value_type>
  sample_point_shift(const InputIterator begin, const InputIterator end, const typename ::std::iterator_traits<InputIterator>::value_type c, const int M) {
    using T = typename ::std::iterator_traits<InputIterator>::value_type;
    ::std::vector<T> res;
    ::tools::sample_point_shift(begin, end, c, M, ::std::back_inserter(res));
    return res;
  }

  template <typename T>
  ::std::vector<T> sample_point_shift(const ::std::initializer_list<T> il, const T c, const int M) {
    ::std::vector<T> res;
    ::tools::sample_point_shift(il.begin(), il.end(), c, M, ::std::back_inserter(res));
    return res;
  }
}


#line 15 "tools/large_fact_mod_cache.hpp"

namespace tools {

  template <class M>
  class large_fact_mod_cache {
    int m_K;
    ::std::vector<M> m_factbs;

  public:
    large_fact_mod_cache() {
      const long long P = M::mod();
      assert(::tools::is_prime(P));
      ::tools::pow_mod_cache<M> pow2(2);

      long long K = 0;
      const long long Q = 200000;
      for (; ::tools::ceil(P * ::tools::ceil_log2(P), ::tools::pow2(K)) + Q * ::tools::pow2(K) > ::tools::ceil(P * ::tools::ceil_log2(P), ::tools::pow2(K + 1)) + Q * ::tools::pow2(K + 1); ++K);
      this->m_K = K;

      ::std::vector<M> f_im1{M(1)};
      ::std::vector<M> f_i;
      for (int i = 1; i <= K; ++i, f_i.swap(f_im1)) {
        ::tools::sample_point_shift(f_im1.begin(), f_im1.end(), pow2[i - 1], 3 * ::tools::pow2(i - 1), ::std::back_inserter(f_im1));
        f_i.resize(::tools::pow2(i));
        for (int j = 0; j < ::tools::pow2(i); ++j) {
          f_i[j] = f_im1[2 * j] * f_im1[2 * j + 1] * pow2[i - 1] * (M(2) * M(j) + M(1));
        }
      }

      this->m_factbs = ::std::move(f_im1);
      if (::tools::pow2(K) <= P / ::tools::pow2(K)) {
        ::tools::sample_point_shift(this->m_factbs.begin(), this->m_factbs.end(), pow2[K], P / ::tools::pow2(K) + 1 - ::tools::pow2(K), ::std::back_inserter(this->m_factbs));
      }
      this->m_factbs.insert(this->m_factbs.begin(), M(1));
      for (int i = 1; i < ::std::ssize(this->m_factbs); ++i) {
        this->m_factbs[i] *= this->m_factbs[i - 1] * pow2[K] * M(i);
      }
    }

    M fact(const long long nll) const {
      assert(nll >= 0);
      if (nll >= M::mod()) return M::raw(0);

      const auto n = static_cast<int>(nll);
      const auto prev = (n >> this->m_K) << this->m_K;
      const auto next = ((n >> this->m_K) + 1) << this->m_K;
      if (n - prev <= ::std::min(next, M::mod() - 1) - n) {
        auto res = this->m_factbs[prev >> this->m_K];
        int i;
        for (M m(i = prev + 1); i <= n; ++i, ++m) {
          res *= m;
        }
        return res;
      } else {
        if (next <= M::mod() - 1) {
          auto res = this->m_factbs[next >> this->m_K].inv();
          int i;
          for (M m(i = n + 1); i <= next; ++i, ++m) {
            res *= m;
          }
          return res.inv();
        } else {
          M res(-1);
          int i;
          for (M m(i = n + 1); i < M::mod(); ++i, ++m) {
            res *= m;
          }
          return res.inv();
        }
      }
    }

    M binomial(long long n, long long r) const {
      if (r < 0) return M::raw(0);
      if (0 <= n && n < r) return M::raw(0);
      if (n < 0) return M(1 - ((r & 1) << 1)) * this->binomial(-n + r - 1, r);

      const auto c = [&](const long long nn, const long long rr) {
        return 0 <= rr && rr <= nn ? this->fact(nn) / (this->fact(nn - rr) * this->fact(rr)) : M::raw(0);
      };

      M result(1);
      while (n > 0 || r > 0) {
        result *= c(n % M::mod(), r % M::mod());
        n /= M::mod();
        r /= M::mod();
      }

      return result;
    }
    M combination(const long long n, const long long r) const {
      if (!(0 <= r && r <= n)) return M::raw(0);
      return this->binomial(n, r);
    }
    M permutation(const long long n, const long long r) const {
      if (!(0 <= r && r <= n)) return M::raw(0);
      return this->binomial(n, r) * this->fact(r);
    }
    M combination_with_repetition(const long long n, const long long r) const {
      if (n < 0 || r < 0) return M::raw(0);
      return this->binomial(n + r - 1, r);
    }
  };
}


#line 1 "tools/extended_lucas.hpp"



#line 1 "tools/int128_t.hpp"



#line 5 "tools/int128_t.hpp"


#line 1 "tools/prime_factorization.hpp"



#line 6 "tools/prime_factorization.hpp"
#include <queue>
#line 1 "tools/floor_log2.hpp"



#line 6 "tools/floor_log2.hpp"

namespace tools {
  template <typename T>
  constexpr T floor_log2(T x) noexcept {
    assert(x > 0);
    return ::tools::bit_width(x) - 1;
  }
}


#line 15 "tools/prime_factorization.hpp"

namespace tools {

  template <typename T>
  ::std::vector<T> prime_factorization(T n) {
    assert(1 <= n && n <= 1000000000000000000);
    ::std::vector<T> result;

    if (n == 1) return result;

    ::std::queue<::std::pair<T, T>> factors({::std::pair<T, T>(n, 1)});
    while (!factors.empty()) {
      const T factor = factors.front().first;
      const T occurrences = factors.front().second;
      factors.pop();
      if (::tools::is_prime(factor)) {
        for (T i = 0; i < occurrences; ++i) {
          result.push_back(factor);
        }
      } else {
        const T m = ::tools::pow2((::tools::floor_log2(factor) + 1) / 8);
        for (T c = 1; ; ++c) {
          const auto f = [&](T& x) {
            x = ::tools::prod_mod(x, x, factor);
            x += c;
            if (x >= factor) x -= factor;
          };
          T y = 2;
          T r = 1;
          T q = 1;
          T x, g, ys;
          do {
            x = y;
            for (T i = 0; i < r; ++i) {
              f(y);
            }
            T k = 0;
            do {
              ys = y;
              for (T i = 0; i < ::std::min(m, r - k); ++i) {
                f(y);
                q = ::tools::prod_mod(q, ::std::abs(x - y), factor);
              }
              g = ::std::gcd(q, factor);
              k += m;
            } while (k < r && g == 1);
            r *= 2;
          } while (g == 1);
          if (g == factor) {
            do {
              f(ys);
              g = ::std::gcd(::std::abs(x - ys), factor);
            } while (g == 1);
          }
          if (g < factor) {
            T h = factor / g;
            if (h < g) ::std::swap(g, h);
            T n = 1;
            while (h % g == 0) {
              h /= g;
              ++n;
            }
            factors.emplace(g, occurrences * n);
            if (h > 1) factors.emplace(h, occurrences);
            break;
          }
        }
      }
    }

    ::std::sort(result.begin(), result.end());
    return result;
  }
}


#line 1 "tools/run_length.hpp"



#line 8 "tools/run_length.hpp"

namespace tools {
  template <typename InputIterator, typename OutputIterator>
  void run_length(const InputIterator& begin, const InputIterator& end, OutputIterator result) {
    using T = typename ::std::iterator_traits<InputIterator>::value_type;
    if (begin == end) return;

    ::std::pair<T, ::std::size_t> prev;
    for (auto [it, breaks] = ::std::make_pair(begin, false); !breaks; breaks = it == end, it = ::std::next(it, breaks ? 0 : 1)) {
      bool flg1, flg2;
      if (it == begin) {
        flg1 = false;
        flg2 = true;
      } else if (it == end) {
        flg1 = true;
        flg2 = false;
      } else if (*it != prev.first) {
        flg1 = true;
        flg2 = true;
      } else {
        flg1 = false;
        flg2 = false;
      }
      if (flg1 || flg2) {
        if (flg1) {
          *result = prev;
          ++result;
        }
        if (flg2) {
          prev.first = *it;
          prev.second = 1;
        }
      } else {
        ++prev.second;
      }
    }
  }
}


#line 1 "tools/garner.hpp"



#line 1 "tools/inv_mod.hpp"



#line 1 "tools/extgcd.hpp"



#include <tuple>
#line 9 "tools/extgcd.hpp"

namespace tools {

  template <typename T>
  ::std::tuple<T, T, T> extgcd(T prev_r, T r) {
    const bool prev_r_is_neg = prev_r < T(0);
    const bool r_is_neg = r < T(0);

    if (prev_r_is_neg) prev_r = -prev_r;
    if (r_is_neg) r = -r;

    #ifndef NDEBUG
    const T a = prev_r;
    const T b = r;
    #endif

    T prev_s(1);
    T prev_t(0);
    T s(0);
    T t(1);
    while (r != T(0)) {
      const T q = prev_r / r;
      ::std::tie(prev_r, r) = ::std::make_pair(r, prev_r - q * r);
      ::std::tie(prev_s, s) = ::std::make_pair(s, prev_s - q * s);
      ::std::tie(prev_t, t) = ::std::make_pair(t, prev_t - q * t);
    }

    if (prev_r_is_neg) prev_s = -prev_s;
    if (r_is_neg) prev_t = -prev_t;

    assert(::tools::abs(prev_s) <= ::std::max(b / prev_r / T(2), T(1)));
    assert(::tools::abs(prev_t) <= ::std::max(a / prev_r / T(2), T(1)));
    return ::std::make_tuple(prev_s, prev_t, prev_r);
  }
}


#line 7 "tools/inv_mod.hpp"

namespace tools {

  template <typename T1, typename T2>
  constexpr T2 inv_mod(const T1 x, const T2 m) {
    const auto [x0, y0, gcd] = ::tools::extgcd(x, m);
    assert(gcd == 1);
    return ::tools::mod(x0, m);
  }
}


#line 9 "tools/garner.hpp"

// Source: https://qiita.com/drken/items/ae02240cd1f8edfc86fd
// License: unknown
// Author: drken

namespace tools {

  template <typename Iterator, typename ModType>
  ::std::pair<long long, long long> garner(const Iterator& begin, const Iterator& end, const ModType& mod) {
    ::std::vector<long long> b, m;
    for (auto it = begin; it != end; ++it) {
      b.push_back(::tools::mod(it->first, it->second));
      m.push_back(it->second);
    }

    auto lcm = 1LL;
    for (::std::size_t i = 0; i < b.size(); ++i) {
      (lcm *= m[i]) %= mod;
    }

    m.push_back(mod);
    ::std::vector<long long> coeffs(m.size(), 1);
    ::std::vector<long long> constants(m.size(), 0);
    for (::std::size_t k = 0; k < b.size(); ++k) {
      long long t = ::tools::mod((b[k] - constants[k]) * ::tools::inv_mod(coeffs[k], m[k]), m[k]);
      for (::std::size_t i = k + 1; i < m.size(); ++i) {
        (constants[i] += t * coeffs[i]) %= m[i];
        (coeffs[i] *= m[k]) %= m[i];
      }
    }

    return ::std::make_pair(constants.back(), lcm);
  }

  template <typename M, typename Iterator>
  ::std::pair<M, M> garner(const Iterator& begin, const Iterator& end) {
    const auto [y, z] = ::tools::garner(begin, end, M::mod());
    return ::std::make_pair(M::raw(y), M::raw(z));
  }
}


#line 12 "tools/extended_lucas.hpp"

// Source: https://hitonanode.github.io/cplib-cpp/number/combination.hpp.html
// License: MIT
// Author: hitonanode

// MIT License
// 
// Copyright (c) 2019 Ryotaro Sato
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

namespace tools {

  template <class M>
  class extended_lucas {
    struct combination_prime_pow {
      int p, q, m;
      ::std::vector<int> fac, invfac, ppow;

      long long ej(long long n) const {
        long long ret = 0;
        while (n) ret += n, n /= this->p;
        return ret;
      }

      combination_prime_pow(const int p_, const int q_) : p(p_), q(q_), m(1), ppow{1} {
        for (int t = 0; t < this->q; ++t) this->m *= this->p, this->ppow.push_back(this->m);
        this->fac.assign(this->m, 1);
        this->invfac.assign(this->m, 1);
        for (int i = 1; i < this->m; ++i) this->fac[i] = static_cast<long long>(this->fac[i - 1]) * (i % this->p ? i : 1) % this->m;
        this->invfac[this->m - 1] = this->fac[this->m - 1]; // Same as Wilson's theorem
        assert(1LL * this->fac.back() * this->invfac.back() % this->m == 1);
        for (int i = this->m - 1; i; --i) this->invfac[i - 1] = static_cast<long long>(this->invfac[i]) * (i % this->p ? i : 1) % this->m;
      }

      int fact(const long long n) const {
        assert(n >= 0);
        const auto q0 = this->ej(n / this->p);
        return q0 < this->q ? static_cast<long long>(this->fac[n]) * this->ppow[q0] % this->m : 0;
      }

      int combination(long long n, long long r) const {
        assert(0 <= r && r <= n);
        if (this->p == 2 && this->q == 1) return !((~n) & r); // Lucas
        long long k = n - r;
        const long long e0 = this->ej(n / this->p) - this->ej(r / this->p) - this->ej(k / this->p);
        if (e0 >= q) return 0;

        long long ret = this->ppow[e0];
        if (this->q == 1) { // Lucas
          while (n) {
            ret = ::tools::int128_t(ret) * this->fac[n % this->p] * this->invfac[r % this->p] * this->invfac[k % this->p] % this->p;
            n /= this->p, r /= this->p, k /= this->p;
          }
          return static_cast<int>(ret);
        } else {
          if ((p > 2 || q < 3) && (this->ej(n / this->m) - this->ej(r / this->m) - this->ej(k / this->m)) & 1) ret = this->m - ret;
          while (n) {
            ret = ::tools::int128_t(ret) * this->fac[n % this->m] * this->invfac[r % this->m] * this->invfac[k % this->m] % this->m;
            n /= this->p, r /= this->p, k /= this->p;
          }
          return static_cast<int>(ret);
        }
      }
    };

    ::std::vector<combination_prime_pow> m_cpps;

  public:
    extended_lucas() {
      const auto prime_factors = ::tools::prime_factorization(M::mod());
      ::std::vector<::std::pair<int, int>> distinct_prime_factors;
      ::tools::run_length(prime_factors.begin(), prime_factors.end(), ::std::back_inserter(distinct_prime_factors));
      for (const auto& [p, q] : distinct_prime_factors) {
        this->m_cpps.emplace_back(p, q);
      }
    }

    M fact(const long long n) const {
      assert(n >= 0);
      ::std::vector<::std::pair<int, int>> rs;
      for (const auto& cpp : this->m_cpps) rs.emplace_back(cpp.fact(n), cpp.m);
      return ::tools::garner<M>(rs.begin(), rs.end()).first;
    }
    M binomial(const long long n, const long long r) const {
      if (r < 0) return M::raw(0);
      if (0 <= n && n < r) return M::raw(0);
      if (n < 0) return M((r & 1) ? -1 : 1) * this->binomial(-n + r - 1, r);

      ::std::vector<::std::pair<int, int>> rs;
      for (const auto& cpp : this->m_cpps) rs.emplace_back(cpp.combination(n, r), cpp.m);
      return ::tools::garner<M>(rs.begin(), rs.end()).first;
    }
    M combination(const long long n, const long long r) const {
      if (!(0 <= r && r <= n)) return M::raw(0);
      return this->binomial(n, r);
    }
    M permutation(const long long n, const long long r) const {
      if (!(0 <= r && r <= n)) return M::raw(0);
      return this->binomial(n, r) * this->fact(r);
    }
    M combination_with_repetition(const long long n, const long long r) const {
      if (n < 0 || r < 0) return M::raw(0);
      return this->binomial(n + r - 1, r);
    }
  };
}


#line 1 "tools/bell.hpp"



#line 1 "tools/stirling_2nd.hpp"



#line 1 "tools/fps.hpp"



#line 1 "tools/less_by_first.hpp"



#line 5 "tools/less_by_first.hpp"

namespace tools {

  class less_by_first {
  public:
    template <class T1, class T2>
    bool operator()(const ::std::pair<T1, T2>& x, const ::std::pair<T1, T2>& y) const {
      return x.first < y.first;
    }
  };
}


#line 19 "tools/fps.hpp"

// Source: https://opt-cp.com/fps-implementation/
// License: CC0
// Author: opt

namespace tools {
  template <typename M>
  class fps {
  private:
    using F = ::tools::fps<M>;
    ::std::vector<M> m_vector;

    // maximum 2^k s.t. x = 1 (mod 2^k)
    static constexpr int pow2_k(const unsigned int x) {
      return (x - 1) & -(x - 1);
    }

    // d <= lpf(M)
    static bool is_leq_lpf_of_M(const int d) {
      if (M::mod() == 1) return true;
      for (int i = 2; i < d; ++i) {
        if (M::mod() % i == 0) return false;
      }
      return true;
    }

  public:
    using reference = M&;
    using const_reference = const M&;
    using iterator = typename ::std::vector<M>::iterator;
    using const_iterator = typename ::std::vector<M>::const_iterator;
    using size_type = ::std::size_t;
    using difference_type = ::std::ptrdiff_t;
    using value_type = M;
    using allocator_type = typename ::std::vector<M>::allocator_type;
    using pointer = M*;
    using const_pointer = const M*;
    using reverse_iterator = typename ::std::vector<M>::reverse_iterator;
    using const_reverse_iterator = typename ::std::vector<M>::const_reverse_iterator;

    fps() = default;
    fps(const F&) = default;
    fps(F&&) = default;
    ~fps() = default;
    F& operator=(const F&) = default;
    F& operator=(F&&) = default;

    explicit fps(const size_type n) : m_vector(n) {}
    fps(const size_type n, const_reference value) : m_vector(n, value) {}
    template <class InputIter> fps(const InputIter first, const InputIter last) : m_vector(first, last) {}
    fps(const ::std::initializer_list<M> il) : m_vector(il) {}

    iterator begin() noexcept { return this->m_vector.begin(); }
    const_iterator begin() const noexcept { return this->m_vector.begin(); }
    iterator end() noexcept { return this->m_vector.end(); }
    const_iterator end() const noexcept { return this->m_vector.end(); }
    const_iterator cbegin() const noexcept { return this->m_vector.cbegin(); }
    const_iterator cend() const noexcept { return this->m_vector.cend(); }
    reverse_iterator rbegin() noexcept { return this->m_vector.rbegin(); }
    const_reverse_iterator rbegin() const noexcept { return this->m_vector.rbegin(); }
    const_reverse_iterator crbegin() const noexcept { return this->m_vector.crbegin(); }
    reverse_iterator rend() noexcept { return this->m_vector.rend(); }
    const_reverse_iterator rend() const noexcept { return this->m_vector.rend(); }
    const_reverse_iterator crend() const noexcept { return this->m_vector.crend(); }

    size_type size() const noexcept { return this->m_vector.size(); }
    size_type max_size() const noexcept { return this->m_vector.max_size(); }
    void resize(const size_type sz) { this->m_vector.resize(sz); }
    void resize(const size_type sz, const M& c) { this->m_vector.resize(sz, c); }
    size_type capacity() const noexcept { return this->m_vector.capacity(); }
    bool empty() const noexcept { return this->m_vector.empty(); }
    void reserve(const size_type n) { this->m_vector.reserve(n); }
    void shrink_to_fit() { this->m_vector.shrink_to_fit(); }

    reference operator[](const size_type n) { return this->m_vector[n]; }
    const_reference operator[](const size_type n) const { return this->m_vector[n]; }
    reference at(const size_type n) { return this->m_vector.at(n); }
    const_reference at(const size_type n) const { return this->m_vector.at(n); }
    pointer data() noexcept { return this->m_vector.data(); }
    const_pointer data() const noexcept { return this->m_vector.data(); }
    reference front() { return this->m_vector.front(); }
    const_reference front() const { return this->m_vector.front(); }
    reference back() { return this->m_vector.back(); }
    const_reference back() const { return this->m_vector.back(); }

    template <class InputIterator> void assign(const InputIterator first, const InputIterator last) { this->m_vector.assign(first, last); }
    void assign(const size_type n, const M& u) { this->m_vector.assign(n, u); }
    void assign(const ::std::initializer_list<M> il) { this->m_vector.assign(il); }
    void push_back(const M& x) { this->m_vector.push_back(x); }
    void push_back(M&& x) { this->m_vector.push_back(::std::forward<M>(x)); }
    template <class... Args> reference emplace_back(Args&&... args) { return this->m_vector.emplace_back(::std::forward<Args>(args)...); }
    void pop_back() { this->m_vector.pop_back(); }
    iterator insert(const const_iterator position, const M& x) { return this->m_vector.insert(position, x); }
    iterator insert(const const_iterator position, M&& x) { return this->m_vector.insert(position, ::std::forward<M>(x)); }
    iterator insert(const const_iterator position, const size_type n, const M& x) { return this->m_vector.insert(position, n, x); }
    template <class InputIterator> iterator insert(const const_iterator position, const InputIterator first, const InputIterator last) { return this->m_vector.insert(position, first, last); }
    iterator insert(const const_iterator position, const ::std::initializer_list<M> il) { return this->m_vector.insert(position, il); }
    template <class... Args> iterator emplace(const const_iterator position, Args&&... args) { return this->m_vector.emplace(position, ::std::forward<Args>(args)...); }
    iterator erase(const const_iterator position) { return this->m_vector.erase(position); }
    iterator erase(const const_iterator first, const const_iterator last) { return this->m_vector.erase(first, last); }
    void swap(F& x) noexcept { this->m_vector.swap(x.m_vector); }
    void clear() { this->m_vector.clear(); }

    allocator_type get_allocator() const noexcept { return this->m_vector.get_allocator(); }

    friend bool operator==(const F& x, const F& y) { return x.m_vector == y.m_vector; }
    friend bool operator!=(const F& x, const F& y) { return x.m_vector != y.m_vector; }

    friend void swap(F& x, F& y) noexcept { x.m_vector.swap(y.m_vector); }

    F operator+() const {
      return *this;
    }
    F operator-() const {
      F res(*this);
      for (auto& e : res) {
        e = -e;
      }
      return res;
    }
    F& operator++() {
      if (!this->empty()) ++(*this)[0];
      return *this;
    }
    F operator++(int) {
      const auto self = *this;
      ++*this;
      return self;
    }
    F& operator--() {
      if (!this->empty()) --(*this)[0];
      return *this;
    }
    F operator--(int) {
      const auto self = *this;
      --*this;
      return self;
    }
    F& operator*=(const M& g) {
      for (auto& e : *this) {
        e *= g;
      }
      return *this;
    }
    F& operator/=(const M& g) {
      assert(::std::gcd(g.val(), M::mod()) == 1);
      *this *= g.inv();
      return *this;
    }
    F& operator+=(const F& g) {
      const int n = this->size();
      const int m = g.size();
      for (int i = 0; i < ::std::min(n, m); ++i) {
        (*this)[i] += g[i];
      }
      return *this;
    }
    F& operator-=(const F& g) {
      const int n = this->size();
      const int m = g.size();
      for (int i = 0; i < ::std::min(n, m); ++i) {
        (*this)[i] -= g[i];
      }
      return *this;
    }
    F& operator<<=(const int d) {
      if (d < 0) *this >>= -d;

      const int n = this->size();
      this->resize(::std::max(0, n - d));
      this->insert(this->begin(), ::std::min(n, d), M::raw(0));
      return *this;
    }
    F& operator>>=(const int d) {
      if (d < 0) *this <<= -d;

      const int n = this->size();
      this->erase(this->begin(), this->begin() + ::std::min(n, d));
      this->resize(n);
      return *this;
    }
    F& multiply_inplace(const F& g, const int d) {
      assert(d >= 0);
      const int n = this->size();
      F res;
      ::tools::convolution(this->cbegin(), this->cbegin() + ::std::min(d, n), g.cbegin(), g.cbegin() + ::std::min<int>(d, g.size()), ::std::back_inserter(res));
      res.resize(d);
      *this = ::std::move(res);
      return *this;
    }
    F& multiply_inplace(const F& g) { return this->multiply_inplace(g, this->size()); }
    F& operator*=(const F& g) { return this->multiply_inplace(g); }
    F multiply(const F& g, const int d) const { return F(*this).multiply_inplace(g, d); }
    F multiply(const F& g) const { return this->multiply(g, this->size()); }

  private:
    F inv_regular(const int d) const {
      assert(d > 0);
      assert(M::mod() > 1);
      assert(!this->empty());
      assert(::std::gcd((*this)[0].val(), M::mod()) == 1);

      const int n = this->size();
      F res{(*this)[0].inv()};
      for (int m = 1; m < d; m *= 2) {
        F f(this->begin(), this->begin() + ::std::min(n, 2 * m));
        f *= -1;
        F r(res);
        r.multiply_inplace(r, 2 * m);
        r.multiply_inplace(f);
        r += res;
        r += res;
        res = ::std::move(r);
      }
      res.resize(d);
      return res;
    }
    template <typename M_ = M>
    F inv_faster(const int d) const {
      static_assert(::atcoder::internal::is_static_modint<M>::value);
      static_assert(2 <= M::mod() && M::mod() <= 2000000000);
      static_assert(::tools::is_prime(M::mod()));
      assert(d > 0);
      assert(!this->empty());
      assert(::tools::pow2(::tools::ceil_log2(d)) <= pow2_k(M::mod()));
      assert(::std::gcd((*this)[0].val(), M::mod()) == 1);

      const int n = this->size();
      F res{(*this)[0].inv()};
      for (int m = 1; m < d; m *= 2) {
        F f(this->begin(), this->begin() + ::std::min(n, 2 * m));
        F r(res);
        f.resize(2 * m);
        ::atcoder::internal::butterfly(f.m_vector);
        r.resize(2 * m);
        ::atcoder::internal::butterfly(r.m_vector);
        for (int i = 0; i < 2 * m; ++i) {
          f[i] *= r[i];
        }
        ::atcoder::internal::butterfly_inv(f.m_vector);
        f.erase(f.begin(), f.begin() + m);
        f.resize(2 * m);
        ::atcoder::internal::butterfly(f.m_vector);
        for (int i = 0; i < 2 * m; ++i) {
          f[i] *= r[i];
        }
        ::atcoder::internal::butterfly_inv(f.m_vector);
        M iz = M(2 * m).inv();
        iz *= -iz;
        for (int i = 0; i < m; ++i) {
          f[i] *= iz;
        }
        res.insert(res.end(), f.begin(), f.begin() + m);
      }
      res.resize(d);
      return res;
    }

  public:
    F inv(const int d) const {
      assert(d >= 0);
      if (d == 0) return F();
      if (M::mod() == 1) return F(d);
      assert(!this->empty());
      assert(::std::gcd((*this)[0].val(), M::mod()) == 1);

      if constexpr (::atcoder::internal::is_static_modint<M>::value && M::mod() <= 2000000000 && ::tools::is_prime(M::mod())) {
        if (::tools::pow2(::tools::ceil_log2(d)) <= pow2_k(M::mod())) {
          return this->inv_faster(d);
        } else {
          return this->inv_regular(d);
        }
      } else {
        return this->inv_regular(d);
      }
    }
    F inv() const { return this->inv(this->size()); }

    F& divide_inplace(const F& g, const int d) {
      assert(d >= 0);
      const int n = this->size();
      const auto g_inv = g.inv(d);
      F res;
      ::tools::convolution(this->cbegin(), this->cbegin() + ::std::min(d, n), g_inv.cbegin(), g_inv.cend(), ::std::back_inserter(res));
      res.resize(d);
      *this = ::std::move(res);
      return *this;
    }
    F& divide_inplace(const F& g) { return this->divide_inplace(g, this->size()); }
    F& operator/=(const F& g) { return this->divide_inplace(g); }
    F divide(const F& g, const int d) const { return F(*this).divide_inplace(g, d); }
    F divide(const F& g) const { return this->divide(g, this->size()); }

    // sparse
    template <class InputIterator>
    F& multiply_inplace(InputIterator g_begin, const InputIterator g_end) {
      assert(::std::is_sorted(g_begin, g_end, ::tools::less_by_first()));

      const int n = this->size();
      if (g_begin == g_end) {
        ::std::fill(this->begin(), this->end(), M::raw(0));
        return *this;
      }

      auto [d, c] = *g_begin;
      if (d == 0) {
        ++g_begin;
      } else {
        c = M::raw(0);
      }
      for (int i = n - 1; i >= 0; --i) {
        (*this)[i] *= c;
        for (auto it = g_begin; it != g_end; ++it) {
          const auto& [j, b] = *it;
          if (j > i) break;
          (*this)[i] += (*this)[i - j] * b;
        }
      }
      return *this;
    }
    F& multiply_inplace(const ::std::initializer_list<::std::pair<int, M>> il) { return this->multiply_inplace(il.begin(), il.end()); }
    template <class InputIterator>
    F multiply(const InputIterator g_begin, const InputIterator g_end) const { return F(*this).multiply_inplace(g_begin, g_end); }
    F multiply(const ::std::initializer_list<::std::pair<int, M>> il) const { return this->multiply(il.begin(), il.end()); }

    template <class InputIterator>
    F& divide_inplace(InputIterator g_begin, const InputIterator g_end) {
      assert(g_begin != g_end);
      assert(::std::is_sorted(g_begin, g_end, ::tools::less_by_first()));

      const int n = this->size();
      if (n == 0) return *this;
      if (M::mod() == 1) return *this;

      const auto [d, c] = *g_begin;
      assert(d == 0 && ::std::gcd(c.val(), M::mod()) == 1);
      const M ic = c.inv();
      ++g_begin;
      for (int i = 0; i < n; ++i) {
        for (auto it = g_begin; it != g_end; ++it) {
          const auto& [j, b] = *it;
          if (j > i) break;
          (*this)[i] -= (*this)[i - j] * b;
        }
        (*this)[i] *= ic;
      }
      return *this;
    }
    F& divide_inplace(const ::std::initializer_list<::std::pair<int, M>> il) { return this->divide_inplace(il.begin(), il.end()); }
    template <class InputIterator>
    F divide(const InputIterator g_begin, const InputIterator g_end) const { return F(*this).divide_inplace(g_begin, g_end); }
    F divide(const ::std::initializer_list<::std::pair<int, M>> il) const { return this->divide(il.begin(), il.end()); }

    // multiply and divide (1 + cz^d)
    F& multiply_inplace(const int d, const M c) {
      assert(d > 0);
      const int n = this->size();
      if (c == M(1)) {
        for (int i = n - d - 1; i >= 0; --i) {
          (*this)[i + d] += (*this)[i];
        }
      } else if (c == M(-1)) {
        for (int i = n - d - 1; i >= 0; --i) {
          (*this)[i + d] -= (*this)[i];
        }
      } else {
        for (int i = n - d - 1; i >= 0; --i) {
          (*this)[i + d] += (*this)[i] * c;
        }
      }
      return *this;
    }
    F multiply(const int d, const M c) const { return F(*this).multiply_inplace(d, c); }
    F& divide_inplace(const int d, const M c) {
      assert(d > 0);
      const int n = this->size();
      if (c == M(1)) {
        for (int i = 0; i < n - d; ++i) {
          (*this)[i + d] -= (*this)[i];
        }
      } else if (c == M(-1)) {
        for (int i = 0; i < n - d; ++i) {
          (*this)[i + d] += (*this)[i];
        }
      } else {
        for (int i = 0; i < n - d; ++i) {
          (*this)[i + d] -= (*this)[i] * c;
        }
      }
      return *this;
    }
    F divide(const int d, const M c) const { return F(*this).divide_inplace(d, c); }

    F& integral_inplace() {
      const int n = this->size();
      assert(is_leq_lpf_of_M(n));

      if (n == 0) return *this;
      if (n == 1) return *this = F{0};
      this->insert(this->begin(), 0);
      this->pop_back();
      ::std::vector<M> inv(n);
      inv[1] = M(1);
      int p = M::mod();
      for (int i = 2; i < n; ++i) {
        inv[i] = -inv[p % i] * (p / i);
      }
      for (int i = 2; i < n; ++i) {
        (*this)[i] *= inv[i];
      }
      return *this;
    }
    F integral() const { return F(*this).integral_inplace(); }

    F& derivative_inplace() {
      const int n = this->size();
      if (n == 0) return *this;
      for (int i = 2; i < n; ++i) {
        (*this)[i] *= i;
      }
      this->erase(this->begin());
      this->push_back(0);
      return *this;
    }
    F derivative() const { return F(*this).derivative_inplace(); }

    F& log_inplace(const int d) {
      assert(d >= 0);
      assert(is_leq_lpf_of_M(d));
      this->resize(d);
      if (d == 0) return *this;
      assert((*this)[0] == M(1));

      const F f_inv = this->inv();
      this->derivative_inplace();
      this->multiply_inplace(f_inv);
      this->integral_inplace();
      return *this;
    }
    F& log_inplace() { return this->log_inplace(this->size()); }
    F log(const int d) const { return F(*this).log_inplace(d); }
    F log() const { return this->log(this->size()); }

  private:
    F& exp_inplace_regular(const int d) {
      assert(d >= 0);
      assert(is_leq_lpf_of_M(d));
      assert(this->empty() || (*this)[0] == M::raw(0));

      const int n = this->size();
      F g{1};
      for (int m = 1; m < d; m *= 2) {
        F r(g);
        r.resize(2 * m);
        r.log_inplace();
        r *= -1;
        r += F(this->begin(), this->begin() + ::std::min(n, 2 * m));
        ++r[0];
        r.multiply_inplace(g);
        g = ::std::move(r);
      }
      g.resize(d);
      *this = ::std::move(g);
      return *this;
    }
    template <typename M_ = M>
    F& exp_inplace_faster(const int d) {
      static_assert(::atcoder::internal::is_static_modint<M>::value);
      static_assert(2 <= M::mod() && M::mod() <= 2000000000);
      static_assert(::tools::is_prime(M::mod()));
      assert(d > 0);
      assert(is_leq_lpf_of_M(d));
      assert(::tools::pow2(::tools::ceil_log2(d)) <= pow2_k(M::mod()));
      assert(this->empty() || (*this)[0] == M::raw(0));
 
      F g{1}, g_fft{1, 1};
      this->resize(d);
      (*this)[0] = 1;
      F h_drv(this->derivative());
      for (int m = 2; m < d; m *= 2) {
        // prepare
        F f_fft(this->begin(), this->begin() + m);
        f_fft.resize(2 * m);
        ::atcoder::internal::butterfly(f_fft.m_vector);

        // Step 2.a'
        {
          F g_(m);
          for (int i = 0; i < m; ++i) {
            g_[i] = f_fft[i] * g_fft[i];
          }
          ::atcoder::internal::butterfly_inv(g_.m_vector);
          g_.erase(g_.begin(), g_.begin() + m / 2);
          g_.resize(m);
          ::atcoder::internal::butterfly(g_.m_vector);
          for (int i = 0; i < m; ++i) {
            g_[i] *= g_fft[i];
          }
          ::atcoder::internal::butterfly_inv(g_.m_vector);
          g_.resize(m / 2);
          g_ /= M(-m) * m;
          g.insert(g.end(), g_.begin(), g_.begin() + m / 2);
        }

        // Step 2.b'--d'
        F t(this->begin(), this->begin() + m);
        t.derivative_inplace();
        {
          // Step 2.b'
          F r{h_drv.begin(), h_drv.begin() + m - 1};
          // Step 2.c'
          r.resize(m);
          ::atcoder::internal::butterfly(r.m_vector);
          for (int i = 0; i < m; ++i) {
            r[i] *= f_fft[i];
          }
          ::atcoder::internal::butterfly_inv(r.m_vector);
          r /= -m;
          // Step 2.d'
          t += r;
          t.insert(t.begin(), t.back());
          t.pop_back();
        }

        // Step 2.e'
        if (2 * m < d) {
          t.resize(2 * m);
          ::atcoder::internal::butterfly(t.m_vector);
          g_fft = g;
          g_fft.resize(2*m);
          ::atcoder::internal::butterfly(g_fft.m_vector);
          for (int i = 0; i < 2 * m; ++i) {
            t[i] *= g_fft[i];
          }
          ::atcoder::internal::butterfly_inv(t.m_vector);
          t.resize(m);
          t /= 2 * m;
        } else { // この場合分けをしても数パーセントしか速くならない
          F g1(g.begin() + m / 2, g.end());
          F s1(t.begin() + m / 2, t.end());
          t.resize(m/2);
          g1.resize(m);
          ::atcoder::internal::butterfly(g1.m_vector);
          t.resize(m);
          ::atcoder::internal::butterfly(t.m_vector);
          s1.resize(m);
          ::atcoder::internal::butterfly(s1.m_vector);
          for (int i = 0; i < m; ++i) {
            s1[i] = g_fft[i] * s1[i] + g1[i] * t[i];
          }
          for (int i = 0; i < m; ++i) {
            t[i] *= g_fft[i];
          }
          ::atcoder::internal::butterfly_inv(t.m_vector);
          ::atcoder::internal::butterfly_inv(s1.m_vector);
          for (int i = 0; i < m / 2; ++i) {
            t[i + m / 2] += s1[i];
          }
          t /= m;
        }

        // Step 2.f'
        F v(this->begin() + m, this->begin() + ::std::min<int>(d, 2 * m));
        v.resize(m);
        t.insert(t.begin(), m - 1, 0);
        t.push_back(0);
        t.integral_inplace();
        for (int i = 0; i < m; ++i) {
          v[i] -= t[m + i];
        }

        // Step 2.g'
        v.resize(2 * m);
        ::atcoder::internal::butterfly(v.m_vector);
        for (int i = 0; i < 2 * m; ++i) {
          v[i] *= f_fft[i];
        }
        ::atcoder::internal::butterfly_inv(v.m_vector);
        v.resize(m);
        v /= 2 * m;

        // Step 2.h'
        for (int i = 0; i < ::std::min(d - m, m); ++i) {
          (*this)[m + i] = v[i];
        }
      }
      return *this;
    }

  public:
    F& exp_inplace(const int d) {
      assert(d >= 0);
      assert(is_leq_lpf_of_M(d));
      assert(this->empty() || (*this)[0] == M::raw(0));

      if (d == 0) {
        this->clear();
        return *this;
      }

      if constexpr (::atcoder::internal::is_static_modint<M>::value && M::mod() <= 2000000000 && ::tools::is_prime(M::mod())) {
        if (::tools::pow2(::tools::ceil_log2(d)) <= pow2_k(M::mod())) {
          return this->exp_inplace_faster(d);
        } else {
          return this->exp_inplace_regular(d);
        }
      } else {
        return this->exp_inplace_regular(d);
      }
    }
    F& exp_inplace() { return this->exp_inplace(this->size()); }
    F exp(const int d) const { return F(*this).exp_inplace(d); }
    F exp() const { return this->exp(this->size()); }

  private:
    F& pow_inplace_regular(long long k, const int d, const int l) {
      assert(k > 0);
      assert(d > 0);
      assert(l >= 0);
      assert(d - l * k > 0);

      this->erase(this->begin(), this->begin() + l);
      this->resize(d - l * k);

      F sum(d - l * k);
      for (F p = *this; k > 0; k /= 2, p *= p) {
        if (k & 1) sum += p;
      }

      *this = ::std::move(sum);
      this->insert(this->begin(), l * k, 0);
      return *this;
    }
    F& pow_inplace_faster(const long long k, const int d, const int l) {
      assert(k > 0);
      assert(d > 0);
      assert(l >= 0);
      assert(d - l * k > 0);
      assert(is_leq_lpf_of_M(d - l * k));
      assert(::std::gcd((*this)[l].val(), M::mod()) == 1);

      M c{(*this)[l]};
      this->erase(this->begin(), this->begin() + l);
      *this /= c;
      this->log_inplace(d - l * k);
      *this *= k;
      this->exp_inplace();
      *this *= c.pow(k);
      this->insert(this->begin(), l * k, 0);
      return *this;
    }

  public:
    F& pow_inplace(const long long k, const int d) {
      assert(k >= 0);
      assert(d >= 0);

      const int n = this->size();
      if (d == 0) {
        this->clear();
        return *this;
      }
      if (k == 0) {
        *this = F(d);
        (*this)[0] = M(1);
        return *this;
      }

      int l = 0;
      while (l < n && (*this)[l] == M::raw(0)) ++l;
      if (l == n || l > (d - 1) / k) {
        return *this = F(d);
      }

      if (::std::gcd((*this)[l].val(), M::mod()) == 1 && is_leq_lpf_of_M(d - l * k)) {
        return this->pow_inplace_faster(k, d, l);
      } else {
        return this->pow_inplace_regular(k, d, l);
      }
    }
    F& pow_inplace(const long long k) { return this->pow_inplace(k, this->size()); }
    F pow(const long long k, const int d) const { return F(*this).pow_inplace(k, d); }
    F pow(const long long k) const { return this->pow(k, this->size()); }

    F operator()(const F& g) const {
      assert(g.empty() || g[0] == M::raw(0));

      const int n = this->size();
      F h(n);
      if (n == 0) return h;

      const int m = g.size();
      int l;
      for (l = 0; l < ::std::min(m, n) && g[l] == M::raw(0); ++l);
      h[0] = (*this)[0];
      if (l == ::std::min(m, n)) return h;

      const F g_1(g.begin() + l, g.begin() + ::std::min(m, n));
      for (int i = l; i < ::std::min(m, n); ++i) {
        h[i] += (*this)[1] * g[i];
      }

      auto g_k = g_1;
      for (int k = 2, d; (d = ::std::min(k * (m - l - 1) + 1, n - l * k)) > 0; ++k) {
        g_k.multiply_inplace(g_1, d);
        for (int i = l * k; i < l * k + d; ++i) {
          h[i] += (*this)[k] * g_k[i - l * k];
        }
      }

      return h;
    }
    F compositional_inverse() const {
      assert(this->size() >= 2);
      assert((*this)[0] == M::raw(0));
      assert(::std::gcd((*this)[1].val(), M::mod()) == 1);

      const int n = this->size();
      ::std::vector<F> f;
      f.reserve(::std::max(2, n - 1));
      f.emplace_back(n);
      f[0][0] = M::raw(1);
      f.push_back(*this);
      for (int i = 2; i < n - 1; ++i) {
        f.push_back(f.back() * f[1]);
      }

      ::std::vector<M> invpow_f11;
      invpow_f11.reserve(n);
      invpow_f11.push_back(M::raw(1));
      invpow_f11.push_back(f[1][1].inv());
      for (int i = 2; i < n; ++i) {
        invpow_f11.push_back(invpow_f11.back() * invpow_f11[1]);
      }

      F g(n);
      g[1] = invpow_f11[1];
      for (int i = 2; i < n; ++i) {
        for (int j = 1; j < i; ++j) {
          g[i] -= f[j][i] * g[j];
        }
        g[i] *= invpow_f11[i];
      }

      return g;
    }

    friend F operator*(const F& f, const M& g) { return F(f) *= g; }
    friend F operator*(const M& f, const F& g) { return F(g) *= f; }
    friend F operator/(const F& f, const M& g) { return F(f) /= g; }
    friend F operator+(const F& f, const F& g) { return F(f) += g; }
    friend F operator-(const F& f, const F& g) { return F(f) -= g; }
    friend F operator*(const F& f, const F& g) { return F(f) *= g; }
    friend F operator/(const F& f, const F& g) { return F(f) /= g; }
    friend F operator<<(const F& f, const int d) { return F(f) <<= d; }
    friend F operator>>(const F& f, const int d) { return F(f) >>= d; }
  };
}


#line 1 "tools/virtual_vector.hpp"



#line 6 "tools/virtual_vector.hpp"
#include <memory>
#line 10 "tools/virtual_vector.hpp"

namespace tools {
  template <typename F>
  class virtual_vector {
  public:
    using size_type = ::std::size_t;

    class iterator {
      const virtual_vector<F> *m_parent;
      size_type m_i;

    public:
      using reference = decltype(::std::declval<F>()(::std::declval<size_type>()));
      using value_type = ::std::remove_const_t<::std::remove_reference_t<reference>>;
      using difference_type = ::std::ptrdiff_t;
      using pointer = const value_type*;
      using iterator_category = ::std::random_access_iterator_tag;

      iterator() = default;
      iterator(const virtual_vector<F> * const parent, const size_type i) : m_parent(parent), m_i(i) {
      }

      reference operator*() const {
        return (*this->m_parent)[this->m_i];
      }
      pointer operator->() const {
        return &(*(*this));
      }

      iterator& operator++() {
        ++this->m_i;
        return *this;
      }
      iterator operator++(int) {
        const auto self = *this;
        ++*this;
        return self;
      }
      iterator& operator--() {
        --this->m_i;
        return *this;
      }
      iterator operator--(int) {
        const auto self = *this;
        --*this;
        return self;
      }
      iterator& operator+=(const difference_type n) {
        this->m_i += n;
        return *this;
      }
      iterator& operator-=(const difference_type n) {
        this->m_i -= n;
        return *this;
      }
      friend iterator operator+(const iterator self, const difference_type n) {
        return iterator(self.m_parent, self.m_i + n);
      }
      friend iterator operator+(const difference_type n, const iterator self) {
        return self + n;
      }
      friend iterator operator-(const iterator self, const difference_type n) {
        return iterator(self.m_parent, self.m_i - n);
      }
      friend difference_type operator-(const iterator lhs, const iterator rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return static_cast<difference_type>(lhs.m_i) - static_cast<difference_type>(rhs.m_i);
      }
      reference operator[](const difference_type n) const {
        return *(*this + n);
      }

      friend bool operator==(const iterator lhs, const iterator rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i == rhs.m_i;
      }
      friend bool operator!=(const iterator lhs, const iterator rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i != rhs.m_i;
      }
      friend bool operator<(const iterator lhs, const iterator rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i < rhs.m_i;
      }
      friend bool operator<=(const iterator lhs, const iterator rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i <= rhs.m_i;
      }
      friend bool operator>(const iterator lhs, const iterator rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i > rhs.m_i;
      }
      friend bool operator>=(const iterator lhs, const iterator rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i >= rhs.m_i;
      }
    };

    using const_reference = decltype(::std::declval<F>()(::std::declval<size_type>()));
    using value_type = ::std::remove_const_t<::std::remove_reference_t<const_reference>>;
    using reference = value_type&;
    using const_iterator = iterator;
    using difference_type = ::std::ptrdiff_t;
    using allocator_type = ::std::allocator<value_type>;
    using pointer = value_type*;
    using const_pointer = const value_type*;
    using reverse_iterator = ::std::reverse_iterator<iterator>;
    using const_reverse_iterator = ::std::reverse_iterator<const_iterator>;

  private:
    size_type m_size;
    F m_selector;

  public:
    virtual_vector() = default;
    virtual_vector(const size_type n, const F& selector) : m_size(n), m_selector(selector) {
    }

    iterator begin() noexcept { return iterator(this, 0); }
    const_iterator begin() const noexcept { return const_iterator(this, 0); }
    const_iterator cbegin() const noexcept { return const_iterator(this, 0); }
    iterator end() noexcept { return iterator(this, this->size()); }
    const_iterator end() const noexcept { return const_iterator(this, this->size()); }
    const_iterator cend() const noexcept { return const_iterator(this, this->size()); }
    reverse_iterator rbegin() noexcept { return ::std::make_reverse_iterator(this->end()); }
    const_reverse_iterator rbegin() const noexcept { return ::std::make_reverse_iterator(this->end()); }
    const_reverse_iterator crbegin() const noexcept { return ::std::make_reverse_iterator(this->cend()); }
    reverse_iterator rend() noexcept { return ::std::make_reverse_iterator(this->begin()); }
    const_reverse_iterator rend() const noexcept { return ::std::make_reverse_iterator(this->begin()); }
    const_reverse_iterator crend() const noexcept { return ::std::make_reverse_iterator(this->cbegin()); }

    size_type size() const noexcept { return this->m_size; }
    bool empty() const noexcept { return this->size() == 0; }

    const_reference operator[](const size_type n) const { assert(n < this->size()); return this->m_selector(n); }
    const_reference at(const size_type n) const { return (*this)[n]; }
    const_reference front() const { return *this->begin(); }
    const_reference back() const { return *this->rbegin(); }

    template <typename G>
    friend bool operator==(const virtual_vector<F>& x, const virtual_vector<G>& y) { return ::std::equal(x.begin(), x.end(), y.begin(), y.end()); }
    template <typename G>
    friend bool operator!=(const virtual_vector<F>& x, const virtual_vector<G>& y) { return !(x == y); }
    template <typename G>
    friend bool operator<(const virtual_vector<F>& x, const virtual_vector<G>& y) { return ::std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
    template <typename G>
    friend bool operator<=(const virtual_vector<F>& x, const virtual_vector<G>& y) { return !(x > y); }
    template <typename G>
    friend bool operator>(const virtual_vector<F>& x, const virtual_vector<G>& y) { return y < x; }
    template <typename G>
    friend bool operator>=(const virtual_vector<F>& x, const virtual_vector<G>& y) { return !(x < y); }
  };
}


#line 12 "tools/stirling_2nd.hpp"

namespace tools {

  namespace stirling_2nd {

    template <typename M>
    auto fixed_n(const int N, const int K) {
      assert(::tools::is_prime(M::mod()));
      assert(0 <= ::std::min(N, K) && ::std::min(N, K) < M::mod());

      ::tools::fact_mod_cache<M> cache;
      ::tools::pow_mod_cache<M> pow_m1(-1);
      ::tools::fps<M> a, b;
      for (int i = 0; i <= ::std::min(N, K); ++i) {
        a.push_back(M(i).pow(N) * cache.fact_inv(i));
        b.push_back(pow_m1[i] * cache.fact_inv(i));
      }
      a.multiply_inplace(b);

      return ::tools::virtual_vector(K + 1, [N, a](const int k) -> const M& {
        static const auto zero = M::raw(0);
        return k <= N ? a[k] : zero;
      });
    }

    template <typename M>
    auto fixed_k(const int N, const int K) {
      assert(::tools::is_prime(M::mod()));
      assert(N >= 0);
      assert(0 <= K && K < M::mod());
      assert(N - K + 1 < M::mod());

      ::tools::fps<M> f(::std::max(0, N - K + 1));
      if (!f.empty()) {
        ::tools::fact_mod_cache<M> cache;
        for (int i = 0; i <= N - K; ++i) {
          f[i] = cache.fact_inv(i + 1);
        }
        f.pow_inplace(K);
        f *= cache.fact_inv(K);
        for (int n = K; n <= N; ++n) {
          f[n - K] *= cache.fact(n);
        }
      }

      return ::tools::virtual_vector(N + 1, [K, f](const int n) -> const M& {
        static const auto zero = M::raw(0);
        return n < K ? zero : f[n - K];
      });
    }

    template <typename M>
    auto diagonal(const int N) {
      assert(N >= 0);

      return ::tools::virtual_vector(N + 1, [](const int n) -> const M& {
        static const M one(1);
        return one;
      });
    }

    template <typename M>
    ::std::vector<::std::vector<M>> all(const int N, const int K) {
      assert(N >= 0);
      assert(K >= 0);
      ::std::vector<::std::vector<M>> S(N + 1);
      S[0].emplace_back(1);
      S[0].insert(S[0].end(), K, M::raw(0));
      for (int n = 1; n <= N; ++n) {
        S[n].resize(K + 1, M::raw(0));
        for (int k = 0; k <= ::std::min(n, K); ++k) {
          if (k > 0) S[n][k] += S[n - 1][k - 1];
          S[n][k] += M(k) * S[n - 1][k];
        }
      }
      return S;
    }
  }
}


#line 14 "tools/bell.hpp"

namespace tools {

  namespace bell {

    template <typename M>
    auto fixed_n(const int N, const int K) {
      assert(::tools::is_prime(M::mod()));
      assert(0 <= ::std::min(N, K) && ::std::min(N, K) < M::mod());

      const auto S_N = ::tools::stirling_2nd::fixed_n<M>(N, K);
      ::std::vector<M> B_N;
      ::std::partial_sum(S_N.begin(), ::std::next(S_N.begin(), ::std::min(N, K) + 1), ::std::back_inserter(B_N));

      return ::tools::virtual_vector(K + 1, [N, B_N](const int k) -> const M& {
        return B_N[::std::min(N, k)];
      });
    }

    template <typename M>
    ::std::vector<M> diagonal(const int N) {
      assert(::tools::is_prime(M::mod()));
      assert(0 <= N && N < M::mod());

      ::tools::fact_mod_cache<M> cache;
      ::tools::fps<M> f(N + 1);
      for (int i = 1; i <= N; ++i) f[i] = cache.fact_inv(i);
      f.exp_inplace();
      for (int n = 0; n <= N; ++n) f[n] *= cache.fact(n);

      return ::std::vector<M>(f.begin(), f.end());
    }

    template <typename M>
    ::std::vector<::std::vector<M>> all(const int N, const int K) {
      assert(N >= 0);
      assert(K >= 0);

      auto res = ::tools::stirling_2nd::all<M>(N, K);
      for (int n = 0; n <= N; ++n) {
        for (int k = 1; k <= K; ++k) {
          res[n][k] += res[n][k - 1];
        }
      }

      return res;
    }
  }
}


#line 1 "tools/partition_function.hpp"



#line 7 "tools/partition_function.hpp"

namespace tools {

  namespace partition_function {
    template <typename M>
    ::std::vector<M> diagonal(const int N) {
      assert(N >= 0);

      ::tools::fps<M> p(N + 1);

      ++p[0];
      for (int k = 1; k * (3 * k + 1) / 2 <= N; k += 2) --p[k * (3 * k + 1) / 2];
      for (int k = 2; k * (3 * k + 1) / 2 <= N; k += 2) ++p[k * (3 * k + 1) / 2];
      for (int k = 1; k * (3 * k - 1) / 2 <= N; k += 2) --p[k * (3 * k - 1) / 2];
      for (int k = 2; k * (3 * k - 1) / 2 <= N; k += 2) ++p[k * (3 * k - 1) / 2];
      p = p.inv();

      return ::std::vector<M>(p.begin(), p.end());
    }

    template <typename M>
    ::std::vector<::std::vector<M>> all(const int N, const int K) {
      assert(N >= 0);
      assert(K >= 0);

      auto dp = ::std::vector(N + 1, ::std::vector<M>(K + 1, M::raw(0)));

      dp[0][0] = M(1);
      for (int i = 0; i <= N; ++i) {
        for (int j = !i; j <= K; ++j) {
          if (j > 0) dp[i][j] += dp[i][j - 1];
          if (i >= j) dp[i][j] += dp[i - j][j];
        }
      }

      return dp;
    }
  }
}


#line 14 "tools/twelvefold_way.hpp"

namespace tools {
  template <bool labels_ball, bool labels_box>
  struct twelvefold_way {};

  template <>
  struct twelvefold_way<true, true> {
    twelvefold_way() = delete;

    template <typename M>
    static M at_most_1(const long long N, const long long K) {
      assert(N >= 0);
      assert(K >= 0);

      if (K < N) {
        // O(1)
        return M::raw(0);
      } else {
        if (::tools::is_prime(M::mod())) {
          if (::std::min<long long>(K, M::mod()) <= 10000000) {
            // O(min(K, P) + log K / log P)
            ::tools::fact_mod_cache<M> cache;
            return cache.permutation(K, N);
          } else {
            // O(sqrt(P log P) + sqrt(P / log P) log K)
            ::tools::large_fact_mod_cache<M> cache;
            return cache.permutation(K, N);
          }
        } else {
          // O(M + (log M / log log M) log K)
          ::tools::extended_lucas<M> cache;
          return cache.permutation(K, N);
        }
      }
    }

    template <typename M>
    static M unrestricted(const long long N, const long long K) {
      assert(N >= 0);
      assert(K >= 0);

      // O(log N)
      return M(K).pow(N);
    }

    template <typename M>
    static M at_least_1(const long long N, const long long K) {
      assert(N >= 0);
      assert(K >= 0);

      if (N < K) {
        // O(1)
        return M::raw(0);
      } else {
        if (::tools::is_prime(M::mod())) {
          // O(K log N)
          ::tools::fact_mod_cache<M> cache;
          ::tools::pow_mod_cache<M> pow_m1(-1);
          auto res = M::raw(0);
          for (int i = 0; i <= K; ++i) {
            res += pow_m1[K - i] * cache.combination(K, i) * M(i).pow(N);
          }
          return res;
        } else {
          if (N <= 10000000 / K) {
            // O(NK)
            ::tools::fact_mod_cache<M> cache;
            auto res = ::tools::stirling_2nd::all<M>(N, K)[N][K];
            int i;
            for (M k(i = 1); i <= K; ++i, ++k) {
              res *= k;
            }
            return res;
          } else {
            // O(M + K ((log M / log log M) log K + (log M / log log M)^2 + log N))
            ::tools::extended_lucas<M> cache;
            ::tools::pow_mod_cache<M> pow_m1(-1);
            auto res = M::raw(0);
            for (int i = 0; i <= K; ++i) {
              res += pow_m1[K - i] * cache.combination(K, i) * M(i).pow(N);
            }
            return res;
          }
        }
      }
    }
  };

  template <>
  struct twelvefold_way<false, true> {
    twelvefold_way() = delete;

    template <typename M>
    static M at_most_1(const long long N, const long long K) {
      assert(N >= 0);
      assert(K >= 0);

      if (K < N) {
        // O(1)
        return M::raw(0);
      } else {
        if (::tools::is_prime(M::mod())) {
          if (std::min<long long>(K, M::mod()) <= 10000000) {
            // O(min(K, P) + log K / log P)
            ::tools::fact_mod_cache<M> cache;
            return cache.combination(K, N);
          } else {
            // O(sqrt(P log P) + sqrt(P / log P) log K)
            ::tools::large_fact_mod_cache<M> cache;
            return cache.combination(K, N);
          }
        } else {
          // O(M + (log M / log log M) log K)
          ::tools::extended_lucas<M> cache;
          return cache.combination(K, N);
        }
      }
    }

    template <typename M>
    static M unrestricted(const long long N, const long long K) {
      assert(N >= 0);
      assert(K >= 0);

      if (::tools::is_prime(M::mod())) {
        if (std::min<long long>(N + K, M::mod()) <= 10000000) {
          // O(min(N + K, P) + log (N + K) / log P)
          ::tools::fact_mod_cache<M> cache;
          return cache.combination_with_repetition(K, N);
        } else {
          // O(sqrt(P log P) + sqrt(P / log P) log (N + K))
          ::tools::large_fact_mod_cache<M> cache;
          return cache.combination_with_repetition(K, N);
        }
      } else {
        // O(M + (log M / log log M) log (N + K))
        ::tools::extended_lucas<M> cache;
        return cache.combination_with_repetition(K, N);
      }
    }

    template <typename M>
    static M at_least_1(const long long N, const long long K) {
      assert(N >= 0);
      assert(K >= 0);

      if (N < K) {
        // O(1)
        return M::raw(0);
      } else {
        if (::tools::is_prime(M::mod())) {
          if (std::min<long long>(N, M::mod()) <= 10000000) {
            // O(min(N, P) + log N / log P)
            ::tools::fact_mod_cache<M> cache;
            return cache.binomial(N - 1, N - K);
          } else {
            // O(sqrt(P log P) + sqrt(P / log P) log N)
            ::tools::large_fact_mod_cache<M> cache;
            return cache.binomial(N - 1, N - K);
          }
        } else {
          // O(M + (log M / log log M) log N)
          ::tools::extended_lucas<M> cache;
          return cache.binomial(N - 1, N - K);
        }
      }
    }
  };

  template <>
  struct twelvefold_way<true, false> {
    twelvefold_way() = delete;

    template <typename M>
    static M at_most_1(const long long N, const long long K) {
      assert(N >= 0);
      assert(K >= 0);

      // O(1)
      return M(N <= K);
    }

    template <typename M>
    static M unrestricted(const long long N, const long long K) {
      assert(N >= 0);
      assert(K >= 0);

      if (::tools::is_prime(M::mod()) && ::std::min(N, K) < M::mod()) {
        // O(min(N, K) log N)
        return ::tools::bell::fixed_n<M>(N, K)[K];
      } else {
        // O(NK)
        return ::tools::bell::all<M>(N, K)[N][K];
      }
    }

    template <typename M>
    static M at_least_1(const long long N, const long long K) {
      assert(N >= 0);
      assert(K >= 0);

      if (N < K) {
        // O(1)
        return M::raw(0);
      } else {
        if (::tools::is_prime(M::mod()) && K < M::mod()) {
          // O(K \log N)
          return ::tools::stirling_2nd::fixed_n<M>(N, K)[K];
        } else {
          // O(NK)
          return ::tools::stirling_2nd::all<M>(N, K)[N][K];
        }
      }
    }
  };

  template <>
  struct twelvefold_way<false, false> {
    twelvefold_way() = delete;

    template <typename M>
    static M at_most_1(const long long N, const long long K) {
      assert(N >= 0);
      assert(K >= 0);

      // O(1)
      return M(N <= K);
    }

    template <typename M>
    static M unrestricted(const long long N, const long long K) {
      assert(N >= 0);
      assert(K >= 0);

      if (N == K) {
        // O(N log N)
        return ::tools::partition_function::diagonal<M>(N)[N];
      } else {
        // O(N min(N, K))
        return ::tools::partition_function::all<M>(N, ::std::min(N, K))[N][::std::min(N, K)];
      }
    }

    template <typename M>
    static M at_least_1(const long long N, const long long K) {
      assert(N >= 0);
      assert(K >= 0);

      if (N < K) {
        // O(1)
        return M::raw(0);
      } else if (N == 2 * K) {
        // O(K log K)
        return ::tools::partition_function::diagonal<M>(K)[K];
      } else {
        // O((N - K) min(N - K, K))
        return ::tools::partition_function::all<M>(N - K, ::std::min(N - K, K))[N - K][::std::min(N - K, K)];
      }
    }
  };
}


#line 6 "tests/twelvefold_way/labeled_ball_labeled_box_at_most_1.test.cpp"

using mint = atcoder::modint1000000007;

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  int n, k;
  std::cin >> n >> k;
  std::cout << tools::twelvefold_way<true, true>::at_most_1<mint>(n, k).val() << '\n';

  return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ 00_sample_01.in :heavy_check_mark: AC 5 ms 4 MB
g++ 00_sample_02.in :heavy_check_mark: AC 4 ms 4 MB
g++ 00_sample_03.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_corner_00.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_corner_01.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_corner_02.in :heavy_check_mark: AC 4 ms 4 MB
g++ 02_maximum_00.in :heavy_check_mark: AC 4 ms 4 MB
g++ 02_maximum_01.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_00.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_01.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_02.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_03.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_04.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_05.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_06.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_07.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_08.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_09.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_10.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_11.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_12.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_13.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_14.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_15.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_16.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_17.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_18.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_random_19.in :heavy_check_mark: AC 4 ms 4 MB
g++ 04_corner_01.in :heavy_check_mark: AC 5 ms 4 MB
g++ 04_corner_02.in :heavy_check_mark: AC 4 ms 4 MB
Back to top page