#include #include #include #include #include #include // (edited) Nyaan's library namespace nachia{ // output : denominator of rational gf template std::vector BerlekampMassey(const std::vector &s){ const int N = (int)s.size(); std::vector b, c; b.reserve(N+1); c.reserve(N+1); const Modint Zero = Modint(0); const Modint One = Modint(1); b.push_back(One); c.push_back(One); Modint y = One; for(int ed=1; ed<=N; ed++){ int l = int(c.size()); int m = int(b.size()); Modint x = Zero; for(int i=0; i #include namespace nachia{ // ax + by = gcd(a,b) // return ( x, - ) std::pair ExtGcd(long long a, long long b){ long long x = 1, y = 0; while(b){ long long u = a / b; std::swap(a-=b*u, b); std::swap(x-=y*u, y); } return std::make_pair(x, a); } } // namespace nachia namespace nachia{ class DynamicModSupplier{ using u64 = unsigned long long; using Int = unsigned int; private: u64 imod; Int mod; // atcoder library u64 reduce2(u64 z) const noexcept { // atcoder library #ifdef _MSC_VER u64 x; _umul128(z, im, &x); #else using u128 = unsigned __int128; u64 x = (u64)(((u128)(z)*imod) >> 64); #endif return z - x * mod; } Int reduce(u64 z) const noexcept { Int v = reduce2(z); if(mod <= v) v += mod; return v; } public: DynamicModSupplier(unsigned int MOD = 998244353) : mod(MOD) { assert(2 <= MOD); assert(MOD < (1u << 31)); imod = (u64)(-1) / mod + 1; } Int add(Int a, Int b) const { a += b; if(a >= mod){ a -= mod; } return a; } Int sub(Int a, Int b) const { a -= b; if(a >= mod){ a += mod; } return a; } Int mul(Int a, Int b) const { return reduce((u64)a * b); } Int muladd(Int a, Int b, Int c) const { return reduce((u64)a * b + c); } Int inv(Int a) const { Int v = ExtGcd(a, mod).first; return (v < mod) ? v : (v + mod); } Int pow(Int a, u64 i) const { Int r = a, ans = 1; while(i){ if(i & 1) ans = mul(ans, r); i /= 2; r = mul(r, r); } return ans; } Int getMod() const { return mod; } }; } // namespace nachia namespace nachia{ template struct GarnerMod{ using Int = unsigned int; using IntLong = unsigned long long; std::vector mods; std::vector dynmods; std::vector> table_coeff; std::vector table_coeffinv; void precalc(std::vector new_mods){ mods = std::move(new_mods); dynmods.resize(mods.size()); for(size_t i=0; i(nmods, 1)); for(int j=0; j& x){ int nmods = mods.size(); std::vector table_const(nmods); FinishType res = 0; FinishType res_coeff = 1; for(int j=0; j calc(std::vector> x){ int n = x[0].size(), m = x.size(); std::vector res(n); std::vector buf(m); for(int i=0; i std::vector PartConvolution(std::vector A, std::vector B) { std::vector> AA(A.size()); for(std::size_t i=0; i> BB(B.size()); for(std::size_t i=0; i res(AB.size()); for(std::size_t i=0; i std::vector Convolution(std::vector A, std::vector B){ auto Q1 = PartConvolution(A, B); auto Q2 = PartConvolution(A, B); auto Q3 = PartConvolution(A, B); GarnerMod garner; garner.precalc({ 998244353, 897581057, 880803841 }); return garner.calc({ Q1, Q2, Q3 }); } } // namespace nachia namespace nachia{ template Modint KthTermOfRationalGF( std::vector denom, std::vector numer, unsigned long long K ){ assert(denom.size() != 0); assert(denom.size() == numer.size()); assert(denom[0].val() != 0); int n = (int)denom.size(); while(K != 0){ auto Qn = denom; Qn.push_back(Modint(0)); for(int i=1; i Modint KthTermOfLinearRecurrence( std::vector denom, std::vector firstTerms, unsigned long long K ){ assert(denom.size() <= firstTerms.size()); firstTerms.resize(denom.size()); auto numer = Convolution(firstTerms, denom); numer.resize(denom.size()); return KthTermOfRationalGF(std::move(denom), std::move(numer), K); } } // namespace nachia using Modint = atcoder::modint1000000007; using i64 = long long; #define rep(i,n) for(int i=0; i> enumBaseState(int N){ vector st; vector arr(N, -1); vector> res; auto dfs = [&](auto& rec, int p, int nx){ if(p == N){ res.push_back(arr); return; } arr[p] = -1; rec(rec, p+1, nx); if(p != 0 && arr[p-1] != -1){ arr[p] = arr[p-1]; rec(rec, p+1, nx); } else { vector stbuf = st; arr[p] = nx; st.push_back(nx); rec(rec, p+1, nx+1); st.pop_back(); for(int x=0; x& Q){ vector occ; for(int& q : Q) if(q != -1){ int res = -1; rep(i,occ.size()) if(occ[i] == q) res = i; if(res == -1){ res = int(occ.size()); occ.push_back(q); } q = res; } } vector> enumNextState(vector cur, int at){ int n = int(cur.size()); int q = cur[at]; auto t0 = cur; t0[at] = -1; auto t1 = cur; vector> res; if(q == -1){ t1[at] = 1001001001; if(at != 0 && t1[at-1] != -1) t1[at] = t1[at-1]; } else { int qocc = 0; rep(i,n) if(cur[i] == q) qocc++; if(qocc == 1) t0 = vector(0); int p = -1; if(at != 0 && cur[at-1] != -1) p = cur[at-1]; if(p != -1) rep(i,n) if(t1[i] == p) t1[i] = q; } if(!t0.empty()){ refine(t0); res.push_back(move(t0)); } if(!t1.empty()){ refine(t1); res.push_back(move(t1)); } return res; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; i64 L; cin >> L; auto baseState = enumBaseState(N); sort(baseState.begin(), baseState.end()); vector>> stateList(N+1); vector>> transList(N); auto lbi = [&](const vector>& l, const vector& tg) -> int { return int(lower_bound(l.begin(), l.end(), tg) - l.begin()); }; stateList[0] = baseState; rep(i,N){ auto& dest = stateList[i+1]; for(auto q : stateList[i]) for(auto r : enumNextState(q, i)){ dest.push_back(r); } sort(dest.begin(), dest.end()); dest.erase(unique(dest.begin(), dest.end()), dest.end()); rep(j,stateList[i].size()){ for(auto r : enumNextState(stateList[i][j], i)){ int k = lbi(dest, r); transList[i].push_back({ j, k }); } } } vector sz(N+1); rep(i,N+1) sz[i] = int(stateList[i].size()); rep(i,N){ vector F(N, -1); F[i] = 0; int j = lbi(stateList[i], F); transList[i].push_back({ j, sz[i+1] }); transList[i].push_back({ sz[i], sz[i+1] }); } rep(i,N+1) sz[i]++; vector firstTerms; vector dp(sz[0]); dp.back() = 1; dp[0] = 1; firstTerms.push_back(1); int Z = sz[0] * 2 + 5; rep(l,Z){ rep(y,N){ vector nx(sz[y+1]); for(auto [u,v] : transList[y]) nx[v] += dp[u]; swap(dp, nx); } firstTerms.push_back(dp.back()); } auto bm = nachia::BerlekampMassey(firstTerms); firstTerms.resize(bm.size()); Modint ans = nachia::KthTermOfLinearRecurrence(bm, firstTerms, L+1) - 1; cout << ans.val() << endl; return 0; }