import sys INF = 1 << 60 MOD = 10**9 + 7 # 998244353 sys.setrecursionlimit(2147483647) input = lambda:sys.stdin.readline().rstrip() def resolve(): p = int(input()) Q = [int(input()) - 2 for _ in range(int(input()))] m = max(Q) dp = [0] * (m + 1) dp[2] = 1 dp[3] = 2 * p % MOD for i in range(4, m + 1): dp[i] = 2 * p * dp[i - 1] - (p**2 - 2) * dp[i - 2] + 2 * p * dp[i - 3] - dp[i - 4] dp[i] %= MOD for i in Q: print(dp[i]) resolve()