#include using namespace std; typedef long long ll; typedef pair l_l; typedef pair i_i; template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } const long double EPS = 1e-10; const long long INF = 1e18; const long double PI = acos(-1.0L); const ll mod = 1000000007; ll p; ll P[2000100]; ll a[2000010]; int main() { //cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); cin >> p; a[2] = 1; for(int i = 3; i <= 2e6; i++) { a[i] = p * a[i-1] + a[i-2]; a[i] %= mod; } P[1] = 0; P[2] = 0; P[3] = 0; P[4] = 1; for(int i = 5; i <= 2e6; i++) { P[i] = P[i-1] * p + P[i-2] + a[i-2]; P[i] %= mod; } int Q; cin >> Q; while(Q--) { int q; cin >> q; cout << P[q] << endl; } return 0; }