#include #include using namespace std; using namespace boost::multiprecision; using BigInt = cpp_int; const int MOD = 998244353; int N; vector A; vector> G; vector ans; // 任意精度整数版 GCD BigInt gcdBig(BigInt a, BigInt b) { while (b != 0) { BigInt t = b; b = a % b; a = t; } return a; } // DFSで部分木のLCMを計算 void dfs(int v, int p) { ans[v] = A[v]; for (int nv : G[v]) { if (nv == p) continue; dfs(nv, v); BigInt g = gcdBig(ans[v], ans[nv]); ans[v] = (ans[v] / g) * ans[nv]; // LCM(ans[v], ans[nv]) } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; A.resize(N); for (int i = 0; i < N; i++) { long long tmp; cin >> tmp; A[i] = tmp; // cpp_int に代入 } G.assign(N, {}); for (int i = 0; i < N-1; i++) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } ans.assign(N, 0); dfs(0, -1); for (int i = 0; i < N; i++) { cout << (ans[i] % MOD) << "\n"; } }