#include using namespace std; template vector operator-(vector a) { for (auto&& e : a) e = -e; return a; } template vector& operator+=(vector& l, const vector& r) { l.resize(max(l.size(), r.size())); for (int i = 0; i < (int)r.size(); ++i) l[i] += r[i]; return l; } template vector operator+(vector l, const vector& r) { return l += r; } template vector& operator-=(vector& l, const vector& r) { l.resize(max(l.size(), r.size())); for (int i = 0; i < (int)r.size(); ++i) l[i] -= r[i]; return l; } template vector operator-(vector l, const vector& r) { return l -= r; } template vector& operator<<=(vector& a, size_t n) { return a.insert(begin(a), n, 0), a; } template vector operator<<(vector a, size_t n) { return a <<= n; } template vector& operator>>=(vector& a, size_t n) { return a.erase(begin(a), begin(a) + min(a.size(), n)), a; } template vector operator>>(vector a, size_t n) { return a >>= n; } template vector operator*(const vector& l, const vector& r) { if (l.empty() or r.empty()) return {}; vector res(l.size() + r.size() - 1); for (int i = 0; i < (int)l.size(); ++i) for (int j = 0; j < (int)r.size(); ++j) res[i + j] += l[i] * r[j]; return res; } template vector& operator*=(vector& l, const vector& r) { return l = l * r; } template vector inverse(const vector& a) { assert(not a.empty() and not (a[0] == 0)); vector b{1 / a[0]}; while (b.size() < a.size()) { vector x(begin(a), begin(a) + min(a.size(), 2 * b.size())); x *= b * b; b.resize(2 * b.size()); for (auto i = b.size() / 2; i < min(x.size(), b.size()); ++i) b[i] = -x[i]; } return {begin(b), begin(b) + a.size()}; } template vector operator/(vector l, vector r) { if (l.size() < r.size()) return {}; reverse(begin(l), end(l)), reverse(begin(r), end(r)); int n = l.size() - r.size() + 1; l.resize(n), r.resize(n); l *= inverse(r); return {rend(l) - n, rend(l)}; } template vector& operator/=(vector& l, const vector& r) { return l = l / r; } template vector operator%(vector l, const vector& r) { if (l.size() < r.size()) return l; l -= l / r * r; return {begin(l), begin(l) + (r.size() - 1)}; } template vector& operator%=(vector& l, const vector& r) { return l = l % r; } template vector derivative(const vector& a) { vector res(max((int)a.size() - 1, 0)); for (int i = 0; i < (int)res.size(); ++i) res[i] = (i + 1) * a[i + 1]; return res; } template vector primitive(const vector& a) { vector res(a.size() + 1); for (int i = 1; i < (int)res.size(); ++i) res[i] = a[i - 1] / i; return res; } template vector logarithm(const vector& a) { assert(not a.empty() and a[0] == 1); auto res = primitive(derivative(a) * inverse(a)); return {begin(res), begin(res) + a.size()}; } template vector exponent(const vector& a) { assert(a.empty() or a[0] == 0); vector b{1}; while (b.size() < a.size()) { vector x(begin(a), begin(a) + min(a.size(), 2 * b.size())); x[0] += 1; b.resize(2 * b.size()); x -= logarithm(b); x *= {begin(b), begin(b) + b.size() / 2}; for (auto i = b.size() / 2; i < min(x.size(), b.size()); ++i) b[i] = x[i]; } return {begin(b), begin(b) + a.size()}; } template vector berlekamp_massey(const vector& a) { T d = 1; vector c{1}, nc{1}; int n = a.size(), k = 0, m = 1; for (int i = 0; i < n; ++i) { T nd = inner_product(rbegin(nc), rend(nc), begin(a) + (i - k), (T)0); if (nd == 0) ++m; else if (2 * k <= i) { auto t = nc; nc -= vector{nd / d} * (c << m); c = t, d = nd; k = i + 1 - k, m = 1; } else { nc -= vector{nd / d} * (c << m); ++m; } } return {rbegin(nc), rend(nc)}; } template > T power(T a, long long n, F op = multiplies(), T e = {1}) { assert(n >= 0); T res = e; while (n) { if (n & 1) res = op(res, a); if (n >>= 1) a = op(a, a); } return res; } template struct Modular { using M = Modular; unsigned v; Modular(long long a = 0) : v((a %= Mod) < 0 ? a + Mod : a) {} M operator-() const { return M() -= *this; } M& operator+=(M r) { if ((v += r.v) >= Mod) v -= Mod; return *this; } M& operator-=(M r) { if (v < r.v) v += Mod; v -= r.v; return *this; } M& operator*=(M r) { v = (uint64_t)v * r.v % Mod; return *this; } M& operator/=(M r) { return *this *= power(r, Mod - 2); } friend M operator+(M l, M r) { return l += r; } friend M operator-(M l, M r) { return l -= r; } friend M operator*(M l, M r) { return l *= r; } friend M operator/(M l, M r) { return l /= r; } friend bool operator==(M l, M r) { return l.v == r.v; } }; constexpr long long mod = 1e9 + 7; using Mint = Modular; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int p; cin >> p; vector a(8); for (int i = 0; i < 8; ++i) { if (i < 2) { a[i] = i; } else { a[i] = p * a[i - 1] + a[i - 2]; } } a *= a; a.resize(8); auto c = berlekamp_massey(a); while (a.size() < 2e6) { int n = a.size(); Mint an; for (int i = 0; i < 4; ++i) { an -= c[i] * a[n - 4 + i]; } a.push_back(an); } int q; cin >> q; while (q--) { int i; cin >> i; i -= 2; cout << a[i].v << '\n'; } }