#include #include using namespace std; using namespace atcoder; typedef modint998244353 mint; typedef long long ll; pair>> find_centroid(int st, int size, vector> &ikeru, vector &sizedp, vector &tansaku, vector &removed){ vector mada = {~st, st}; tansaku[st] = 1; while (!mada.empty()){ int i = mada.back(); mada.pop_back(); if (i >= 0){ for (int j: ikeru[i]){ if (removed[j]) continue; if (tansaku[j] != 1){ tansaku[j] = 1; mada.push_back(~j); mada.push_back(j); } } }else{ i = ~i; int tmp = 1; for (int j: ikeru[i]){ if (removed[j]) continue; if (tansaku[j] == 2) tmp += sizedp[j]; } sizedp[i] = tmp; tansaku[i] = 2; } } int now = st; tansaku[now] = 3; vector> ret(0); int par = st; while (true){ int v = 0, w = 0; if (size != sizedp[now]){ ret = {make_pair(par, size-sizedp[now])}; } for (int j: ikeru[now]){ if (removed[j]) continue; if (tansaku[j] == 2){ tansaku[j] = 3; ret.push_back(make_pair(j, sizedp[j])); if (v < sizedp[j]){ v = sizedp[j]; w = j; } } } if (v * 2 <= size) break; par = now; now = w; } removed[now] = true; return make_pair(now, ret); } void solve(int st, int size, vector> &ikeru, vector &sizedp, vector &tansaku, vector &removed, mint &ans, vector &a, vector &asize){ pair>> ret = find_centroid(st, size, ikeru, sizedp, tansaku, removed); //vector goes((int)ret.second.size()); if ((int)ret.second.size() > 0){ vector come((int)ret.second.size()); vector goes((int)ret.second.size()); vector nums((int)ret.second.size()); vector> dp;// KAIWAI, KETA, ATAI (goes) mint comesum = 0; mint goessum = 0; mint numssum = 0; int cnt = 0; for (pair i: ret.second){ vector mada = {i.first}; vector sgn = {asize[i.first]}; vector gotmp = {a[i.first]}; vector cotmp = {a[i.first]}; tansaku[i.first] = 4; while(!mada.empty()){ //cout << (int)mada.size() << " " << sgn.size() << " " << gotmp.size() << " " << cotmp.size() << endl; int x = mada.back(); mada.pop_back(); numssum++; nums[cnt]++; int xsgn = sgn.back(); sgn.pop_back(); goes[cnt] += gotmp.back(); //goessum += gotmp.back(); mint nowgo = gotmp.back(); goessum += nowgo; gotmp.pop_back(); come[cnt] += cotmp.back(); mint nowco = cotmp.back(); comesum += nowco; cotmp.pop_back(); ans += nowgo + mint(10).pow(xsgn) * a[ret.first]; ans += nowco * mint(10).pow(asize[ret.first]) + a[ret.first]; dp.push_back(tuple(cnt, xsgn, nowgo)); for (int j: ikeru[x]){ if (removed[j]) continue; if (tansaku[j] != 4){ tansaku[j] = 4; mada.push_back(j); sgn.push_back(asize[j] + xsgn); gotmp.push_back(nowgo * mint(10).pow(asize[j]) + a[j]); cotmp.push_back(nowco + a[j] * mint(10).pow(xsgn)); } } } cnt++; } for (auto [kaiwai, keta, atai]: dp){ //cout << atai.val() << " " << keta << endl; ans += (atai + mint(10).pow(keta) * a[ret.first]) * (numssum - nums[kaiwai]) + mint(10).pow(keta + asize[ret.first]) * (comesum - come[kaiwai]); } } for (pair i: ret.second){ solve(i.first, i.second, ikeru, sizedp, tansaku, removed, ans, a, asize); } } int main(){ int n; cin >> n; vector as(n); for (int i=0; i> as[i]; } vector asize(n); for (int i=0; i a(n); for (int i=0; i> ikeru(n, vector(0)); vector sizedp(n); vector tansaku(n); vector removed(n); for (int i=0; i> a >> b; a--; b--; ikeru[a].push_back(b); ikeru[b].push_back(a); } mint ans = 0; for (int i=0; i