/+ dub.sdl: name "A" dependency "dcomp" version=">=0.7.4" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.numeric.convolution; // import dcomp.modint; // import dcomp.modpoly; immutable int MD = 10^^9 + 7; alias Mint = ModInt!MD; alias MPol = ModPoly!MD; int main() { auto po = [ 0, 32, 316, 2292, 14422, 84744, 479004, 2638328, 14258574, 75940592, 399782668, 84795558, 786749020, 442043859, 352536615, 76576421, 744912747, 420315017, 25759333, 562730793, 424899366, 153177921, 250747498, 306910436, 324829483, 572545341, 104022619, 226237183, 421453002, 754280938, 291624319, 60437277, 297658752, 677142927, 63550828, 801541292, 683008492, 650348, 519624175, 715484025, 724658778, 152363657, 280344328, 892278238, 206785631, 227202296, 788486407, 392284243, 927772200, 781378846, 881515964, 905982211, 674841192, 139044658, 711210295, 384364637, 137653614, 441363040, 812818651, 929556368, 494420762, 802527485, 700803632, 461521718, 152786116, 688977792, 48724029, 642700933, 15567410, 246397043, 859581827, 685250826, 120226158, 551687712, 987163282, 422276494, 570671107, 813070470, 429968009, 849487586, 453150672, 606112895, 921636591, 961537190, 68995663, 873642098, 377371441, 101407285, 187434129, 180415383, 920712750, 544594592, 402649575, 549811430, 619506771, 426917748, 274013175, 615469131, 800944525, 925880089, ].map!(x => Mint(x)).array; auto pol = MPol(po); auto v = berlekampMassey(po); // writeln(v); long x; sc.read(x); auto z = nthMod(x, v); Mint sm = 0; foreach (i, d; po) { sm += d * z[i]; } writeln(sm); // for (int i: rng(int(v.size()))) { // sm += po[i] * z.freq(i); // } // cout << to_string(z) << endl; // cout << sm.v << endl;*/ return 0; } Scanner sc; static this() { sc = new Scanner(stdin); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/modpoly.d */ // module dcomp.modpoly; // import dcomp.numeric.primitive; // import dcomp.numeric.convolution; // import dcomp.modint; // import dcomp.container.stack; struct ModPoly(uint MD) if (MD < int.max) { alias Mint = ModInt!MD; import std.algorithm : min, max, reverse; Stack!Mint d; void shrink() { while (!d.empty && d.back == Mint(0)) d.removeBack; } @property size_t length() const { return d.length; } @property inout(Mint)[] data() inout { return d.data; } this(in Mint[] v) { d = v.dup; shrink(); } const(Mint) opIndex(size_t i) const { if (i < d.length) return d[i]; return Mint(0); } void opIndexAssign(Mint x, size_t i) { if (i < d.length) { d[i] = x; shrink(); return; } if (x == Mint(0)) return; while (d.length < i) d.insertBack(Mint(0)); d.insertBack(x); return; } ModPoly opBinary(string op : "+")(in ModPoly r) const { size_t N = length, M = r.length; Mint[] res = new Mint[max(N, M)]; foreach (i; 0..max(N, M)) res[i] = this[i] + r[i]; return ModPoly(res); } ModPoly opBinary(string op : "-")(in ModPoly r) const { size_t N = length, M = r.length; Mint[] res = new Mint[max(N, M)]; foreach (i; 0..max(N, M)) res[i] = this[i] - r[i]; return ModPoly(res); } ModPoly opBinary(string op : "*")(in ModPoly r) const { size_t N = length, M = r.length; if (min(N, M) == 0) return ModPoly(); return ModPoly(multiply(data, r.data)); } ModPoly opBinary(string op : "*")(in Mint r) const { Mint[] res = new Mint[length]; foreach (i; 0..length) res[i] = this[i]*r; return ModPoly(res); } ModPoly opBinary(string op : "/")(in ModPoly r) const { size_t B = max(1, length, r.length); return divWithInv(r.inv(B), B); } ModPoly opBinary(string op : "%")(in ModPoly r) const { return *this - y * div(y); } ModPoly opBinary(string op : "<<")(size_t n) const { Mint[] res = new Mint[n+length]; foreach (i; 0..length) res[i+n] = this[i]; return ModPoly(res); } ModPoly opBinary(string op : ">>")(size_t n) const { if (length <= n) return ModPoly(); Mint[] res = new Mint[length-n]; foreach (i; n..length) res[i-n] = this[i]; return ModPoly(res); } ModPoly opOpAssign(string op)(in ModPoly r) { return mixin("this=this"~op~"r"); } ModPoly strip(size_t n) const { auto res = d.data.dup; res = res[0..min(n, length)]; return ModPoly(res); } ModPoly divWithInv(in ModPoly ir, size_t B) const { return (this * ir) >> (B-1); } ModPoly remWithInv(in ModPoly r, in ModPoly ir, size_t B) const { return this - r * divWithInv(ir, B); } ModPoly rev(ptrdiff_t n = -1) const { auto res = d.data.dup; if (n != -1) res = res[0..n]; reverse(res); return ModPoly(res); } ModPoly inv(size_t n) const { assert(length >= 1); assert(n >= length-1); ModPoly c = rev(); ModPoly d = ModPoly([Mint(1)/c[0]]); for (ptrdiff_t i = 1; i+length-2 < n; i *= 2) { d = (d * (ModPoly([Mint(2)]) - c*d)).strip(2*i); } return d.rev(n+1-length); } string toString() { import std.conv : to; import std.range : join; string[] l = new string[length]; foreach (i; 0..length) { l[i] = (this[i]).toString ~ "x^" ~ i.to!string; } return l.join(" + "); } } ModPoly!MD nthMod(uint MD)(ulong n, in ModPoly!MD mod) { import core.bitop : bsr; alias Mint = ModInt!MD; assert(mod.length); size_t B = mod.length * 2 - 1; auto modInv = mod.inv(B); auto p = ModPoly!MD([Mint(1)]); if (n == 0) return p; auto m = bsr(n); foreach_reverse(i; 0..m+1) { if (n & (1L< a[n-1]*T(n)).take(length).array; } T[] invFactTable(T)(size_t length) if (isModInt!T) { import std.algorithm : map, reduce; import std.range : take, recurrence, iota; import std.array : array; auto res = new T[length]; res[$-1] = T(1) / iota(1, length).map!T.reduce!"a*b"; foreach_reverse (i, v; res[0..$-1]) { res[i] = res[i+1] * T(i+1); } return res; } T[] invTable(T)(size_t length) if (isModInt!T) { auto f = factTable!T(length); auto invf = invFactTable!T(length); auto res = new T[length]; foreach (i; 1..length) { res[i] = invf[i] * f[i-1]; } return res; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/numeric/convolution.d */ // module dcomp.numeric.convolution; T[] zeta(T)(T[] v, bool rev) { import core.bitop : bsr; int n = bsr(v.length); assert(1<>1] = l-r; } } swap(a, b); } c[] = a[]; } double[] multiply(in double[] a, in double[] b) { alias P = Complex!double; size_t A = a.length, B = b.length; if (!A || !B) return []; size_t lg = 1; while ((1<>1] = l-r; } now *= base; } swap(a, b); } c[] = a[]; } Mint[] multiply(uint G, Mint)(in Mint[] a, in Mint[] b) { size_t A = a.length, B = b.length; if (!A || !B) return []; size_t lg = 1; while ((1<> (ph*W)) % (1<> (ph*W)) % (1<>= 1; } return r % md; } ulong ulongPowMod(U)(ulong x, U n, ulong md) if (isIntegral!U || is(U == BigInt)) { // import dcomp.int128; x %= md; ulong r = 1; while (n) { if (n & 1) { r = mul128(r, x).mod128(md); } x = mul128(x, x).mod128(md); n >>= 1; } return r % md; } T lcm(T)(in T a, in T b) { import std.numeric : gcd; return a / gcd(a,b) * b; } T[3] extGcd(T)(in T a, in T b) if (!isIntegral!T || isSigned!T) { if (b==0) { return [T(1), T(0), a]; } else { auto e = extGcd(b, a%b); return [e[1], e[0]-a/b*e[1], e[2]]; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/ldc/inline.d */ // module dcomp.ldc.inline; version(LDC) { pragma(LDC_inline_ir) R inlineIR(string s, R, P...)(P); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.container.stack; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } import core.bitop : popcnt; static if (!__traits(compiles, popcnt(ulong.max))) { public import core.bitop : popcnt; int popcnt(ulong v) { return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32)); } } bool poppar(ulong v) { v^=v>>1; v^=v>>2; v&=0x1111111111111111UL; v*=0x1111111111111111UL; return ((v>>60) & 1) != 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/stack.d */ // module dcomp.container.stack; struct StackPayload(T, size_t MIN = 4) { import core.exception : RangeError; import std.algorithm : max; import std.conv; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private T* _data; private uint len, cap; bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { return _data[i]; } ref inout(T) front() inout { return _data[0]; } ref inout(T) back() inout { assert(len); return _data[len-1]; } void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen.to!uint; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void free() { import core.memory : GC; GC.free(_data); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(MIN, cap*2)); } _data[len++] = item; } void insertBack(T item) { this ~= item; } void removeBack() { len--; } void clear() { len = 0; } inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } static struct RangeT(A) { import std.traits : CopyTypeQualifiers; alias E = CopyTypeQualifiers!(A, T); A *p; size_t a, b; @property bool empty() const { return b <= a; } @property size_t length() const { return b-a; } @property RangeT save() { return RangeT(p, a, b); } @property RangeT!(const A) save() const { return typeof(return)(p, a, b); } alias opDollar = length; @property ref inout(E) front() inout { return (*p)[a]; } @property ref inout(E) back() inout { return (*p)[b-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); a++; } void popBack() { version(assert) if (empty) throw new RangeError(); b--; } ref inout(E) opIndex(size_t i) inout { return (*p)[i]; } RangeT opSlice() { return this.save; } RangeT opSlice(size_t i, size_t j) { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } RangeT!(const A) opSlice() const { return this.save; } RangeT!(const A) opSlice(size_t i, size_t j) const { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } } alias Range = RangeT!(StackPayload!T); alias ConstRange = RangeT!(const StackPayload!T); alias ImmutableRange = RangeT!(immutable StackPayload!T); } struct Stack(T) { import core.exception : RangeError; import core.memory : GC; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; alias Payload = StackPayload!T; alias Range = Payload.Range; alias ConstRange = Payload.ConstRange; alias ImmutableRange = Payload.ImmutableRange; Payload* p; private void I() { if (!p) p = new Payload(); } private void C() const { version(assert) if (!p) throw new RangeError(); } private this(Payload* p) { this.p = p; } this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) { p = new Payload(); foreach (v; values) { insertBack(v); } } this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) { p = new Payload(); foreach (v; r) { insertBack(v); } } static Stack make() { return Stack(new Payload()); } @property bool havePayload() const { return (p !is null); } @property bool empty() const { return (!havePayload || p.empty); } @property size_t length() const { return (havePayload ? p.length : 0); } @property inout(T)[] data() inout {C; return (!p) ? [] : p.data; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void clear() { if (p) p.clear(); } void insertBack(T v) {I; p.insertBack(v); } alias stableInsertBack = insertBack; void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/int128.d */ // module dcomp.int128; // import dcomp.array; version(LDC) { // import dcomp.ldc.inline; } version(LDC) version(X86_64) { version = LDC_IR; } ulong[2] mul128(ulong a, ulong b) { ulong[2] res; version(LDC_IR) { ulong upper, lower; inlineIR!(` %r0 = zext i64 %0 to i128 %r1 = zext i64 %1 to i128 %r2 = mul i128 %r1, %r0 %r3 = trunc i128 %r2 to i64 %r4 = lshr i128 %r2, 64 %r5 = trunc i128 %r4 to i64 store i64 %r3, i64* %2 store i64 %r5, i64* %3`, void)(a, b, &lower, &upper); return [lower, upper]; } else version(D_InlineAsm_X86_64) { ulong upper, lower; asm { mov RAX, a; mul b; mov lower, RAX; mov upper, RDX; } return [lower, upper]; } else { ulong B = 2UL^^32; ulong[2] a2 = [a % B, a / B]; ulong[2] b2 = [b % B, b / B]; ulong[4] c; foreach (i; 0..2) { foreach (j; 0..2) { c[i+j] += a2[i] * b2[j] % B; c[i+j+1] += a2[i] * b2[j] / B; } } foreach (i; 0..3) { c[i+1] += c[i] / B; c[i] %= B; } return [c[0] + c[1] * B, c[2] + c[3] * B]; } } ulong div128(ulong[2] a, ulong b) { version(LDC_IR) { return inlineIR!(` %r0 = zext i64 %0 to i128 %r1 = zext i64 %1 to i128 %r2 = shl i128 %r1, 64 %r3 = add i128 %r0, %r2 %r4 = zext i64 %2 to i128 %r5 = udiv i128 %r3, %r4 %r6 = trunc i128 %r5 to i64 ret i64 %r6`,ulong)(a[0], a[1], b); } else version(D_InlineAsm_X86_64) { ulong upper = a[1], lower = a[0]; ulong res; asm { mov RDX, upper; mov RAX, lower; div b; mov res, RAX; } return res; } else { if (b == 1) return a[0]; while (!(b & (1UL << 63))) { a[1] <<= 1; if (a[0] & (1UL << 63)) a[1] |= 1; a[0] <<= 1; b <<= 1; } ulong ans = 0; foreach (i; 0..64) { bool up = (a[1] & (1UL << 63)) != 0; a[1] <<= 1; if (a[0] & (1UL << 63)) a[1] |= 1; a[0] <<= 1; ans <<= 1; if (up || b <= a[1]) { a[1] -= b; ans++; } } return ans; } } ulong mod128(ulong[2] a, ulong b) { version(D_InlineAsm_X86_64) { ulong upper = a[1], lower = a[0]; ulong res; asm { mov RDX, upper; mov RAX, lower; div b; mov res, RDX; } return res; } else { return a[0] - div128(a, b) * b; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */ // module dcomp.array; T[N] fixed(T, size_t N)(T[N] a) {return a;} /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */