import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines MOD = 10 ** 9 + 7 N = int(readline()) R = tuple(map(int, readline().split())) class UnionFind: def __init__(self, N): self.root = list(range(N)) self.size = [1] * (N) self.largest = list(range(N)) def find_root(self, x): root = self.root while root[x] != x: root[x] = root[root[x]] x = root[x] return x def merge(self, x, y): x = self.find_root(x) y = self.find_root(y) if x == y: return False sx, sy = self.size[x], self.size[y] v = max(self.largest[x], self.largest[y]) if sx < sy: self.root[x] = y self.size[y] += sx self.largest[y] = v else: self.root[y] = x self.size[x] += sy self.largest[x] = v return True graph = [[] for _ in range(N + 1)] m = map(int, read().split()) for u, v in zip(m, m): graph[u].append(v) graph[v].append(u) parent = [0] * (N + 1) uf = UnionFind(N + 1) merge = uf.merge find = uf.find_root for v in range(1, N + 1): child = set(uf.largest[find(w)] for w in graph[v] if w < v) for w in child: merge(v, w) parent[w] = v S = [1] * (N + 1) for i in range(N - 1, 0, -1): S[i] += S[parent[i]] answer = 1 for r, s in zip(R, S[1:]): answer *= (r + s) answer %= MOD print(answer)