#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include #define rep(i,n) for(int i=0;i<(n);i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) ((int)(x).size()) #define pb push_back using ll = long long; using namespace std; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b> G; vector dp1; vector A; vector len; vector child; vector p10(1500000); ll dfs_c(int n, int p){ int ans = 1; rep(i,sz(G[n])){ int x = G[n][i]; if(x==p) continue; ans += dfs_c(x,n); } child[n] = ans; return ans; } ll dfs(int n, int p){ ll ans = 0; rep(i,sz(G[n])){ int x = G[n][i]; if(x==p) continue; ans += dfs(x,n); ans %= mod; } ans *= p10[len[n]]; ans %= mod; ans += child[n] * A[n] % mod; ans %= mod; dp1[n] = ans; return ans; } int main(){ int N; cin >> N; A.resize(N); rep(i,N) cin >> A[i]; len.resize(N); rep(i,N){ len[i] = (ll)sz(to_string(A[i])); } rep(i,N) A[i] %= mod; p10[0] = 1; rep(i,1500000-1){ p10[i+1] = p10[i] * 10 % mod; } G.resize(N); rep(i,N-1){ int a,b; cin >> a >> b; a--; b--; G[a].pb(b); G[b].pb(a); } dp1.assign(N,-1); child.assign(N,-1); dfs_c(0,-1); dfs(0,-1); vector d(N,-1); vector dp2(N,-1); queue q; q.push(0); d[0] = 0; dp2[0] = dp1[0]; while(!q.empty()){ int n = q.front(); q.pop(); rep(i,sz(G[n])){ int x=G[n][i]; if(d[x]>=0) continue; q.push(x); d[x] = d[n]+1; dp2[x] = dp1[x]; dp2[x] += (dp2[n] - (dp1[x]*p10[len[n]]%mod+child[x]*A[n]%mod)%mod + mod) %mod * (p10[len[x]]) % mod; dp2[x] += A[x]*(child[0]-child[x]) % mod; dp2[x] %= mod; } } ll ans = 0; rep(i,N) ans += dp2[i]; cout << ans % mod << endl; return 0; };