#include using namespace std; using ll = long long; ll x = 0; struct Graph { struct Vertex { int n; int a; }; struct Edge { int i, n; }; Graph(int n, int m) : v(n, { -1, 0 }), e(m), n(n), m(0) {} void add_edge(int a, int b) { e[m] = { b, v[a].n }; v[a].n = m; m++; } int dfs(int i, int p) { int k = 1, k1[2] = {}; auto f = [&](int i1, int l) { if (v[i].a != v[i1].a) { k1[v[i].a > v[i1].a] += l; x -= (ll)l * l; } }; for (int j = v[i].n; j >= 0; j = e[j].n) { Edge &o = e[j]; if (o.i == p) continue; int l = dfs(o.i, i); k += l; f(o.i, l); } if (p >= 0) f(p, n - k); x += (ll)k1[0] * k1[0] + (ll)k1[1] * k1[1]; return k; } vector v; vector e; int n, m; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; Graph g(n, (n - 1) * 2); for (int j = 0; j < n - 1; j++) { int u, v; cin >> u >> v; u--; v--; g.add_edge(u, v); g.add_edge(v, u); } for (int i = 0; i < n; i++) { cin >> g.v[i].a; } g.dfs(0, -1); cout << x / 2 % 998244353 << endl; return 0; }