#line 1 "src.cpp" // #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #include using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; template using V = vector; template using VV = V>; template using VVV = V>; template using VVVV = VV>; #define rep(i,n) for(ll i=0ll;(i)<(n);(i)++) #define REP(i,a,n) for(ll i=(a);(i)<(n);(i)++) #define rrep(i,n) for(ll i=(n)-1;(i)>=(0ll);(i)--) #define RREP(i,a,n) for(ll i=(n)-1;(i)>=(a);(i)--) const long long INF = (1LL << 60); const long long mod99 = 998244353; const long long mod107 = 1000000007; const long long mod = mod99; #define eb emplace_back #define be(v) (v).begin(),(v).end() #define all(v) (v).begin(),(v).end() #define foa(i,v) for(auto& (i) : (v)) #define UQ(v) sort(be(v)), (v).erase(unique(be(v)), (v).end()) #define UQ2(v,cmp) sort(be(v)), (v).erase(unique(be(v),cmp), (v).end()) #define UQ3(v,cmp) sort(be(v),cmp), (v).erase(unique(be(v)), (v).end()) #define UQ4(v,cmp,cmp2) sort(be(v), cmp), (v).erase(unique(be(v),cmp2), (v).end()) #define LB(x,v) (lower_bound(be(v),(x))-(v).begin()) #define LB2(x,v,cmp) (lower_bound(be(v),(x),(cmp))-(v).begin()) #define UB(x,v) (upper_bound(be(v),(x))-(v).begin()) #define UB2(x,v,cmp) (upper_bound(be(v),(x),(cmp))-(v).begin()) #define dout() cout << fixed << setprecision(20) #define randinit() srand((unsigned)time(NULL)) template bool chmin(T& t, const U& u) { if (t > u){ t = u; return 1;} return 0; } template bool chmax(T& t, const U& u) { if (t < u){ t = u; return 1;} return 0; } ll Rnd(ll L=0, ll R=mod99){return rand()%(R-L)+L;} #line 2 "tree/auxiliary-tree.hpp" #line 6 "tree/auxiliary-tree.hpp" using namespace std; #line 2 "tree/heavy-light-decomposition.hpp" #line 2 "graph/graph-template.hpp" template struct edge { int src, to; T cost; edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {} edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template using Edges = vector>; template using WeightedGraph = vector>; using UnweightedGraph = vector>; // Input of (Unweighted) Graph UnweightedGraph graph(int N, int M = -1, bool is_directed = false, bool is_1origin = true) { UnweightedGraph g(N); if (M == -1) M = N - 1; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; if (is_1origin) x--, y--; g[x].push_back(y); if (!is_directed) g[y].push_back(x); } return g; } // Input of Weighted Graph template WeightedGraph wgraph(int N, int M = -1, bool is_directed = false, bool is_1origin = true) { WeightedGraph g(N); if (M == -1) M = N - 1; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; cin >> c; if (is_1origin) x--, y--; g[x].emplace_back(x, y, c); if (!is_directed) g[y].emplace_back(y, x, c); } return g; } // Input of Edges template Edges esgraph([[maybe_unused]] int N, int M, int is_weighted = true, bool is_1origin = true) { Edges es; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; if (is_weighted) cin >> c; else c = 1; if (is_1origin) x--, y--; es.emplace_back(x, y, c); } return es; } // Input of Adjacency Matrix template vector> adjgraph(int N, int M, T INF, int is_weighted = true, bool is_directed = false, bool is_1origin = true) { vector> d(N, vector(N, INF)); for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; if (is_weighted) cin >> c; else c = 1; if (is_1origin) x--, y--; d[x][y] = c; if (!is_directed) d[y][x] = c; } return d; } /** * @brief グラフテンプレート * @docs docs/graph/graph-template.md */ #line 4 "tree/heavy-light-decomposition.hpp" template struct HeavyLightDecomposition { private: void dfs_sz(int cur) { size[cur] = 1; for (auto& dst : g[cur]) { if (dst == par[cur]) { if (g[cur].size() >= 2 && int(dst) == int(g[cur][0])) swap(g[cur][0], g[cur][1]); else continue; } depth[dst] = depth[cur] + 1; par[dst] = cur; dfs_sz(dst); size[cur] += size[dst]; if (size[dst] > size[g[cur][0]]) { swap(dst, g[cur][0]); } } } void dfs_hld(int cur) { down[cur] = id++; for (auto dst : g[cur]) { if (dst == par[cur]) continue; nxt[dst] = (int(dst) == int(g[cur][0]) ? nxt[cur] : int(dst)); dfs_hld(dst); } up[cur] = id; } // [u, v) vector> ascend(int u, int v) const { vector> res; while (nxt[u] != nxt[v]) { res.emplace_back(down[u], down[nxt[u]]); u = par[nxt[u]]; } if (u != v) res.emplace_back(down[u], down[v] + 1); return res; } // (u, v] vector> descend(int u, int v) const { if (u == v) return {}; if (nxt[u] == nxt[v]) return {{down[u] + 1, down[v]}}; auto res = descend(u, par[nxt[v]]); res.emplace_back(down[nxt[v]], down[v]); return res; } public: G& g; int root, id; vector size, depth, down, up, nxt, par; HeavyLightDecomposition(G& _g, int _root = 0) : g(_g), root(_root), id(0), size(g.size(), 0), depth(g.size(), 0), down(g.size(), -1), up(g.size(), -1), nxt(g.size(), root), par(g.size(), root) { dfs_sz(root); dfs_hld(root); } pair idx(int i) const { return make_pair(down[i], up[i]); } template void path_query(int u, int v, bool vertex, const F& f) { int l = lca(u, v); for (auto&& [a, b] : ascend(u, l)) { int s = a + 1, t = b; s > t ? f(t, s) : f(s, t); } if (vertex) f(down[l], down[l] + 1); for (auto&& [a, b] : descend(l, v)) { int s = a, t = b + 1; s > t ? f(t, s) : f(s, t); } } template void path_noncommutative_query(int u, int v, bool vertex, const F& f) { int l = lca(u, v); for (auto&& [a, b] : ascend(u, l)) f(a + 1, b); if (vertex) f(down[l], down[l] + 1); for (auto&& [a, b] : descend(l, v)) f(a, b + 1); } template void subtree_query(int u, bool vertex, const F& f) { f(down[u] + int(!vertex), up[u]); } int lca(int a, int b) { while (nxt[a] != nxt[b]) { if (down[a] < down[b]) swap(a, b); a = par[nxt[a]]; } return depth[a] < depth[b] ? a : b; } int dist(int a, int b) { return depth[a] + depth[b] - depth[lca(a, b)] * 2; } }; /** * @brief Heavy Light Decomposition(重軽分解) * @docs docs/tree/heavy-light-decomposition.md */ #line 9 "tree/auxiliary-tree.hpp" template struct AuxiliaryTree { G g; HeavyLightDecomposition hld; AuxiliaryTree(const G& _g, int root = 0) : g(_g), hld(g, root) {} // ps : 頂点集合 // 返り値 : (aux tree, aux tree の頂点と g の頂点の対応表) // aux tree は 親->子 の向きの辺のみ含まれる // ps が空の場合は空のグラフを返す pair>, vector> get(vector ps) { if (ps.empty()) return {}; auto comp = [&](int i, int j) { return hld.down[i] < hld.down[j]; }; // sort(begin(ps), end(ps), comp); for (int i = 0, ie = ps.size(); i + 1 < ie; i++) { ps.push_back(hld.lca(ps[i], ps[i + 1])); } sort(begin(ps), end(ps), comp); ps.erase(unique(begin(ps), end(ps)), end(ps)); vector> aux(ps.size()); vector rs; rs.push_back(0); for (int i = 1; i < (int)ps.size(); i++) { int l = hld.lca(ps[rs.back()], ps[i]); while (ps[rs.back()] != l) rs.pop_back(); aux[rs.back()].push_back(i); rs.push_back(i); } return make_pair(aux, ps); } }; /** * @brief Auxiliary Tree */ #line 46 "src.cpp" #include using namespace atcoder; using mint = modint998244353; void solve(){ ll n; cin >> n; V v(n); rep(i,n) cin >> v[i]; auto G = graph(n, n-1); V w = v; ll M = 1000001; V lc(M); V prime; for(int i=0; i= M) break; if(x > lc[i]) break; lc[i*x] = x; } } AuxiliaryTree auxgen(G, 0); V ord(n); rep(i, n) ord[i] = i; auto comp = [&](int i, int j) { return auxgen.hld.down[i] < auxgen.hld.down[j]; }; sort(be(ord), comp); map> memo; for(auto i:ord){ V vv; while(w[i] != 1){ vv.eb(lc[w[i]]); w[i] /= lc[w[i]]; } UQ(vv); for(auto x:vv) memo[x].eb(i); } V ans(n, mint::raw(1)); V vv(n, 1); V inv(1001000, mint::raw(1)); ll cnt = 0; for(auto [i, node]: memo){ { ll x = 1; mint ii = mint::raw(1) / i; while(x*i < 1001000){ inv[x*i] = inv[x] * ii; x *= i; } } for(auto x:node){ vv[x] = 1; while(v[x] % i == 0){ v[x] /= i; vv[x] *= i; } } if(node.size() == 1){ ans[node[0]] *= vv[node[0]]; continue; } auto[aux, mp] = auxgen.get(node); auto dfs = [&](auto&&dfs, ll nd) -> ll { ll num = mp[nd]; if(vv[num]%i != 0) vv[num] = 1; ll ret = vv[num]; mint val = 1; for(auto nx:aux[nd]){ ll a = dfs(dfs, nx); chmax(ret, a); val *= inv[a]; } val *= ret; ans[num] *= val; return ret; }; dfs(dfs, 0); } auto dfs = [&](auto&&dfs, ll nd, ll par) -> void { for(auto nx:G[nd]) if(nx != par){ dfs(dfs, nx, nd); ans[nd] *= ans[nx]; } }; dfs(dfs, 0, -1); rep(i, n) cout << ans[i].val() << "\n"; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int t=1; // cin >> t; rep(i,t) solve(); }