#include #include using namespace std; using mint = atcoder::modint998244353; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector A(N); for (int i = 0; i < N; i++) cin >> A[i]; vector> G(N); for (int i = 1; i < N; i++) { int u, v; cin >> u >> v; u-- ,v--; G[u].push_back(v); G[v].push_back(u); } vector S(N); mint ans = 0; const mint inv2 = mint::raw(2).inv(); auto dfs = [&](const auto &dfs, int u, int p) -> void { mint s = 0, ss = 0; for (int v : G[u]) { if (v != p) { dfs(dfs, v, u); s += S[v]; ss += S[v] * S[v]; } } S[u] = A[u] * (s + 1); ans += A[u] * (s + (s * s - ss) * inv2); }; dfs(dfs, 0, -1); cout << ans.val() << endl; }