#include #include #define For(i, a, b) for(long long i = a; i < b; i++) #define rep(i, n) For(i, 0, n) #define rFor(i, a, b) for(long long i = a; i >= b; i--) #define ALL(v) (v).begin(), (v).end() #define rALL(v) (v).rbegin(), (v).rend() using namespace std; using lint = long long; using ld = long double; using mint = atcoder::modint998244353; int INF = 2000000000; lint LINF = 1000000000000000000; template struct Edge { int from, to; T cost; int idx; Edge() {} Edge(int to_) : to(to_) {} Edge(int to_, T cost_) : to(to_), cost(cost_) {} Edge(int from_, int to_, int idx_) : from(from_), to(to_), idx(idx_) {} Edge(int from_, int to_, T cost_, int idx_) : from(from_), to(to_), cost(cost_), idx(idx_) {} }; template using Graph = vector>>; using graph = Graph; using edge = Edge; #define add emplace_back template , T), T (*add_node)(int, T), T (*id)()> struct RerootingDP { private: int n; vector>> g; vector> dp, l, r; T dfs1(int v, int pv) { T res = id(); for (int i = 0; i < (int)g[v].size(); i++) { int nv = g[v][i].to; if (nv == pv) { continue; } dp[v][i] = dfs1(nv, v); res = merge(add_edge(g[v][i], dp[v][i]), res); } return add_node(v, res); } void dfs2(int v, int pv, T num) { int sz = g[v].size(); for (int i = 0; i < sz; i++) { int nv = g[v][i].to; if (nv == pv) { dp[v][i] = add_node(pv, num); } } l[v][0] = r[v][sz] = id(); for (int i = 0; i < sz; i++) { l[v][i + 1] = merge(add_edge(g[v][i], dp[v][i]), l[v][i]); r[v][sz - i - 1] = merge(add_edge(g[v][sz - i - 1], dp[v][sz - i - 1]), r[v][sz - i]); } for (int i = 0; i < sz; i++) { int nv = g[v][i].to; if (nv == pv) { continue; } T nnum = merge(l[v][i], r[v][i + 1]); dfs2(nv, v, nnum); } } public: RerootingDP(vector>> g_) : n(g_.size()), g(g_), dp(n), l(n), r(n) { for (int i = 0; i < n; i++) { dp[i].resize(g[i].size()); l[i].resize(g[i].size() + 1); r[i].resize(g[i].size() + 1); } dfs1(0, -1); dfs2(0, -1, id()); } T get(int v) { T res = id(); for (int i = 0; i < (int)g[v].size(); i++) { res = merge(add_edge(g[v][i], dp[v][i]), res); } return add_node(v, res); } void dump() { for (int v = 0; v < n; v++) { for (int i = 0; i < (int)g[v].size(); i++) { cout << v + 1 << "->" << g[v][i].to + 1 << " : "; cout << dp[v][i]; cout << endl; } } } }; vector A(200010); mint merge(mint a, mint b) { return a + b; } mint add_edge(Edge e, mint a) { return a; } mint add_node(int v, mint a) { return A[v] * a + A[v]; } mint id() { return mint(0); } int main() { int n; cin >> n; rep(i, n) { int x; cin >> x; A[i] = mint(x); } Graph g(n); rep(i, n - 1) { int u, v; cin >> u >> v; u--; v--; g[u].add(v); g[v].add(u); } RerootingDP rd(g); mint ans = mint(0); rep(i, n) { ans += rd.get(i); } ans -= accumulate(ALL(A), mint(0)); ans /= mint(2); cout << ans.val() << endl; }