#pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; template class y_combinator { F f; public: y_combinator(F&& f) : f(std::forward(f)) {} template auto operator()(Args&&... args) const { return f(*this, std::forward(args)...); } }; using ll = long long; using ld = long double; using u8 = uint8_t; using u32 = uint32_t; using u64 = uint64_t; using vi = vector; using vl = vector; using pii = pair; using pll = pair; template > using prique = std::priority_queue, U>; inline constexpr int popcnt(u64 x) noexcept { return __builtin_popcountll(x); } template T floor(T a, T b) noexcept { return a / b - (a % b && (a ^ b) < 0); } template T ceil(T a, T b) noexcept { return floor(a + b - 1, b); } template bool chmin(T& x, const T& y) noexcept { return (x > y ? x = y, true : false); } template bool chmax(T& x, const T& y) noexcept { return (x < y ? x = y, true : false); } template void UNIQUE(std::vector& v) { std::sort(std::begin(v), std::end(v)), v.erase(std::unique(std::begin(v), std::end(v)), std::end(v)); } void SCAN() {} template void SCAN(H& h, T&... t) { std::cin >> h, SCAN(t...); } #define INT(...) \ int __VA_ARGS__; \ SCAN(__VA_ARGS__) #define LL(...) \ long long __VA_ARGS__; \ SCAN(__VA_ARGS__) #define LD(...) \ long double __VA_ARGS__; \ SCAN(__VA_ARGS__) #define STR(...) \ std::string __VA_ARGS__; \ SCAN(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ SCAN(__VA_ARGS__) #define VEC(type, name, size) \ std::vector name(size); \ for (int i = 0; i < (int)size; i++) SCAN(name[i]); #define VV(type, name, h, w) \ std::vector> name(h, std::vector(w)); \ for (int i = 0; i < (int)h; i++) \ for (int j = 0; j < (int)w; j++) SCAN(name[i][j]); #define overload4(a, b, c, d, e, ...) e #define rep1(a) for (long long _i = 0; _i < (a); _i++) #define rep2(i, a) for (long long i = 0; i < (a); i++) #define rep3(i, a, b) for (long long i = (a); i < (b); i++) #define rep4(i, a, b, c) for (long long i = (a); i < (b); i += (c)) #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rrep(i, a, b, c) for (long long i = (a); i > (b); i += (c)) #define all(x) std::begin(x), std::end(x) #define rall(x) std::rbegin(x), std::rend(x) #define len(x) (long long)(size(x)) #define pb push_back #ifndef LOCAL #define debug(...) #endif template class Osa_k { std::array lpf; public: constexpr Osa_k() { for (int x = 2; x < W; x++) { if (lpf[x] != 0) continue; lpf[x] = x; if (x >= (W + x - 1) / x) continue; for (int y = x * x; y < W; y += x) { if (lpf[y] == 0) lpf[y] = x; } } } std::vector> factor(int x) const { assert(1 <= x && x < W); std::vector> res; while (x > 1) { if (res.empty() || res.back().first != lpf[x]) { res.push_back(std::make_pair(lpf[x], 1)); } else { res.back().second += 1; } x /= lpf[x]; } return res; } bool is_prime(int x) const { assert(1 <= x && x < W); if (x == 1) return 0; return lpf[x] == x; } }; #include using mint = atcoder::modint998244353; Osa_k<1 << 20> F; const int maxn = 5 << 17; vector g[5 << 17]; vector col[5 << 17]; int sz[5 << 17]; map* cnt[maxn]; mint ANS[5 << 17]; void dfs(int v, int p) { int mx = -1, bigChild = -1; for (auto u : g[v]) { if (u != p) { dfs(u, v); if (sz[u] > mx) { mx = sz[u]; bigChild = u; } } } if (bigChild != -1) { cnt[v] = cnt[bigChild]; } else { cnt[v] = new map(); } for (auto [p, e] : col[v]) chmax((*cnt[v])[p], e); for (auto u : g[v]) { if (u != p && u != bigChild) { for (auto x : *cnt[u]) chmax((*cnt[v])[x.first], x.second); } } mint t = 1; for (auto [p, e] : *cnt[v]) { t *= mint::raw(p).pow(e); } ANS[v] = t; } void run_case() { INT(N); VEC(int, A, N); rep(i, N) { col[i] = F.factor(A[i]); } rep(i, N - 1) { INT(a, b); a--, b--; g[a].pb(b); g[b].pb(a); } y_combinator([&](auto self, int v, int p) -> void { sz[v] = 1; for (int u : g[v]) { if (u != p) { self(u, v); sz[v] += sz[u]; } } })(0, -1); dfs(0, -1); rep(i, N) cout << ANS[i].val() << "\n"; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::fixed(std::cout).precision(16); int T = 1; while (T--) run_case(); return 0; }