/* -*- coding: utf-8 -*- * * 2377.cc: No.2377 SUM AND XOR on Tree - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int BN = 30; const int MOD = 998244353; /* typedef */ using vi = vector; template struct MI { int v; MI(): v() {} MI(int _v): v(_v % MOD) { if (v < 0) v += MOD; } MI(long long _v): v(_v % MOD) { if (v < 0) v += MOD; } explicit operator int() const { return v; } MI operator+(const MI m) const { return MI(v + m.v); } MI operator-(const MI m) const { return MI(v + MOD - m.v); } MI operator*(const MI m) const { return MI((long long)v * m.v); } MI &operator+=(const MI m) { return (*this = *this + m); } MI &operator-=(const MI m) { return (*this = *this - m); } MI &operator*=(const MI m) { return (*this = *this * m); } bool operator==(const MI m) const { return v == m.v; } bool operator!=(const MI m) const { return v != m.v; } MI pow(int n) const { // a^n % MOD MI pm = 1, a = *this; while (n > 0) { if (n & 1) pm *= a; a *= a; n >>= 1; } return pm; } MI inv() const { return pow(MOD - 2); } MI operator/(const MI m) const { return *this * m.inv(); } MI &operator/=(const MI m) { return (*this = *this / m); } }; using mi = MI; /* global variables */ vi nbrs[MAX_N]; int as[MAX_N]; int ps[MAX_N], cis[MAX_N]; mi ss0[MAX_N], ss1[MAX_N]; /* subroutines */ /* main */ int main() { int n; scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); u--, v--; nbrs[u].push_back(v); nbrs[v].push_back(u); } for (int i = 0; i < n; i++) scanf("%d", as + i); mi sum = 0; for (int i = 0; i < BN; i++) { fill(cis, cis + n, 0); fill(ss0, ss0 + n, 1); fill(ss1, ss1 + n, 0); ps[0] = -1; for (int u = 0; u >= 0;) { auto &nbru = nbrs[u]; int up = ps[u]; if (cis[u] < nbru.size()) { int v = nbru[cis[u]++]; if (v != up) { ps[v] = u; u = v; } } else { if ((as[u] >> i) & 1) swap(ss0[u], ss1[u]); if (up >= 0) { mi s0 = ss0[up] * ss0[u] + ss1[up] * ss1[u] + ss0[up] * ss1[u]; mi s1 = ss0[up] * ss1[u] + ss1[up] * ss0[u] + ss1[up] * ss1[u]; ss0[up] = s0, ss1[up] = s1; } u = up; } } sum += ss1[0] * (1 << i); //printf(" i=%d: s=%d\n", i, ss1[0]); } printf("%d\n", (int)sum); return 0; }