#include <bits/stdc++.h>
using namespace std;

namespace Legendgod {
namespace Read {
//    #define Fread
    #ifdef Fread
    const int Siz = (1 << 21) + 5;
    char *iS, *iT, buf[Siz];
    #define gc() ( iS == iT ? (iT = (iS = buf) + fread(buf, 1, Siz, stdin), iS == iT ? EOF : *iS ++) : *iS ++ )
    #define getchar gc
    #endif // Fread

    template <typename T>
    void r1(T &x) {
        x = 0; char c(getchar()); int f(1);
        for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
        for(; isdigit(c); c = getchar()) x = (x * 10) + (c ^ 48);
        x *= f;
    }
    template <typename T, typename...Args>
    void r1(T &x, Args&...arg) {
        r1(x), r1(arg...);
    }
}

using Read::r1;

const int maxn = 5e3 + 5;
int n, m, head[maxn], cnt(1);
struct Edge {
    int to, next;
}edg[maxn << 1];
void add(int u,int v) {
    edg[++ cnt] = (Edge) {v, head[u]}, head[u] = cnt;
}
const int mod = 998244353;

int ksm(int x,int mi) {
    int res(1);
    while(mi) {
        if(mi & 1) res = 1ll * res * x % mod;
        mi >>= 1;
        x = 1ll * x * x % mod;
    }
    return res;
}

int Inv(int x) { return ksm(x, mod - 2); }
int fa[maxn];
int f[maxn], sm[maxn], rt;
int a[maxn], sn[maxn];
void dfs(int p,int pre) {
    sm[p] = a[p], fa[p] = pre;
    for(int i = head[p];i;i = edg[i].next) {
        int to = edg[i].to; if(to == pre) continue;
        dfs(to, p), ++ sn[p];
        sm[p] = (sm[p] + sm[to]) % mod;
    }
}

void Solve() {
    int i, j;
    r1(n, rt);
    for(i = 1; i <= n; ++ i) head[i] = 0; cnt = 1;
    for(i = 1; i <= n; ++ i) r1(a[i]);
    for(i = 1; i < n; ++ i) {
        int u, v; r1(u, v), add(u, v), add(v, u);
    }
    dfs(rt, 0);
    int res(0);
    for(int x = 1; x <= n; ++ x) {
        int y = fa[x], pr(a[x]), ppr(x);
        res = (res + a[x]) % mod;
        while(y) {
            int now(0);
            memset(f, 0, sizeof(f));
            for(i = head[y];i;i = edg[i].next) {
                int to = edg[i].to; if(to == fa[y]) continue;
                if(to == ppr) continue;
                for(j = sn[y]; j >= 1; -- j) f[j] = (f[j] + f[j - 1] + sm[to]) % mod;
            }
            int t = 1;
            for(i = 0; i <= sn[y] - 1; ++ i) {
                now = (now + 1ll * t * (pr + f[i])) % mod;
                t = 1ll * t * Inv(i + 1) % mod;
            }
//            printf("(%d, %d) = %d\n", x, y, now);
            res = (res + now + a[y]) % mod;
            pr = now, ppr = y, y = fa[y];
        }
    }
    printf("%d\n", res);
}

signed main() {
    freopen("dfs.in", "r", stdin);
    freopen("dfs.out", "w", stdout);
    int i, j, T;
    r1(T); while(T --) Solve();
    return 0;
}

}

signed main() { return Legendgod::main(), 0; }
