use std::collections::HashMap; use std::io::{self, Read}; const MAX: usize = 1_000_010; const MOD: i64 = 998_244_353; fn mod_pow(mut a: i64, mut e: usize) -> i64 { let mut res = 1i64; while e > 0 { if e & 1 == 1 { res = res * a % MOD; } a = a * a % MOD; e >>= 1; } res } fn fact(mut x: usize, sieve: &[usize]) -> HashMap { let mut mp = HashMap::new(); while x != 1 { let p = sieve[x]; *mp.entry(p).or_insert(0) += 1; x /= p; } mp } fn main() { let mut input = String::new(); io::stdin().read_to_string(&mut input).unwrap(); let mut it = input.split_whitespace(); let n: usize = it.next().unwrap().parse().unwrap(); let mut a = vec![0usize; n]; for i in 0..n { a[i] = it.next().unwrap().parse().unwrap(); } // smallest prime factor let mut sieve = vec![0usize; MAX]; for i in 0..MAX { sieve[i] = i; } for i in 2..MAX { if sieve[i] != i { continue; } let mut j = i; while j < MAX { if sieve[j] == j { sieve[j] = i; } j += i; } } let mut e = vec![Vec::::new(); n]; for _ in 0..n - 1 { let x: usize = it.next().unwrap().parse::().unwrap() - 1; let y: usize = it.next().unwrap().parse::().unwrap() - 1; e[x].push(y); e[y].push(x); } // rooted tree let root = 0; let mut parent = vec![usize::MAX; n]; parent[root] = root; let mut child = vec![Vec::::new(); n]; let mut stack = vec![root]; let mut topo = Vec::new(); while let Some(v) = stack.pop() { topo.push(v); for &to in &e[v] { if parent[to] == usize::MAX { parent[to] = v; child[v].push(to); stack.push(to); } } } let mut dp: Vec)>> = (0..n).map(|_| None).collect(); let mut ans = vec![0i64; n]; for &v in topo.iter().rev() { let mut score = a[v] as i64 % MOD; let mut mp = fact(a[v], &sieve); for &to in &child[v] { let (child_score, mut child_map) = dp[to].take().unwrap(); // small-to-large if mp.len() < child_map.len() { std::mem::swap(&mut mp, &mut child_map); score = child_score; } for (p, cnt2) in child_map { match mp.get_mut(&p) { Some(cnt1) => { if *cnt1 < cnt2 { let add = cnt2 - *cnt1; score = score * mod_pow(p as i64, add) % MOD; *cnt1 = cnt2; } } None => { mp.insert(p, cnt2); score = score * mod_pow(p as i64, cnt2) % MOD; } } } } ans[v] = score; dp[v] = Some((score, mp)); } for x in ans { println!("{}", x); } }