import sys INF = 1 << 60 MOD = 10**9 + 7 # 998244353 sys.setrecursionlimit(2147483647) input = lambda:sys.stdin.readline().rstrip() def linear_recurrence(A): n = len(A) C, pC = [1], [0] m, pd = 0, 0 for i in range(n): m += 1 d = sum(c * A[i - j] % MOD for j, c in enumerate(C)) % MOD if d == 0: continue q = pow(pd, MOD - 2, MOD) * d % MOD if len(C) < len(pC) + m: T = C[:] C += [0] * (len(pC) + m - len(C)) for j, v in enumerate(pC): C[j + m] = (C[j + m] - q * v) % MOD pC = T m, pd = 0, d else: for j, v in enumerate(pC): C[j + m] = (C[j + m] - q * v) % MOD return [-v % MOD for v in C[1:]] def resolve(): p = int(input()) dp = [0] * 10 dp[1] = 1 for i in range(2, len(dp)): dp[i] = (p * dp[i - 1] + dp[i - 2]) % MOD dp2 = [0] * (len(dp) * 2 - 1) for i in range(len(dp)): for j in range(len(dp)): dp2[i + j] += dp[i] * dp[j] dp2[i + j] %= MOD C = linear_recurrence(dp2[:len(dp)]) l = len(C) M = 2 * 10**6 dp = [0] * M dp[:l] = dp2[:l] for i in range(l, M): dp[i] = sum(c * dp[i - j] % MOD for j, c in enumerate(C, 1)) % MOD for _ in range(int(input())): print(dp[int(input()) - 2]) resolve()