#include int ri() { int n; scanf("%d", &n); return n; } template struct NTT { int get_mod() { return mod; } int pow(int a, int b) { int res = 1; for (; b; b >>= 1) { if (b & 1) res = (int64_t) res * a % mod; a = (int64_t) a * a % mod; } return res; } int inv(int i) { return pow(i, mod - 2); } void ntt(std::vector &a, bool inverse) { int n = a.size(); assert((n & -n) == n); int h = pow(proot, (mod - 1) / n); if (inverse) h = inv(h); for (int i = 0, j = 1; j < n - 1; j++) { for (int k = n >> 1; k > (i ^= k); k >>= 1); if (j < i) std::swap(a[i], a[j]); } for (int i = 1; i < n; i <<= 1) { int base = pow(h, n / i / 2); int w = 1; std::vector ws(i); for (int j = 0; j < i; j++) ws[j] = w, w = (int64_t) w * base % mod; for (int j = 0; j < n; j += i << 1) { for (int k = 0; k < i; k++) { int u = a[k + j]; int d = (int64_t) a[k + j + i] * ws[k] % mod; a[k + j] = u + d >= mod ? u + d - mod : u + d; a[k + j + i] = d > u ? u + mod - d : u - d; } } } if (inverse) { int ninv = inv(a.size()); for (auto &i : a) i = (int64_t) i * ninv % mod; } } std::vector conv(const std::vector a_, const std::vector b_) { std::vector a = a_, b = b_; size_t size = 1; for (; size < a_.size() + b_.size(); size <<= 1); a.resize(size); b.resize(size); ntt(a, false); ntt(b, false); for (size_t i = 0; i < size; i++) a[i] = (int64_t) a[i] * b[i] % mod; ntt(a, true); a.resize(a_.size() + b_.size() - 1); return a; } }; template int64_t inv(int64_t a) { a %= mod; int res = 1; for (int x = mod - 2; x; x >>= 1) { if (x & 1) res = (int64_t) res * a % mod; a = (int64_t) a * a % mod; } return res; } #define MOD 1000000007 // copied from https://math314.hateblo.jp/entry/2015/05/07/014908 std::vector conv_mod(std::vector a, std::vector b){ NTT<935329793, 3> ntt1; NTT<943718401, 7> ntt2; NTT<998244353, 3> ntt3; auto x_ = ntt1.conv(a, b); auto y_ = ntt2.conv(a, b); auto z_ = ntt3.conv(a, b); std::vector x, y, z; for (auto i : x_) x.push_back(i); for (auto i : y_) y.push_back(i); for (auto i : z_) z.push_back(i); // garnerのアルゴリズムを極力高速化した constexpr int64_t m1 = 935329793, m2 = 943718401, m3 = 998244353; const int64_t m1_inv_m2 = inv(m1); const int64_t m12_inv_m3 = inv(m1 * m2); const int64_t m12_mod = m1 * m2 % MOD; std::vector ret(x.size()); for (int i = 0; i < (int) x.size(); i++) { int64_t v1 = (y[i] - x[i]) * m1_inv_m2 % m2; if (v1 < 0) v1 += m2; int64_t v2 = (z[i] - (x[i] + m1 * v1) % m3) * m12_inv_m3 % m3; if (v2 < 0) v2 += m3; int64_t constants3 = (x[i] + m1 * v1 + m12_mod * v2) % MOD; if (constants3 < 0) constants3 += MOD; ret[i] = constants3; } return ret; } int main() { int p = ri(); std::vector a(2000001); a[1] = 0; a[2] = 1; for (int i = 3; i <= 2000000; i++) a[i] = ((int64_t) a[i - 1] * p + a[i - 2]) % MOD; auto res = conv_mod(a, a); int q = ri(); for (int i = 0; i < q; i++) { printf("%d\n", (int) res[ri()]); } return 0; }