#include using namespace std; #if __has_include() #include using namespace atcoder; using mint = modint998244353; // using mint = modint1000000007; // using mint = double; #endif //define using ll = long long; using ull = unsigned long long; using pii = pair; using pll = pair; using vi = vector; using vll = vector; #define rep(i,l,r) for (int i = (int)(l); i < (int)(r); i++) #define rrep(i,l,r) for (int i = (int)(r-1); i >= (int)(l); i--) #define len(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define elif else if #define pb push_back #define eb emplace_back const int inf = 1e9; const long long infl = 1LL<<60; const int mod = 998244353; ll pow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } template bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; } template bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; } template using spq = priority_queue, greater>; templateistream& operator>>(istream& i, vector& v) {for(int j = 0; j < (int)(v).size(); j++) i >> v[j]; return i;} struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(15); } } iosetup; void solve(){ int n; cin >> n; vi a(n); cin >> a; vector e(n); rep(i, 0, n-1){ int u, v; cin >> u >> v; u--; v--; e[u].pb(v); e[v].pb(u); } vll ans(n); auto dfs = [&](auto dfs, int u, int f) -> pair, ll>{ map ndp; ll nr = 1; for (auto v : e[u]){ if (v != f){ auto[dp, r] = dfs(dfs, v, u); if (len(dp) > len(ndp)){ swap(dp, ndp); swap(r, nr); } for (auto [p, c] : dp){ if (ndp[p] < c){ int d = c - ndp[p]; ndp[p] = c; while(d--){ nr *= p; nr %= mod; } } } } } int p = 2; while(p*p < a[u]){ int c = 0; while (a[u] % p == 0){ c++; a[u]/=p; } if (ndp[p] < c){ int d = c - ndp[p]; ndp[p] = c; while(d--){ nr *= p; nr %= mod; } } p++; } if (a[u] != 1){ int p = a[u]; int c = 1; if (ndp[p] < c){ int d = c - ndp[p]; ndp[p] = c; while(d--){ nr *= p; nr %= mod; } } } ans[u] = nr; return {ndp, nr}; }; dfs(dfs, 0, -1); rep(i, 0, n){ cout << ans[i] << endl; } return ; } int main(){ int t; // cin >> t; t = 1; while(t--) solve(); return 0; }