def pow_mod(a, x, mod): res = 1 a %= mod while x > 0: if x & 1 == 1: res = (res * a) % mod a = (a * a) % mod x >>= 1 return res def mod_inv(a, mod): def extended_gcd(a, b): if b == 0: return a, 1, 0 gcd, x1, y1 = extended_gcd(b, a % b) x = y1 y = x1 - (a // b) * y1 return gcd, x, y gcd, x, _ = extended_gcd(a % mod, mod) if gcd != 1: return None return (x % mod + mod) % mod def geometric_sum(n, k, mod): n %= mod if n == 0: return 1 % mod if n == 1: return (k + 1) % mod inv = mod_inv((n - 1) % mod, mod) if inv is None: result = 0 power = 1 for _ in range(min(k + 1, mod)): result = (result + power) % mod power = (power * n) % mod return result numerator = (pow_mod(n, k + 1, mod) - 1 + mod) % mod return (numerator * inv) % mod def chinese_remainder_theorem(a1, a2): inv2 = mod_inv(2, 499) t = ((a2 - a1) * inv2) % 499 return (a1 + 2 * t) % 998 def main(): t = int(input()) for _ in range(t): n, m = map(int, input().split()) for _ in range(m): k = int(input()) result_mod2 = geometric_sum(n, k, 2) result_mod499 = geometric_sum(n, k, 499) result = chinese_remainder_theorem(result_mod2, result_mod499) print(result) if __name__ == "__main__": main()