#include #include using namespace std; using namespace atcoder; typedef modint998244353 mint; typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } int main(){ int n; cin >> n; vector ikeru(n, vector(0)); rep(i,0,n-1){ int u, v; cin >> u >> v; u--; v--; ikeru[u].push_back(v); ikeru[v].push_back(u); } mint ans = 0; vector a(n); rep(i,0,n)cin>>a[i]; rep(num,0,30){ vector b(n); rep(i,0,n){ if (a[i]>>num&1) b[i] = 1; } vector mada = {~0, 0}; vector tansaku(n); tansaku[0] = 1; vector dp0(n); vector dp1(n); while(!mada.empty()){ int i = mada.back(); mada.pop_back(); if (i >= 0){ for(int j: ikeru[i]){ if (tansaku[j] == 0){ tansaku[j] = 1; mada.push_back(~j); mada.push_back(j); } } }else{ i = ~i; mint tmp0 = 0; mint tmp1 = 0; if (b[i] == 0) tmp0++; else tmp1++; for (int j: ikeru[i]){ if (tansaku[j] == 2){ mint ntmp0 = tmp0 * dp1[j] + tmp1 * dp1[j] + tmp0 * dp0[j]; mint ntmp1 = tmp1 * dp1[j] + tmp0 * dp1[j] + tmp1 * dp0[j]; tmp0 = ntmp0; tmp1 = ntmp1; } } dp0[i] = tmp0; dp1[i] = tmp1; tansaku[i] = 2; } } ans += mint(2).pow(num) * dp1[0]; } cout << ans.val() << endl; }