#include #include #define rep(i,n) for(int i=0;i vi; typedef vector vl; typedef vector> vvi; typedef vector> vvl; typedef long double ld; typedef pair P; template ostream& operator<<(ostream& os, const static_modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const dynamic_modint& a) {os << a.val(); return os;} template istream& operator>>(istream& is, static_modint& a) {long long x; is >> x; a = x; return is;} template istream& operator>>(istream& is, dynamic_modint& a) {long long x; is >> x; a = x; return is;} template istream& operator>>(istream& is, vector& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;} template ostream& operator<<(ostream& os, const pair& p){os << p.first << ' ' << p.second; return os;} template ostream& operator<<(ostream& os, const vector& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const vector>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;} template ostream& operator<<(ostream& os, const set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const unordered_set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const atcoder::segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const atcoder::lazy_segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template void chmin(T& a, T b){a = min(a, b);} template void chmax(T& a, T b){a = max(a, b);} // Rerooting // Arranged from https://github.com/atcoder/live_library/blob/master/rerooting.cpp // https://youtu.be/zG1L4vYuGrg?t=7092 // TODO: vertex info, edge info template struct Rerooting { static_assert(is_convertible_v>, "op must work as S(S, S)"); static_assert(is_convertible_v>, "e must work as S()"); static_assert(is_convertible_v>, "leaf must work as S()"); static_assert(is_convertible_v>, "mapping must work as S(T, S)"); int n; vector>> G; vector>> G_inv; vector> dp; vector ans; Rerooting(int n) : n(n), G(n), G_inv(n), dp(n), ans(n) {} void add_edge(int a, int b, T t) { G[a].emplace_back(b, t); G_inv[b].emplace_back(a, t); } void check(vector>> G){ int cnt = 0; rep(i, n) cnt += G[i].size(); assert(cnt == 2 * (n - 1)); } void init() { check(G); check(G_inv); dfs(0); bfs(0); } S dfs(int v, int p = -1) { bool isLeaf = true; S dpSum = e(); dp[v] = vector(G_inv[v].size(), e()); rep(i, int(G_inv[v].size())) { auto [u, t] = G_inv[v][i]; if (u == p) continue; isLeaf = false; dp[v][i] = mapping(t, dfs(u,v)); dpSum = op(dpSum, dp[v][i]); } if(isLeaf) dpSum = leaf(); return dpSum; } void bfs(int v, const S& dpP = e(), int p = -1) { int deg = G[v].size(); rep(i, deg) if (G[v][i].first == p) dp[v][i] = dpP; vector dpSumL(deg + 1, e()); rep(i, deg) dpSumL[i + 1] = op(dpSumL[i], dp[v][i]); vector dpSumR(deg + 1, e()); for (int i = deg - 1; i >= 0; --i) dpSumR[i] = op(dpSumR[i + 1], dp[v][i]); ans[v] = dpSumL[deg]; rep(i, deg) { auto [u, t] = G[v][i]; if (u == p) continue; S d = op(dpSumL[i], dpSumR[i + 1]); if(p == -1 and deg == 1) d = leaf(); bfs(u, mapping(t, d), v); } } }; using mint = atcoder::modint998244353; using S = mint; S op(S s1, S s2){ return s1 + s2; } S e(){ return 0; } S leaf(){ return 0; } using T = mint; S mapping(T t, S s){ return s * t + t; } int main(){ int n; cin >> n; vector a(n); cin >> a; Rerooting rerooting(n); rep(i, n - 1){ int u, v; cin >> u >> v; u--; v--; rerooting.add_edge(u, v, a[u]); rerooting.add_edge(v, u, a[v]); } rerooting.init(); auto ans = rerooting.ans; mint res = 0; rep(i, n) res += ans[i] * a[i]; cout << res / 2 << "\n"; return 0; }