fn main() { input!{ n: usize, a: [usize; n], uv: [(Usize1, Usize1); n-1], } let mut g = vec![Vec::new(); n]; for &(u, v) in &uv { g[u].push(v); g[v].push(u); } let mut ans = 0; dfs(0, MOD93, &g, &a, &mut ans); println!("{}", ans); fn dfs(pos: usize, ppos: usize, g: &Vec>, a: &Vec, ans: &mut usize) -> usize { let mut sm1 = 0; let mut sm2 = 0; for &npos in &g[pos] { if npos == ppos { continue; } let esm = dfs(npos, pos, g, a, ans) % MOD93; sm1 += esm; sm1 %= MOD93; sm2 += esm * esm; sm2 %= MOD93; } // ans+=a[p]*(sm1+(sm1*sm1-sm2)/2); let multi = (sm1*sm1+MOD93-sm2) % MOD93; let divs = div(multi, 2, MOD93); *ans += a[pos]* (sm1+divs); *ans %= MOD93; a[pos]*(sm1+1) % MOD93 } } // 繰り返し二乗法 fn mod_pow(mut a: usize, mut b: usize, mods: usize) -> usize { let mut res = 1; while b > 0 { if b % 2 == 1 { res *= a; res %= mods; } a *= a; a %= mods; b >>= 1; } res } // 繰り返し二乗法による除算 fn div(a: usize, b: usize, mods: usize) -> usize { let mut ans = a; let x = mod_pow(b, mods - 2, mods); ans *= x; ans %= mods; ans } // const MOD17: usize = 1000000007; const MOD93: usize = 998244353; // const INF: usize = 1 << 60; // let dx = vec![!0, 0, 1, 0]; // 上左下右 // let dy = vec![0, !0, 0, 1]; // 上左下右 // let d = vec!{(!0, 0), (0, !0), (1, 0), (0, 1)}; // 上左下右 #[allow(unused)] use proconio::{input, marker::Chars, marker::Usize1}; #[allow(unused)] use std::{ mem::swap, cmp::min, cmp::max, cmp::Reverse, collections::HashSet, collections::BTreeSet, collections::HashMap, collections::BTreeMap, collections::BinaryHeap, collections::VecDeque, iter::FromIterator, };