#include #include #include #include //!=================================================!// //! .88888. dP !// //! d8' '88 88 !// //! 88 88d888b. .d8888b. 88d888b. 88d888b. !// //! 88 YP88 88' '88 88' '88 88' '88 88' '88 !// //! Y8. .88 88 88. .88 88. .88 88 88 !// //! '88888' dP '88888P8 88Y888P' dP dP !// //! 88 !// //! dP !// //!=================================================!// struct Graph { Graph(const std::size_t v) : V{v}, edge(v), rev_edge(v) {} void addEdge(const std::size_t from, const std::size_t to, const bool bi = false) { assert(from < V), assert(to < V); edge[from].push_back(to), rev_edge[to].push_back(from); if (bi) { addEdge(to, from, false); } } const std::vector& operator[](const std::size_t i) const { return assert(i < V), edge[i]; } friend std::ostream& operator<<(std::ostream& os, const Graph& g) { os << "[\n"; for (std::size_t i = 0; i < g.V; i++) { for (const std::size_t to : g.edge[i]) { os << i << "->" << to << "\n"; } } return (os << "]\n"); } std::size_t V; std::vector> edge, rev_edge; }; //!==============================================================================!// //! dP dP oo 88888888b oo dP !// //! 88 88 88 88 !// //! 88 88 88d888b. dP .d8888b. 88d888b. a88aaaa dP 88d888b. .d888b88 !// //! 88 88 88' '88 88 88' '88 88' '88 88 88 88' '88 88' '88 !// //! Y8. .8P 88 88 88 88. .88 88 88 88 88 88 88 88. .88 !// //! 'Y88888P' dP dP dP '88888P' dP dP dP dP dP dP '88888P8 !// //!==============================================================================!// class UnionFind { public: UnionFind(const std::size_t v) : V(v), parent(v), size(v, 1) { std::iota(parent.begin(), parent.end(), 0); } std::size_t find(const std::size_t a) { return assert(a < V), parent[a] == a ? a : parent[a] = find(parent[a]); } bool same(const std::size_t a, const std::size_t b) { assert(a < V), assert(b < V); return find(a) == find(b); } void unite(std::size_t a, std::size_t b) { assert(a < V), assert(b < V); a = find(a), b = find(b); if (a == b) { return; } if (size[a] < size[b]) { std::swap(a, b); } size[a] += size[b], parent[b] = a; } std::size_t getSize(const std::size_t a) { return assert(a < V), size[find(a)]; } friend std::ostream& operator<<(std::ostream& os, const UnionFind& uf) { os << "["; for (std::size_t i = 0; i < uf.parent.size(); i++) { os << uf.parent[i] << (i + 1 == uf.parent.size() ? "" : ","); } return (os << "]\n"); } private: const std::size_t V; std::vector parent, size; }; //!============================================!// //! 8888ba.88ba oo !// //! 88 '8b '8b !// //! 88 88 88 .d8888b. dP 88d888b. !// //! 88 88 88 88' '88 88 88' '88 !// //! 88 88 88 88. .88 88 88 88 !// //! dP dP dP '88888P8 dP dP dP !// //!============================================!// int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); using ull = unsigned long long; constexpr ull MOD = 1000000007; std::size_t N; std::cin >> N; std::vector r(N); for (std::size_t i = 0; i < N; i++) { std::cin >> r[i]; } Graph T(N); for (std::size_t i = 0; i < N - 1; i++) { std::size_t u, v; std::cin >> u >> v, u--, v--; T.addEdge(u, v, true); } UnionFind uf(N); std::vector top(N); std::iota(top.begin(), top.end(), 0); Graph T2(N); for (std::size_t i = 0; i < N; i++) { for (const std::size_t to : T[i]) { if (to > i) { continue; } const std::size_t rep = uf.find(to); T2.addEdge(i, top[rep]); uf.unite(i, rep); top[uf.find(i)] = i; } } ull ans = 1; auto dfs = [&](auto&& self, const std::size_t s, const ull d) -> void { (ans *= (r[s] + d)) %= MOD; for (const std::size_t to : T2[s]) { self(self, to, d + 1); } }; dfs(dfs, N - 1, 1); std::cout << ans << std::endl; return 0; }