#include const int maxn = 1 << 18; typedef long long ll; const ll mo = 998244353; int n; ll c[maxn], d[maxn], l[maxn], h[maxn], b[maxn], a[maxn]; ll mpow(ll u, ll k) { ll ans = 1; for(; k > 0; k >>= 1, u = u * u % mo) { if(k & 1) { ans = ans * u % mo; } } return ans; } void fwt(const ll *u, ll *v) { memcpy(v, u, sizeof(*u) * n); for(int d = 1; d < n; d *= 2) for(int i = 0; i < n; i += d * 2) for(int j = 0; j < d; ++j) { auto ui = v[i + j], uj = v[i + j + d]; auto &vi = v[i + j], &vj = v[i + j + d]; vi = (ui + uj); vj = (ui - uj); } } int main() { scanf("%d", &n); n = 1 << n; for(int i = 0; i < n; ++i) { scanf("%lld", c + i); } for(int i = 0; i < n; ++i) { scanf("%lld%lld", l + i, h + i); } fwt(c, d); for(int i = 0; i < n; ++i) { if(d[i] >= 0) b[i] = h[i]; else b[i] = l[i]; } fwt(b, a); ll ans = 0ll; for(int i = 0; i < n; ++i) ans = (ans + (a[i] % mo) * (c[i] % mo) % mo) % mo; ans = ans * mpow(n, mo - 2) % mo; ans = (ans + mo) % mo; printf("%lld\n", ans); return 0; }