import sys input = sys.stdin.readline mod = 998244353 class ReRooting(): def __init__(self, N): self.N = N self.G = [[] for i in range(N)] self.mem = [[] for i in range(N)] self.AcL = [None] * N self.AcR = [None] * N self.par = [-1] * N self.sz = [1] * N self.unit = 0 self.dp1 = [self.unit] * N self.dp2 = [self.unit] * N def add_edge(self, i, j): self.G[i].append(j) self.G[j].append(i) def merge(self, a, b): return (a + b) % mod def calc_dp(self, v, s, sz): return (v * pow(10, len(str(s)), mod) + s * sz) % mod def calc_ans(self, v, s): return (v * pow(10, len(str(s)), mod) + s * N) % mod def bottom_up(self, s): stack = [s] while stack: u = stack.pop() if u >= 0: stack.append(~u) for v in self.G[u]: if v == self.par[u]: continue self.par[v] = u stack.append(v) else: u = ~u self.mem[u] = [] for v in self.G[u]: if v == self.par[u]: continue self.sz[u] += self.sz[v] self.dp1[u] = self.merge(self.dp1[u], self.dp1[v]) self.mem[u].append(v) self.dp1[u] = self.calc_dp(self.dp1[u], A[u], self.sz[u]) Nt = len(self.mem[u]) self.AcL[u] = [self.unit] * (Nt + 1) self.AcR[u] = [self.unit] * (Nt + 1) for i, v in enumerate(self.mem[u]): self.AcL[u][i + 1] = self.merge(self.AcL[u][i], self.dp1[v]) for i, v in enumerate(self.mem[u][::-1]): self.AcR[u][i + 1] = self.merge(self.AcR[u][i], self.dp1[v]) return def top_down(self, s): stack = [s] while stack: u = stack.pop() for i, v in enumerate(self.mem[u]): self.dp2[v] = self.merge(self.merge(self.dp2[u], self.AcL[u][i]), self.AcR[u][len(self.mem[u]) - i - 1]) self.dp2[v] = self.calc_dp(self.dp2[v], A[u], N - self.sz[v]) stack.append(v) return def solve(self, s): self.bottom_up(s) self.top_down(s) # print(self.dp1) # print(self.dp2) ans = [None] * self.N for i in range(self.N): ans[i] = self.dp2[i] for u in self.G[i]: if self.par[i] == u: continue ans[i] = self.merge(ans[i], self.dp1[u]) ans[i] = self.calc_ans(ans[i], A[i]) return ans N = int(input()) A = list(map(int, input().split())) G = ReRooting(N) for _ in range(N - 1): u, v = map(int, input().split()) u, v = u - 1, v - 1 G.add_edge(u, v) print(sum(G.solve(0))%mod)