#include #include using namespace std; using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; using mint = modint998244353; const int mod = 998244353; //const int INF = 1e9; //const long long LINF = 1e18; //const bool debug = false; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } vector pw10(21); struct ReRooting { struct Value { mint val; int sz; Value() :val(0), sz(0) {} Value(mint _val, int _sz) :val(_val), sz(_sz) {} }; struct Data { mint val; mint cnt; Data() :val(0), cnt(0) {} Data(mint _val, mint _cnt) :val(_val), cnt(_cnt) {} }; vector>to; vectordp; vectorans; vectordata; int sz; ReRooting(int _sz) :sz(_sz) { to.resize(sz); dp.reserve(sz); ans.resize(sz); data.resize(sz); } void ReadData() { rep(i, sz) { string s; cin >> s; data[i] = { stoll(s),(int)s.size() }; } } void ReadGraph() { rep(i, sz - 1) { int a, b; cin >> a >> b; a--; b--; to[a].push_back(b); to[b].push_back(a); } } Data merge(const Data &lh, const Data &rh, int idx) { Data ret = lh; ret.cnt += rh.cnt; ret.val += rh.val * pw10[data[idx].sz]; ret.val += data[idx].val * rh.cnt; return ret; } Data mergeR(const Data &lh, const Data &rh, int idx) { Data ret = lh; ret.cnt -= rh.cnt; ret.val -= rh.val * pw10[data[idx].sz]; ret.val -= data[idx].val * rh.cnt; return ret; } void dfs(int v, int p = -1) { Data val = { data[v].val,1 }; for (auto nv : to[v]) { if (p == nv)continue; dfs(nv, v); val = merge(val, dp[nv], v); } dp[v] = val; } void dfsR(int v, int p = -1) { ans[v] = dp[v]; if (-1 != p)ans[v] = merge(ans[v], dp[p], v); for (auto nv : to[v]) { if (p == nv)continue; dp[v] = mergeR(ans[v], dp[nv], v); dfsR(nv, v); } } void output() { mint result = 0; rep(i, sz)result += ans[i].val; cout << result.val() << endl; } void solve() { ReadData(); ReadGraph(); dfs(0); dfsR(0); output(); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); pw10[0] = 1; rep(i, 20) pw10[i + 1] = pw10[i] * 10; int n; cin >> n; ReRooting reroot(n); reroot.solve(); return 0; }