#include #define REP(i,n) for(int i=0,i##_len=int(n);i>= 1; } return r; } // NTT Core {{{ template < int MAX_H > struct Pool { static ll *tmp, *A, *B; }; template < int MAX_H > ll *Pool< MAX_H >::tmp = new ll[1 << MAX_H]; template < int MAX_H > ll *Pool< MAX_H >::A = new ll[1 << MAX_H]; template < int MAX_H > ll *Pool< MAX_H >::B = new ll[1 << MAX_H]; template < int MAX_H, ll mod, ll primitive > class Core { public: static_assert((mod & ((1 << MAX_H) - 1)) == 1, "mod is too small; comment out"); // ord zetaList[i] = 2^(i + 1) ll zetaList[MAX_H], zetaInvList[MAX_H]; // constexpr Core() { zetaList[MAX_H - 1] = modpow(primitive, (mod - 1) / (1 << MAX_H), mod); zetaInvList[MAX_H - 1] = modinv(zetaList[MAX_H - 1], mod); for(int ih = MAX_H - 2; ih >= 0; --ih) { zetaList[ih] = zetaList[ih + 1] * zetaList[ih + 1] % mod; zetaInvList[ih] = zetaInvList[ih + 1] * zetaInvList[ih + 1] % mod; } } void fft(ll *a, uint n, uint nh, bool inverse) const { ll *tmp = Pool< MAX_H >::tmp; uint mask = n - 1; for(uint i = n >> 1, ih = nh - 1; i >= 1; i >>= 1, --ih) { ll zeta = inverse ? zetaInvList[nh - 1 - ih] : zetaList[nh - 1 - ih]; ll powZeta = 1; for(uint j = 0; j < n; j += i) { for(uint k = 0; k < i; ++k) { tmp[j | k] = (a[((j << 1) & mask) | k] + powZeta * a[(((j << 1) | i) & mask) | k]) % mod; } powZeta = powZeta * zeta % mod; } swap(a, tmp); } if(nh & 1) { swap(a, tmp); for(uint i = 0; i < n; ++i) a[i] = tmp[i]; } if(inverse) { ll invN = modinv(n, mod); for(uint i = 0; i < n; ++i) a[i] = a[i] * invN % mod; } } vector< ll > conv(const vector< ll > &a, const vector< ll > &b) const { uint t = a.size() + b.size() - 1; uint n = 1, nh = 0; while(n < t) n <<= 1, ++nh; return convStrict(a, b, n, nh); } vector< ll > convStrict(const vector< ll > &a, const vector< ll > &b, uint n, uint nh) const { ll *A = Pool< MAX_H >::A, *B = Pool< MAX_H >::B; for(uint i = 0; i < n; ++i) A[i] = B[i] = 0; copy(a.begin(), a.end(), A); copy(b.begin(), b.end(), B); fft(A, n, nh, 0), fft(B, n, nh, 0); for(uint i = 0; i < n; ++i) A[i] = A[i] * B[i] % mod; fft(A, n, nh, 1); return vector< ll >(A, A + n); } }; // Convolution With Garner {{{ template < int MAX_H, int I > class ConvolutionWithGarnerCore { public: static void conv_for(uint n, uint nh, const vector< ll > &a, const vector< ll > &b, vector< ll > &mods, vector< ll > &coeffs, vector< vector< ll > > &constants) { static const Core< MAX_H, NTT_PRIMES[I][0], NTT_PRIMES[I][1] > ntt; auto c = ntt.convStrict(a, b, n, nh); mods[I] = NTT_PRIMES[I][0]; ConvolutionWithGarnerCore< MAX_H, I - 1 >::conv_for( n, nh, a, b, mods, coeffs, constants); // garner for(size_t i = 0; i < c.size(); ++i) { ll v = (c[i] - constants[I][i]) * modinv(coeffs[I], mods[I]) % mods[I]; if(v < 0) v += mods[I]; for(size_t j = I + 1; j < mods.size(); ++j) { constants[j][i] = (constants[j][i] + coeffs[j] * v) % mods[j]; } } for(size_t j = I + 1; j < mods.size(); ++j) { coeffs[j] = (coeffs[j] * mods[I]) % mods[j]; } } }; template < int MAX_H > class ConvolutionWithGarnerCore< MAX_H, -1 > { public: static void conv_for(uint, uint, const vector< ll > &, const vector< ll > &, vector< ll > &, vector< ll > &, vector< vector< ll > > &) {} }; template < int MAX_H > class ConvolutionWithGarner { public: template < int USE > static vector< ll > conv(const vector< ll > &a, const vector< ll > &b, ll mod) { static_assert(USE >= 1, "USE must be positive"); static_assert(USE <= sizeof(NTT_PRIMES) / sizeof(*NTT_PRIMES), "USE is too big"); uint nt = a.size() + b.size() - 1; uint n = 1, nh = 0; while(n < nt) n <<= 1, ++nh; vector< ll > coeffs(USE + 1, 1); vector< vector< ll > > constants(USE + 1, vector< ll >(n)); vector< ll > mods(USE + 1, mod); ConvolutionWithGarnerCore< MAX_H, USE - 1 >::conv_for( n, nh, a, b, mods, coeffs, constants); return constants.back(); } }; } // 1st param is MAX_H NTT::Core< 18, NTT::NTT_PRIMES[0][0], NTT::NTT_PRIMES[0][1] > nttBig; NTT::Core< 18, 998244353, 5 > ntt; using nttconv = NTT::ConvolutionWithGarner< 18 >; // nttconv::conv< USE >(a, b, mod) int main(){ constexpr ll mod=1e9+7; ll p,Q;cin>>p>>Q; vector query(Q); REP(i,Q){ cin>>query[i]; } int N=*max_element(All(query)); vector A(N+1); A[2]=1; rep(i,3,N+1) (A[i]=p*A[i-1]+A[i-2])%=mod; nttconv c; vector C = c.conv<4>(A,A,mod); REP(i,Q) cout<