#include using namespace std; template struct prime_modint { using mint = prime_modint; unsigned int v; prime_modint() : v(0) {} prime_modint(unsigned int a) { a %= MOD; v = a; } prime_modint(unsigned long long a) { a %= MOD; v = a; } prime_modint(int a) { a %= (int)(MOD); if(a < 0)a += MOD; v = a; } prime_modint(long long a) { a %= (int)(MOD); if(a < 0)a += MOD; v = a; } static constexpr int mod() { return MOD; } mint& operator++() {v++; if(v == MOD)v = 0; return *this;} mint& operator--() {if(v == 0)v = MOD; v--; return *this;} mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { v += rhs.v; if(v >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& rhs) { if(v < rhs.v) v += MOD; v -= rhs.v; return *this; } mint& operator*=(const mint& rhs) { v = (unsigned int)((unsigned long long)(v) * rhs.v % MOD); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint r = 1, x = *this; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { assert(v); return pow(MOD - 2); } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return (lhs.v == rhs.v); } friend bool operator!=(const mint& lhs, const mint& rhs) { return (lhs.v != rhs.v); } friend std::ostream& operator << (std::ostream &os, const mint& rhs) noexcept { return os << rhs.v; } }; //using mint = prime_modint<1000000007>; using mint = prime_modint<998244353>; vector a; mint tot; struct ReRooting{ using T = mint; const T identity = 0; //mergeの演算に対する単位元 int n; vector> G; vector> dp; vector ans; //vのi番目の辺の子からRを受け取ったときの処理 T receive(int v,int i,T &R){ return T(R); } //全体の累積lに子receiveの値をmerge //mergeの演算はモノイドでなくてはならない T merge(T l, T r){ return T(l + r); } //親に渡すときの処理 T givep(int v, T cum){ return T(++cum * a[v]); } ReRooting() {} ReRooting(int N):n(N){ G.resize(N); dp.resize(N); ans.resize(N,identity); } void add_edge(int a, int b) { G[a].emplace_back(b); G[b].emplace_back(a); } void read(){ int a, b; for(int i = 1; i < n; i++){ cin >> a >> b; a--, b--; G[a].emplace_back(b); G[b].emplace_back(a); } } void build() { dfs(0); // 普通に木DP dfs2(0, identity); // 残りの部分木に対応するDPを計算 } T dfs(int v, int p = -1) { T res = identity; int deg = G[v].size(); dp[v] = vector(deg, identity); for (int i = 0; i < deg; i++) { int u = G[v][i]; if (u == p) continue; dp[v][i] = dfs(u, v); res = merge(res, receive(v, i, dp[v][i]) ); } return givep(v,res); } void dfs2(int v, const T& dp_p, int p = -1) { int deg = G[v].size(); for (int i = 0; i < deg; i++) { // 前のdfsで計算した有向辺に対応する部分木のDPを保存 if (G[v][i] == p){dp[v][i] = dp_p; break;} } vector dp_l(deg + 1, identity), dp_r(deg + 1, identity); // 累積merge for (int i = 0; i < deg; i++) { dp_l[i + 1] = merge(dp_l[i], receive(v, i,dp[v][i])); } for (int i = deg - 1; i >= 0; i--) { dp_r[i] = merge(dp_r[i + 1], receive(v, i,dp[v][i])); } tot += dp_l[deg] * a[v]; //ans[v] = dp_l[deg]; // 頂点 v の答え for (int i = 0; i < deg; i++) { // 一つ隣の頂点に対しても同様に計算 int u = G[v][i]; if (u == p) continue; dfs2(u, givep(v,merge(dp_l[i], dp_r[i + 1])),v); } } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; a.resize(n); for(int i = 0; i < n; i++) cin >> a[i].v; ReRooting rr(n); rr.read(); rr.build(); cout << tot / 2 << '\n'; }