#pragma GCC optimize("O3") #pragma GCC target("tune=native") #pragma GCC target("avx") #include #include #include using ull = unsigned long long; constexpr ull MOD = 1000000007; int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::size_t N; std::cin >> N; std::vector R(N); for (std::size_t i = 0; i < N; i++) { std::cin >> R[i]; } std::vector> g(N); for (std::size_t i = 0; i < N - 1; i++) { std::size_t u, v; std::cin >> u >> v, u--, v--; g[u].push_back(v), g[v].push_back(u); } std::vector sindo(N, 0); auto dfs = [&](auto&& self, const std::size_t s, const std::size_t p, const std::size_t R) -> void { sindo[s]++; for (const std::size_t to : g[s]) { if (to == p) { continue; } if (to >= R) { continue; } self(self, to, s, R); } }; for (std::size_t i = 0; i < N; i++) { dfs(dfs, i, N, i); } ull ans = 1; for (std::size_t i = 0; i < N; i++) { (ans *= (sindo[i] + R[i]) % MOD) %= MOD; } std::cout << ans << std::endl; return 0; }