#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } using mint = atcoder::modint998244353; ostream& operator<<(ostream& os, const mint& m){ os << m.val(); return os; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n; cin >> n; vector a(n); for(int i = 0; i < n; i++) cin >> a[i]; vector> g(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); } vector> dp(n); vector subtree_size(n); for(int i = 0; i < n; i++) dp[i].resize(g[i].size()); function dfs1 = [&](int v, int p){ subtree_size[v] = 1; mint pw = mint(10).pow(a[v].size()); mint ans = pw; for(int i = 0; i < g[v].size(); i++){ int u = g[v][i]; if(u == p) continue; dp[v][i] = dfs1(u, v); ans += dp[v][i]*pw; subtree_size[v] += subtree_size[u]; } return ans; }; dfs1(0, -1); function dfs2 = [&](int v, int p, mint from_par){ int deg = g[v].size(); for(int i = 0; i < deg; i++){ if(g[v][i] == p){ dp[v][i] = from_par; break; } } mint pw = mint(10).pow(a[v].size()); vector from_right(deg+1, 0); for(int i = deg-1; i >= 0; i--){ from_right[i] = from_right[i+1] + dp[v][i]; } mint from_left = 0; for(int i = 0; i < deg; i++){ int u = g[v][i]; if(u != p){ mint x = (from_left+from_right[i+1]+1)*pw; dfs2(u, v, x); } from_left = from_left + dp[v][i]; } }; dfs2(0, -1, mint(0)); mint ans = 0; for(int v = 0; v < n; v++){ mint x = stoll(a[v]); mint sum = 0; for(int i = 0; i < g[v].size(); i++){ int to = g[v][i]; if(subtree_size[to] > subtree_size[v]){ sum += dp[v][i]*x*subtree_size[v]; }else{ sum += dp[v][i]*x*(n-subtree_size[to]); } } sum += x*n; ans += sum; // cout << v << ':' << sum << endl; } cout << ans << endl; }