#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(2e5)+7;
const int LOG = 20;
const int maxK = 27;
const int mod = 998244353;
int add(int x , int y){
x += y;
if (x >= mod) x -= mod;
return x;
}
void self_add(int &x , int y){
x = add(x , y);
}
int sub(int x , int y){
x -= y;
if (x < 0) x += mod;
return x;
}
void self_sub(int &x , int y){
x = sub(x , y);
}
int mul(int x , int y){
return (1ll * x * y) % mod;
}
int n , k , q , a[maxN] , tmp_a[maxN] , c[maxK];
vector<int> g[maxN];
struct lca{
int h[maxN] , up[maxN][LOG + 1] , sz[maxN] , cost[maxN];
int in[maxN] , out[maxN] , DfsTime = 0;
void dfs(int u , int par){
for (int i = 1 ; i <= LOG ; i++) up[u][i] = up[up[u][i - 1]][i - 1];
sz[u] = 1;
in[u] = ++DfsTime;
for (int v : g[u]){
if (v == par) continue;
up[v][0] = u;
h[v] = h[u] + 1;
dfs(v , u);
sz[u] += sz[v];
}
out[u] = DfsTime;
}
int k_th(int u , int k){
for (int i = LOG ; i >= 0 ; i--){
if ((k>>i)&1) u = up[u][i];
}
return u;
}
bool inside(int u , int v){
return in[u] <= in[v] && out[v] <= out[u];
}
int getNum(int u , int v){
if (inside(u , v)){
return mul(n - sz[k_th(v , h[v] - h[u] - 1)] , sz[v]);
}
if (inside(v , u)){
return mul(n - sz[k_th(u , h[u] - h[v] - 1)] , sz[u]);
}
return mul(sz[u] , sz[v]);
}
int getSize(int u , int v){
if (u == v) return n;
if (inside(u , v)){
return sz[v];
}
if (inside(v , u)){
return n - sz[k_th(u , h[u] - h[v] - 1)];
}
return sz[v];
}
void build_cost(){
for (int i = 1 ; i <= n ; i++){
int pre = n - sz[i];
cost[i] = n - 1;
for (int j : g[i]){
if (j != up[i][0]){
self_add(cost[i] , mul(pre , sz[j]));
pre += sz[j];
}
}
}
}
} sigma;
int sz[maxN] , h[maxN] , p[maxN] , nxt_sz[LOG + 1][maxN] , num[LOG + 1][maxN];
bool del[maxN];
void dfs_size(int u , int par){
sz[u] = 1;
for (int v : g[u]){
if (v != par && del[v] == 0){
dfs_size(v , u);
sz[u] += sz[v];
}
}
}
int dfs_find(int u , int par , int half){
for (int v : g[u]){
if (v != par && del[v] == 0 && sz[v] > half){
return dfs_find(v , u , half);
}
}
return u;
}
void dfs_prepare(int x , int u , int par , int d){
nxt_sz[d][u] = sigma.getSize(x , u);
num[d][u] = sigma.getNum(x , u);
for (int v : g[u]){
if (v != par && del[v] == 0){
dfs_prepare(x , v , u , d);
}
}
}
int id[maxN] , numID = 0;
void dfs_build(int u , int par , int d){
dfs_size(u , 0);
u = dfs_find(u , 0 , sz[u] / 2);
del[u] = 1;
h[u] = d;
p[u] = par;
id[u] = ++numID;
dfs_prepare(u , u , par , d);
for (int v : g[u]){
if (del[v] == 0){
dfs_build(v , u , d + 1);
}
}
}
int ans = 0 , sum[maxK][maxN] , cur[maxK][maxN] , sum_sub[maxK][maxN];
void update(int u , int t){
if (t == +1){
self_add(ans , cur[a[u]][u]);
self_add(ans , mul(sigma.cost[u] , c[a[u]]));
}
else{
self_sub(ans , cur[a[u]][u]);
self_sub(ans , mul(sigma.cost[u] , c[a[u]]));
}
for (int x = p[u] , y = u ; x != 0 ; x = p[x] , y = p[y]){
if (t == +1){
self_add(sum[a[u]][x] , nxt_sz[h[x]][u]);
self_add(sum_sub[a[u]][id[y]] , nxt_sz[h[x]][u]);
self_add(ans , mul(mul(2 , c[a[u]]) , mul(sub(sum[a[u]][x] , sum_sub[a[u]][id[y]]) , nxt_sz[h[x]][u])));
self_add(cur[a[u]][x] , mul(mul(2 , c[a[u]]) , num[h[x]][u]));
if (a[x] == a[u]){
self_add(ans , mul(mul(2 , c[a[u]]) , num[h[x]][u]));
}
}
else{
self_sub(sum[a[u]][x] , nxt_sz[h[x]][u]);
self_sub(sum_sub[a[u]][id[y]] , nxt_sz[h[x]][u]);
self_sub(ans , mul(mul(2 , c[a[u]]) , mul(sub(sum[a[u]][x] , sum_sub[a[u]][id[y]]) , nxt_sz[h[x]][u])));
self_sub(cur[a[u]][x] , mul(mul(2 , c[a[u]]) , num[h[x]][u]));
if (a[x] == a[u]){
self_sub(ans , mul(mul(2 , c[a[u]]) , num[h[x]][u]));
}
}
}
}
void solve(){
cin >> n >> k >> q;
for (int i = 1 ; i <= n ; i++){
cin >> tmp_a[i];
}
for (int i = 1 ; i <= k ; i++) cin >> c[i];
for (int i = 1 ; i < n ; i++){
int u , v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
sigma.dfs(1 , 0);
sigma.build_cost();
dfs_build(1 , 0 , 0);
//return;
for (int i = 1 ; i <= n ; i++){
a[i] = tmp_a[i];
update(i , +1);
}
cout << ans << "\n";
while (q--){
int u , x;
cin >> u >> x;
update(u , -1);
a[u] = x;
update(u , +1);
cout << ans << "\n";
}
}
#define name "fbuy"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBhbGwodikgdi5iZWdpbigpICwgdi5lbmQoKQojZGVmaW5lIHN6KHYpIGludCh2LnNpemUoKSkKI2RlZmluZSB1bnEodikgc29ydChhbGwodikpOyB2LnJlc2l6ZSh1bmlxdWUoYWxsKHYpKSAtIHYuYmVnaW4oKSk7CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBwYWlyPGludCAsIGludD4gaWk7CnR5cGVkZWYgcGFpcjxsb25nIGxvbmcgLCBpbnQ+IGxsaTsKCmNvbnN0IGludCBtYXhOID0gaW50KDJlNSkrNzsKY29uc3QgaW50IExPRyA9IDIwOwpjb25zdCBpbnQgbWF4SyA9IDI3Owpjb25zdCBpbnQgbW9kID0gOTk4MjQ0MzUzOwoKaW50IGFkZChpbnQgeCAsIGludCB5KXsKICAgIHggKz0geTsKICAgIGlmICh4ID49IG1vZCkgeCAtPSBtb2Q7CiAgICByZXR1cm4geDsKfQoKdm9pZCBzZWxmX2FkZChpbnQgJnggLCBpbnQgeSl7CiAgICB4ID0gYWRkKHggLCB5KTsKfQoKaW50IHN1YihpbnQgeCAsIGludCB5KXsKICAgIHggLT0geTsKICAgIGlmICh4IDwgMCkgeCArPSBtb2Q7CiAgICByZXR1cm4geDsKfQoKdm9pZCBzZWxmX3N1YihpbnQgJnggLCBpbnQgeSl7CiAgICB4ID0gc3ViKHggLCB5KTsKfQoKaW50IG11bChpbnQgeCAsIGludCB5KXsKICAgIHJldHVybiAoMWxsICogeCAqIHkpICUgbW9kOwp9CgppbnQgbiAsIGsgLCBxICwgYVttYXhOXSAsIHRtcF9hW21heE5dICwgY1ttYXhLXTsKdmVjdG9yPGludD4gZ1ttYXhOXTsKCnN0cnVjdCBsY2F7CiAgICBpbnQgaFttYXhOXSAsIHVwW21heE5dW0xPRyArIDFdICwgc3pbbWF4Tl0gLCBjb3N0W21heE5dOwogICAgaW50IGluW21heE5dICwgb3V0W21heE5dICwgRGZzVGltZSA9IDA7CgogICAgdm9pZCBkZnMoaW50IHUgLCBpbnQgcGFyKXsKICAgICAgICBmb3IgKGludCBpID0gMSA7IGkgPD0gTE9HIDsgaSsrKSB1cFt1XVtpXSA9IHVwW3VwW3VdW2kgLSAxXV1baSAtIDFdOwogICAgICAgIHN6W3VdID0gMTsKICAgICAgICBpblt1XSA9ICsrRGZzVGltZTsKICAgICAgICBmb3IgKGludCB2IDogZ1t1XSl7CiAgICAgICAgICAgIGlmICh2ID09IHBhcikgY29udGludWU7CiAgICAgICAgICAgIHVwW3ZdWzBdID0gdTsKICAgICAgICAgICAgaFt2XSA9IGhbdV0gKyAxOwogICAgICAgICAgICBkZnModiAsIHUpOwogICAgICAgICAgICBzelt1XSArPSBzelt2XTsKICAgICAgICB9CiAgICAgICAgb3V0W3VdID0gRGZzVGltZTsKICAgIH0KCiAgICBpbnQga190aChpbnQgdSAsIGludCBrKXsKICAgICAgICBmb3IgKGludCBpID0gTE9HIDsgaSA+PSAwIDsgaS0tKXsKICAgICAgICAgICAgaWYgKChrPj5pKSYxKSB1ID0gdXBbdV1baV07CiAgICAgICAgfQogICAgICAgIHJldHVybiB1OwogICAgfQoKICAgIGJvb2wgaW5zaWRlKGludCB1ICwgaW50IHYpewogICAgICAgIHJldHVybiBpblt1XSA8PSBpblt2XSAmJiBvdXRbdl0gPD0gb3V0W3VdOwogICAgfQoKICAgIGludCBnZXROdW0oaW50IHUgLCBpbnQgdil7CiAgICAgICAgaWYgKGluc2lkZSh1ICwgdikpewogICAgICAgICAgICByZXR1cm4gbXVsKG4gLSBzeltrX3RoKHYgLCBoW3ZdIC0gaFt1XSAtIDEpXSAsIHN6W3ZdKTsKICAgICAgICB9CiAgICAgICAgaWYgKGluc2lkZSh2ICwgdSkpewogICAgICAgICAgICByZXR1cm4gbXVsKG4gLSBzeltrX3RoKHUgLCBoW3VdIC0gaFt2XSAtIDEpXSAsIHN6W3VdKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIG11bChzelt1XSAsIHN6W3ZdKTsKICAgIH0KCiAgICBpbnQgZ2V0U2l6ZShpbnQgdSAsIGludCB2KXsKICAgICAgICBpZiAodSA9PSB2KSByZXR1cm4gbjsKICAgICAgICBpZiAoaW5zaWRlKHUgLCB2KSl7CiAgICAgICAgICAgIHJldHVybiBzelt2XTsKICAgICAgICB9CiAgICAgICAgaWYgKGluc2lkZSh2ICwgdSkpewogICAgICAgICAgICByZXR1cm4gbiAtIHN6W2tfdGgodSAsIGhbdV0gLSBoW3ZdIC0gMSldOwogICAgICAgIH0KICAgICAgICByZXR1cm4gc3pbdl07CiAgICB9CgogICAgdm9pZCBidWlsZF9jb3N0KCl7CiAgICAgICAgZm9yIChpbnQgaSA9IDEgOyBpIDw9IG4gOyBpKyspewogICAgICAgICAgICBpbnQgcHJlID0gbiAtIHN6W2ldOwogICAgICAgICAgICBjb3N0W2ldID0gbiAtIDE7CiAgICAgICAgICAgIGZvciAoaW50IGogOiBnW2ldKXsKICAgICAgICAgICAgICAgIGlmIChqICE9IHVwW2ldWzBdKXsKICAgICAgICAgICAgICAgICAgICBzZWxmX2FkZChjb3N0W2ldICwgbXVsKHByZSAsIHN6W2pdKSk7CiAgICAgICAgICAgICAgICAgICAgcHJlICs9IHN6W2pdOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9IHNpZ21hOwoKaW50IHN6W21heE5dICwgaFttYXhOXSAsIHBbbWF4Tl0gLCBueHRfc3pbTE9HICsgMV1bbWF4Tl0gLCBudW1bTE9HICsgMV1bbWF4Tl07CmJvb2wgZGVsW21heE5dOwoKdm9pZCBkZnNfc2l6ZShpbnQgdSAsIGludCBwYXIpewogICAgc3pbdV0gPSAxOwogICAgZm9yIChpbnQgdiA6IGdbdV0pewogICAgICAgIGlmICh2ICE9IHBhciAmJiBkZWxbdl0gPT0gMCl7CiAgICAgICAgICAgIGRmc19zaXplKHYgLCB1KTsKICAgICAgICAgICAgc3pbdV0gKz0gc3pbdl07CiAgICAgICAgfQogICAgfQp9CgppbnQgZGZzX2ZpbmQoaW50IHUgLCBpbnQgcGFyICwgaW50IGhhbGYpewogICAgZm9yIChpbnQgdiA6IGdbdV0pewogICAgICAgIGlmICh2ICE9IHBhciAmJiBkZWxbdl0gPT0gMCAmJiBzelt2XSA+IGhhbGYpewogICAgICAgICAgICByZXR1cm4gZGZzX2ZpbmQodiAsIHUgLCBoYWxmKTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gdTsKfQoKdm9pZCBkZnNfcHJlcGFyZShpbnQgeCAsIGludCB1ICwgaW50IHBhciAsIGludCBkKXsKICAgIG54dF9zeltkXVt1XSA9IHNpZ21hLmdldFNpemUoeCAsIHUpOwogICAgbnVtW2RdW3VdID0gc2lnbWEuZ2V0TnVtKHggLCB1KTsKICAgIGZvciAoaW50IHYgOiBnW3VdKXsKICAgICAgICBpZiAodiAhPSBwYXIgJiYgZGVsW3ZdID09IDApewogICAgICAgICAgICBkZnNfcHJlcGFyZSh4ICwgdiAsIHUgLCBkKTsKICAgICAgICB9CiAgICB9Cn0KCmludCBpZFttYXhOXSAsIG51bUlEID0gMDsKCnZvaWQgZGZzX2J1aWxkKGludCB1ICwgaW50IHBhciAsIGludCBkKXsKICAgIGRmc19zaXplKHUgLCAwKTsKICAgIHUgPSBkZnNfZmluZCh1ICwgMCAsIHN6W3VdIC8gMik7CiAgICBkZWxbdV0gPSAxOwogICAgaFt1XSA9IGQ7CiAgICBwW3VdID0gcGFyOwogICAgaWRbdV0gPSArK251bUlEOwogICAgZGZzX3ByZXBhcmUodSAsIHUgLCBwYXIgLCBkKTsKICAgIGZvciAoaW50IHYgOiBnW3VdKXsKICAgICAgICBpZiAoZGVsW3ZdID09IDApewogICAgICAgICAgICBkZnNfYnVpbGQodiAsIHUgLCBkICsgMSk7CiAgICAgICAgfQogICAgfQp9CgppbnQgYW5zID0gMCAsIHN1bVttYXhLXVttYXhOXSAsIGN1clttYXhLXVttYXhOXSAsIHN1bV9zdWJbbWF4S11bbWF4Tl07Cgp2b2lkIHVwZGF0ZShpbnQgdSAsIGludCB0KXsKICAgIGlmICh0ID09ICsxKXsKICAgICAgICBzZWxmX2FkZChhbnMgLCBjdXJbYVt1XV1bdV0pOwogICAgICAgIHNlbGZfYWRkKGFucyAsIG11bChzaWdtYS5jb3N0W3VdICwgY1thW3VdXSkpOwogICAgfQogICAgZWxzZXsKICAgICAgICBzZWxmX3N1YihhbnMgLCBjdXJbYVt1XV1bdV0pOwogICAgICAgIHNlbGZfc3ViKGFucyAsIG11bChzaWdtYS5j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V1dKSAsIG51bVtoW3hdXVt1XSkpOwogICAgICAgICAgICBpZiAoYVt4XSA9PSBhW3VdKXsKICAgICAgICAgICAgICAgIHNlbGZfc3ViKGFucyAsIG11bChtdWwoMiAsIGNbYVt1XV0pICwgbnVtW2hbeF1dW3VdKSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KCnZvaWQgc29sdmUoKXsKICAgIGNpbiA+PiBuID4+IGsgPj4gcTsKICAgIGZvciAoaW50IGkgPSAxIDsgaSA8PSBuIDsgaSsrKXsKICAgICAgICBjaW4gPj4gdG1wX2FbaV07CiAgICB9CiAgICBmb3IgKGludCBpID0gMSA7IGkgPD0gayA7IGkrKykgY2luID4+IGNbaV07CiAgICBmb3IgKGludCBpID0gMSA7IGkgPCBuIDsgaSsrKXsKICAgICAgICBpbnQgdSAsIHY7CiAgICAgICAgY2luID4+IHUgPj4gdjsKICAgICAgICBnW3VdLnB1c2hfYmFjayh2KTsKICAgICAgICBnW3ZdLnB1c2hfYmFjayh1KTsKICAgIH0KICAgIHNpZ21hLmRmcygxICwgMCk7CiAgICBzaWdtYS5idWlsZF9jb3N0KCk7CiAgICBkZnNfYnVpbGQoMSAsIDAgLCAwKTsKICAgIC8vcmV0dXJuOwogICAgZm9yIChpbnQgaSA9IDEgOyBpIDw9IG4gOyBpKyspewogICAgICAgIGFbaV0gPSB0bXBfYVtpXTsKICAgICAgICB1cGRhdGUoaSAsICsxKTsKICAgIH0KICAgIGNvdXQgPDwgYW5zIDw8ICJcbiI7CiAgICB3aGlsZSAocS0tKXsKICAgICAgICBpbnQgdSAsIHg7CiAgICAgICAgY2luID4+IHUgPj4geDsKICAgICAgICB1cGRhdGUodSAsIC0xKTsKICAgICAgICBhW3VdID0geDsKICAgICAgICB1cGRhdGUodSAsICsxKTsKICAgICAgICBjb3V0IDw8IGFucyA8PCAiXG4iOwogICAgfQp9CgojZGVmaW5lIG5hbWUgImZidXkiCgppbnQgbWFpbigpewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsgY291dC50aWUoMCk7CiAgICBpZiAoZm9wZW4obmFtZSIuSU5QIiAsICJyIikpewogICAgICAgIGZyZW9wZW4obmFtZSIuSU5QIiAsICJyIiAsIHN0ZGluKTsKICAgICAgICBmcmVvcGVuKG5hbWUiLk9VVCIgLCAidyIgLCBzdGRvdXQpOwogICAgfQogICAgaW50IHQgPSAxOyAvL2NpbiA+PiB0OwogICAgd2hpbGUgKHQtLSkgc29sdmUoKTsKICAgIHJldHVybiAwOwp9Cg==