// Validate Input by testlib.h // clang-format off #include using namespace std; using ll = long long; #if __has_include("testlib.h") #include "testlib.h" #else #include "/opt/testlib/testlib.h" #endif #include #include using namespace atcoder; using mint = modint998244353; // clang-format on int main(int argc, char* argv[]) { // -------------------- Validate Input -------------------- // Ref: https://github.com/MikeMirzayanov/testlib/blob/master/validators/undirected-tree-validator.cpp registerValidation(argc, argv); int N = inf.readInt(2, (int)1e5, "N"); inf.readChar('\n'); dsu uf(N); set> edges; vector> G(N); for (int i = 0; i < N - 1; i++) { int u = inf.readInt(1, N, "u_i"); inf.readSpace(); int v = inf.readInt(1, N, "v_i"); inf.readChar('\n'); ensuref(u != v, "Tree can't contain loops"); ensuref(edges.count(make_pair(u, v)) == 0, "Tree can't contain multiple edges between a pair of vertices"); edges.insert(make_pair(u, v)); edges.insert(make_pair(v, u)); ensuref(not uf.same(u - 1, v - 1), "Tree can't contain cycles"); uf.merge(u - 1, v - 1); G[u - 1].push_back(v - 1); G[v - 1].push_back(u - 1); } ensuref(uf.size(0) == N, "Tree must be a connected graph"); vector A(N); const int MAX_A = (1 << 30) - 1; for (int i = 0; i < N; i++) { A[i] = inf.readInt(0, MAX_A, "A_i"); if (i < N - 1) { inf.readSpace(); } else { inf.readChar('\n'); } } inf.readEof(); // -------------------- Solve -------------------- mint ans = 0; for (int b = 0; b < 30; b++) { auto dfs = [&](auto&& self, int u, int p) -> vector { vector dp(2); dp[(A[u] >> b) & 1]++; for (int v : G[u]) { if (v == p) { continue; } auto dp_v = self(self, v, u); vector dp_new(2); // 辺 (u, v) を削除する dp_new[0] += dp[0] * dp_v[1]; dp_new[1] += dp[1] * dp_v[1]; // 辺 (u, v) を削除しない(繋ぐ) dp_new[0] += dp[0] * dp_v[0]; dp_new[1] += dp[0] * dp_v[1]; dp_new[1] += dp[1] * dp_v[0]; dp_new[0] += dp[1] * dp_v[1]; swap(dp, dp_new); } return dp; }; auto dp = dfs(dfs, 0, -1); ans += (1 << b) * dp[1]; } cout << ans.val() << '\n'; return 0; }