#include using namespace std; using ll = long long; const int INF = 1e9 + 10; const ll INFL = 4e18; #include using mint = atcoder::modint998244353; int main() { int N; cin >> N; vector A(N); for (int i = 0; i < N; i++) cin >> A[i]; vector> G(N); for (int i = 0; i < N - 1; i++) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } vector> dp(N); auto dfs1 = [&](auto dfs1, int now, int pre) -> mint { dp[now] = vector(ssize(G[now])); mint ret = 0; for (int i = 0; i < ssize(G[now]); i++) { int nxt = G[now][i]; if (nxt != pre) { dp[now][i] = dfs1(dfs1, nxt, now) * A[now]; ret += dp[now][i]; } } return ret + A[now]; }; mint ans = 0; auto dfs2 = [&](auto dfs2, int now, int pre, mint prp) -> void { int deg = ssize(G[now]); for (int i = 0; i < deg; i++) { if (G[now][i] == pre) dp[now][i] = prp * A[now]; } vector dpl(deg + 1, 0), dpr(deg + 1, 0); for (int i = 0; i < deg; i++) dpl[i + 1] = dpl[i] + dp[now][i]; for (int i = deg - 1; i >= 0; i--) dpr[i] = dpr[i + 1] + dp[now][i]; ans += dpl.back(); for (int i = 0; i < deg; i++) { int nxt = G[now][i]; if (nxt != pre) { mint nprp = dpl[i] + dpr[i + 1] + A[now]; dfs2(dfs2, nxt, now, nprp); } } }; dfs1(dfs1, 0, -1); dfs2(dfs2, 0, -1, 0); ans /= 2; cout << ans.val() << endl; }