#include using namespace std; const long long MOD = 998244353; vector make(int x){ vector A(1001, 0); A[x] = 1; return A; } vector operator +(vector A, vector B){ vector C(1001); for (int i = 0; i <= 1000; i++){ C[i] = (A[i] + B[i]) % MOD; } return C; } vector operator *(vector A, long long x){ for (int i = 0; i <= 1000; i++){ A[i] *= x; A[i] %= MOD; } return A; } vector max_convolution(vector A, vector B){ for (int i = 0; i < 1000; i++){ A[i + 1] += A[i]; A[i + 1] %= MOD; B[i + 1] += B[i]; B[i + 1] %= MOD; } vector C(1001); for (int i = 0; i <= 1000; i++){ C[i] = A[i] * B[i] % MOD; } for (int i = 999; i >= 0; i--){ C[i + 1] += MOD - C[i]; C[i + 1] %= MOD; } return C; } int main(){ int N; cin >> N; vector A(N); for (int i = 0; i < N; i++){ cin >> A[i]; } vector> E(N); for (int i = 0; i < N - 1; i++){ int u, v; cin >> u >> v; u--; v--; E[u].push_back(v); E[v].push_back(u); } vector POW(N); POW[0] = 1; for (int i = 0; i < N - 1; i++){ POW[i + 1] = POW[i] * 2 % MOD; } vector p(N, -1); vector> c(N); queue Q; Q.push(0); vector bfs; while (!Q.empty()){ int v = Q.front(); Q.pop(); bfs.push_back(v); for (int w : E[v]){ if (w != p[v]){ p[w] = v; c[v].push_back(w); Q.push(w); } } } reverse(bfs.begin(), bfs.end()); vector sz(N, 1); for (int v : bfs){ for (int w : c[v]){ sz[v] += sz[w]; } } vector> dp1(N, vector(1001, 0)); for (int v : bfs){ dp1[v] = make(A[v]); for (int w : c[v]){ dp1[v] = max_convolution(dp1[v], dp1[w]) + dp1[v] * POW[sz[w] - 1]; } } reverse(bfs.begin(), bfs.end()); vector> dp2(N, vector(1001, 0)); dp2[0][0] = 1; for (int v : bfs){ int cnt = c[v].size(); vector> L(cnt + 1, vector(1001, 0)); L[0][0] = 1; for (int i = 0; i < cnt; i++){ int w = c[v][i]; L[i + 1] = max_convolution(L[i], dp1[w]) + L[i] * POW[sz[w] - 1]; } vector> R(cnt + 1, vector(1001, 0)); R[cnt][0] = 1; for (int i = cnt - 1; i >= 0; i--){ int w = c[v][i]; R[i] = max_convolution(R[i + 1], dp1[w]) + R[i + 1] * POW[sz[w] - 1]; } for (int i = 0; i < cnt; i++){ int w = c[v][i]; dp2[w] = max_convolution(dp2[v], max_convolution(L[i], R[i + 1])); dp2[w] = max_convolution(dp2[w], make(A[w])) + make(A[w]) * POW[N - 1 - sz[w]]; } } long long ans = 0; for (int i = 0; i < N; i++){ vector dp = max_convolution(dp1[i], dp2[i]); for (int j = 0; j <= 1000; j++){ ans += dp[j] * j % MOD; } ans %= MOD; } cout << ans << endl; }