#include #include using namespace std; using ll = long long; using namespace atcoder; using mint = modint998244353; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); //dp(i)=iを根とする部分木での答え ll N, U, V; cin >> N; vector A(N); vector> E(N); for (int i=0; i> A[i]; for (int i=0; i> U >> V; U--; V--; E[U].push_back(V); E[V].push_back(U); } mint ans=0, iv=mint(2).inv(); vector dp(N); auto dfs=[&](auto self, int from, int p=-1)->void{ mint res=0, res2=0; for (auto to : E[from]){ if (to == p) continue; self(self, to, from); res += dp[to]; res2 += dp[to] * dp[to]; dp[from] += dp[to]; } dp[from] += 1; //fromをLCPとする頂点間での答えへの寄与 //prod(i, j)dp[i]dp[j] * A[i] ans += (res*res-res2) * iv * A[from]; dp[from] *= A[from]; //fromを末端とするパスの答えへの寄与 ans += dp[from]-A[from]; }; dfs(dfs, 0); cout << ans.val() << endl; return 0; }