n = int(input()) s = 1 << n C = list(map(int, input().split())) L = [0] * s H = [0] * s for i in range(s): L[i], H[i] = map(int, input().split()) MOD = 998244353 for k in range(n): d = 1 << k m = d << 1 for i in range(0, s, m): for j in range(d): x = C[i + j + 0] y = C[i + j + d] C[i + j + 0] = x + y C[i + j + d] = x - y ans = 0 for i in range(s): if C[i] < 0: ans += C[i] * L[i] % MOD else: ans += C[i] * H[i] % MOD ans %= MOD print(ans * pow(s, MOD - 2, MOD) % MOD)