from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc N = int(input()) A = list(map(int, input().split())) G = [[] for _ in range(N)] for _ in range(N-1): u, v = map(int, input().split()) G[u-1].append(v-1) G[v-1].append(u-1) MOD = 998244353 @bootstrap def dfs(n, pre): ans = 0 for i, v in enumerate(G[n]): if v != pre: dp[n][i] = yield dfs(v, n) ans += dp[n][i] ans %= MOD yield ((ans*A[n])%MOD+A[n])%MOD dp = [[0]*len(G[i]) for i in range(N)] dfs(0, -1) def LRset(n): for i in range(1, len(G[n])): L[n][i] = (L[n][i-1]+dp[n][i-1])%MOD for i in reversed(range(len(G[n])-1)): R[n][i] = (R[n][i+1]+dp[n][i+1])%MOD L = [[0]*len(G[i]) for i in range(N)] R = [[0]*len(G[i]) for i in range(N)] LRset(0) @bootstrap def rerooting(n, pre, no): visited[n] = True preno = 0 for i in range(len(G[n])): if G[n][i] == pre: preno = i break dp[n][preno] = ((L[pre][no]+R[pre][no])*A[pre]%MOD+A[pre])%MOD LRset(n) for i in range(len(G[n])): if not visited[G[n][i]]: yield rerooting(G[n][i], n, i) yield visited = [False]*N visited[0] = True for i in range(len(G[0])): if not visited[G[0][i]]: rerooting(G[0][i], 0, i) ans = 0 for i, d in enumerate(dp): for n in d: ans += n*A[i] ans %= MOD def inverse(n, d): return n * pow(d, -1, MOD) % MOD print(inverse(ans, 2))