#include using namespace std; using lint = long long; using pi = array; #define sz(a) ((int)(a).size()) #define all(a) (a).begin(), (a).end() const int MAXN = 300005; const int mod = 998244353; struct mint { int val; mint() { val = 0; } mint(const lint &v) { val = (-mod <= v && v < mod) ? v : v % mod; if (val < 0) val += mod; } friend ostream &operator<<(ostream &os, const mint &a) { return os << a.val; } friend bool operator==(const mint &a, const mint &b) { return a.val == b.val; } friend bool operator!=(const mint &a, const mint &b) { return !(a == b); } friend bool operator<(const mint &a, const mint &b) { return a.val < b.val; } mint operator-() const { return mint(-val); } mint &operator+=(const mint &m) { if ((val += m.val) >= mod) val -= mod; return *this; } mint &operator-=(const mint &m) { if ((val -= m.val) < 0) val += mod; return *this; } mint &operator*=(const mint &m) { val = (lint)val * m.val % mod; return *this; } friend mint ipow(mint a, lint p) { mint ans = 1; for (; p; p /= 2, a *= a) if (p & 1) ans *= a; return ans; } mint inv() const { return ipow(*this, mod - 2); } mint &operator/=(const mint &m) { return (*this) *= m.inv(); } friend mint operator+(mint a, const mint &b) { return a += b; } friend mint operator-(mint a, const mint &b) { return a -= b; } friend mint operator*(mint a, const mint &b) { return a *= b; } friend mint operator/(mint a, const mint &b) { return a /= b; } operator int64_t() const { return val; } }; struct node { int j; mint A, B, S; bool operator<(const node &nd) const { return j < nd.j; } }; vector gph[MAXN]; int val[MAXN]; vector dfs(int x, int p = -1) { vector a; a.push_back((node){val[x], mint(1), mint(0), mint(1)}); for (auto &y : gph[x]) { if (y == p) continue; auto b = dfs(y, x); vector nxt; mint ssum = 0, basum = 0; for (auto bb : b) { ssum += bb.S; basum += (bb.B + mint(bb.j) * bb.A); } for (auto aa : a) { nxt.push_back((node){aa.j, aa.A * ssum, aa.S * basum + aa.B * ssum, aa.S * ssum}); } if (sz(a) > sz(b)) swap(a, b); mint tasum = 0, tbsum = 0, tssum = 0; vector bisect(sz(a) + 1); int j = 0; for (int i = 0; i < sz(a); i++) { while (j < sz(b) && b[j].j < a[i].j) j++; bisect[i] = j; } bisect[sz(a)] = sz(b); for (int i = 0; i < sz(a); i++) { mint asum = 0, bsum = 0, ssum = 0; for (int j = 0; j < bisect[i]; j++) { asum += b[j].A; bsum += b[j].B; ssum += b[j].S; } tasum += a[i].A; tbsum += a[i].B; tssum += a[i].S; a[i].A = a[i].A * ssum + a[i].S * asum; a[i].B = a[i].B * ssum + a[i].S * bsum; a[i].S = a[i].S * ssum; for (int j = bisect[i]; j < bisect[i + 1]; j++) { auto cp = b[j]; cp.A = tssum * cp.A + tasum * cp.S; cp.B = tssum * cp.B + tbsum * cp.S; cp.S = tssum * cp.S; nxt.push_back(cp); } } for (auto &z : a) nxt.push_back(z); a.clear(); sort(all(nxt)); for (int i = 0; i < sz(nxt);) { int j = i; node toput; toput.j = nxt[i].j; while (j < sz(nxt) && nxt[i].j == nxt[j].j) { toput.A += nxt[j].A; toput.B += nxt[j].B; toput.S += nxt[j].S; j++; } i = j; a.push_back(toput); } } return a; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) cin >> val[i]; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--; v--; gph[u].push_back(v); gph[v].push_back(u); } auto ans = dfs(0); mint ret = 0; for (auto &z : ans) { ret += z.A * mint(z.j) + z.B; } cout << ret << "\n"; }