from collections import deque def bfs(s): dq = deque() dq.append([s, 0]) high = [10**9 for _ in range(N)] high[s] = 0 re=[] while len(dq) != 0: p, h = dq.popleft() re.append(p) for e in edge[p]: if high[e] == 10**9: dq.append([e, h + 1]) high[e] = h + 1 return re def extgcd(a, b): if b: d, y, x = extgcd(b, a % b) y -= (a // b) * x return d, x, y return a, 1, 0 #以下modinv def mod_inv(a, m): g, x, y = extgcd(a, m) if g != 1: raise Exception() if x < 0: x += m return x N=int(input()) A=list(map(int,input().split())) edge=[[] for _ in range(N)] for _ in range(N-1): u,v=map(int,input().split()) edge[u-1].append(v-1) edge[v-1].append(u-1) lis=bfs(0) dp=[-1 for _ in range(N)] ans=0 p=998244353 inv2=mod_inv(2,p) for i in range(len(lis)-1,-1,-1): pos=lis[i] down=[] for e in edge[pos]: if dp[e]!=-1: down.append(dp[e]) su=sum(down) if len(down)!=0: for d in down: ans+=d*A[pos]*2 ans+=(su-d)*d*A[pos] ans%=p #print(i,pos,su,ans) dp[pos]=(su+1)*A[pos]%p #print(lis) print(ans*inv2%p)