#include using namespace std; using P = pair; const int M = 1000000007; int main() { cin.tie(0); ios::sync_with_stdio(false); int p, q; cin >> p >> q; int n = 2000010; vector a(n), s(n); a[1] = 1; for (int i = 2; i < n; i++) { a[i] = (p * a[i - 1] + a[i - 2]) % M; s[i] = ((p * s[i - 1] + s[i - 2] + a[i - 1]) % M + M) % M; } for (int i = 0; i < q; i++) { int x; cin >> x; cout << s[x - 2] << '\n'; } return 0; }