#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
long long mod = 1e9 + 7;
long long dp[maxn * 3][3];
long long nv[maxn * 3];
long long qpow(long long a, long long n) {
  long long ans = 1;
  while (n) {
    if (n & 1) ans = ans * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ans;
}
long long a[maxn * 3];
int main() {
  int n, q, x;
  long long inv = qpow(3, mod - 2);
  scanf("%d%d", &n, &q);
  long long sum = 3 * n;
  a[0] = 1;
  a[1] = sum;
  dp[0][0] = n;
  dp[0][1] = n;
  dp[0][2] = n;
  nv[1] = 1;
  for (int i = 2; i <= 3 * n; i++) {
    nv[i] = mod - mod / i * nv[mod % i] % mod;
  }
  for (int i = 1; i < 3 * n; i++) {
    sum = sum * (3 * n - i) % mod * nv[i + 1] % mod;
    a[i + 1] = sum;
    dp[i][0] =
        (sum - 2 * dp[i - 1][0] - dp[i - 1][1] + 3 * mod) % mod * inv % mod;
    dp[i][1] = (dp[i][0] + dp[i - 1][0]) % mod;
  }
  while (q--) {
    scanf("%d", &x);
    if (x == 3 * n) {
      cout << 1 << '\n';
      continue;
    }
    printf("%lld\n", (dp[x][0] + a[x]) % mod);
  }
  return 0;
}
