#include const int Mod = 998244353; typedef struct Edge { struct Edge *next; int v, id; long long dp[1001]; } edge; int main() { int i, N, M, u, w, A[5001]; edge *adj[5001] = {}, e[10001], *p; scanf("%d", &N); for (u = 1; u <= N; u++) scanf("%d", &(A[u])); for (i = 0; i < N - 1; i++) { scanf("%d %d", &u, &w); e[i*2].v = w; e[i*2+1].v = u; e[i*2].id = i * 2; e[i*2+1].id = i * 2 + 1; e[i*2].next = adj[u]; e[i*2+1].next = adj[w]; adj[u] = &(e[i*2]); adj[w] = &(e[i*2+1]); } int par[5001] = {}, pred[5001], q[5001], head, tail; q[0] = 1; par[1] = 1; pred[1] = -1; for (head = 0, tail = 1; head < tail; head++) { u = q[head]; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (par[w] == 0) { par[w] = u; pred[w] = p->id; q[tail++] = w; } } } int k; long long sum; for (head--; head >= 1; head--) { u = q[head]; i = pred[u]; if (adj[u]->next == NULL) { for (k = 0; k < A[u]; k++) e[i].dp[k] = 1; for (k = A[u]; k <= 1000; k++) e[i].dp[k] = 2; continue; } for (p = adj[u], sum = 1; p != NULL; p = p->next) { if (p->v == par[u]) continue; sum = sum * p->dp[1000] % Mod; } e[i].dp[0] = sum; for (k = 1; k < A[u]; k++) e[i].dp[k] = e[i].dp[0]; for (k = A[u]; k <= 1000; k++) { e[i].dp[k] = e[i].dp[0]; for (p = adj[u], sum = 1; p != NULL; p = p->next) { if (p->v == par[u]) continue; sum = sum * p->dp[k] % Mod; } e[i].dp[k] = (e[i].dp[0] + sum) % Mod; } } for (head = 1; head < tail; head++) { w = q[head]; i = pred[w] ^ 1; u = par[w]; if (adj[u]->next == NULL) { for (k = 0; k < A[u]; k++) e[i].dp[k] = 1; for (k = A[u]; k <= 1000; k++) e[i].dp[k] = 2; continue; } for (p = adj[u], sum = 1; p != NULL; p = p->next) { if (p->id == (i ^ 1)) continue; sum = sum * p->dp[1000] % Mod; } e[i].dp[0] = sum; for (k = 1; k < A[u]; k++) e[i].dp[k] = e[i].dp[0]; for (k = A[u]; k <= 1000; k++) { e[i].dp[k] = e[i].dp[0]; for (p = adj[u], sum = 1; p != NULL; p = p->next) { if (p->id == (i ^ 1)) continue; sum = sum * p->dp[k] % Mod; } e[i].dp[k] = (e[i].dp[0] + sum) % Mod; } } long long ans = 0, total[1001]; for (u = 1; u <= N; u++) { for (k = 0; k <= 1000; k++) { for (p = adj[u], total[k] = 1; p != NULL; p = p->next) total[k] = total[k] * p->dp[k] % Mod; if (k > 0) ans += (total[k] - total[k-1] + Mod) * ((k >= A[u])? k: A[u]) % Mod; } ans += total[0] * A[u] % Mod; } printf("%lld\n", ans % Mod); fflush(stdout); return 0; }