#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353, inv2 = (mod + 1) / 2;

int key1[400010], key2[400010], inv[400010];
int power(int a, int b) {
    long long res = a, ans = 1;
    for (; b; b >>= 1, res = res * res % mod) if (b & 1) ans = ans * res % mod;
    return ans;
}
int Mod(int x) { return x >= mod ? x - mod : x; }
struct node {
    int son[2], fa, L, R;
    int s1, s2, ls2[2], prod;
    int cls2(int x) {
        return (1ll * ls2[0] * inv[x] + 1ll * ls2[1] * inv[x - 1]) % mod;;
    }
} t[400010];
int merge(int a, int b, int c) {
    return 1ll * a * b % mod * inv[c] % mod;
}
void up(int x) {
    int ls = t[x].son[0], rs = t[x].son[1];
    if (rs) {
        t[x].prod = merge(1, t[rs].prod, key2[x] - key1[t[rs].L]);
        t[x].s1 = Mod(t[rs].s1 + t[x].prod);
        t[x].R = t[rs].R;
    }
    else {
        t[x].prod = 1;
        t[x].s1 = 1;
        t[x].R = x;
    }

    if (ls) {
        t[x].s1 = Mod(t[x].s1 + merge(t[ls].s1, t[x].prod, key2[t[ls].R] - key1[x]));
        t[x].prod = merge(t[ls].prod, t[x].prod, key2[t[ls].R] - key1[x]);
        t[x].L = t[ls].L;
    }
    else t[x].L = x;

    if (rs) t[x].s2 = 1 + merge(1, t[rs].s2, key2[x] - key1[t[rs].L]);
    else t[x].s2 = 1;
    t[x].s2 = Mod(t[x].s2 + t[x].cls2(key2[x]));
    if (ls) t[x].s2 = Mod(t[ls].s2 + merge(t[ls].prod, t[x].s2, key2[t[ls].R] - key1[x]));
}
int nroot(int x) { return x == t[t[x].fa].son[0] || x == t[t[x].fa].son[1]; }
void rotate(int x) {
    int f = t[x].fa, g = t[f].fa, k = x == t[f].son[1], w = t[x].son[k ^ 1];
    t[x].son[k ^ 1] = f, t[f].son[k] = w;
    if (nroot(f)) t[g].son[f == t[g].son[1]] = x;
    t[w].fa = f, t[f].fa = x, t[x].fa = g;
    up(f);
}
void splay(int x) {
    while (nroot(x)) {
        int f = t[x].fa, g = t[f].fa;
        if (nroot(f)) rotate((x == t[f].son[1]) == (f == t[g].son[1]) ? f : x);
        rotate(x);
    }
    up(x);
}
void access(int x) {
    for (int y = 0; x; y = x, x = t[x].fa) {
        splay(x);
        int rs = t[x].son[1];
        t[x].ls2[key1[rs]] = Mod(t[x].ls2[key1[rs]] + t[rs].s2);
        t[x].ls2[key1[y]] = Mod(t[x].ls2[key1[y]] + mod - t[y].s2);
        t[x].son[1] = y;
        up(x);
    }
}
void link(int x, int y) {
    access(y), splay(y);
    t[y].fa = x;
    t[x].ls2[key1[y]] = Mod(t[x].ls2[key1[y]] + t[y].s2);
    up(x);
}

int n, root;
vector <int> son[400010];
int fa[400010], a[400010], b[400010];
pair <int, int> id[800010];
int tag1[400010], tag2[400010];
void dfs1(int cur, int f) {
    fa[cur] = f;
    b[cur] = a[cur];
    for (int i = 0; i < son[cur].size(); i++) {
        if (son[cur][i] == f) {
            son[cur][i] = son[cur].back();
            son[cur].pop_back();
            i--;
            continue;
        }
        dfs1(son[cur][i], cur);
        b[cur] = min(b[cur], b[son[cur][i]]);
    }
}
void rmain() {
    scanf("%d%d", &n, &root);
    for (int i = 1; i <= n; i++) {
        son[i].clear();
        t[i].son[0] = t[i].son[1] = t[i].fa = 0;
        t[i].s1 = t[i].s2 = 1;
        t[i].L = t[i].R = i;
        t[i].ls2[0] = t[i].ls2[1] = 0;
        t[i].prod = 1;
    }
    for (int i = 1; i <= n; i++) scanf("%d", a + i);
    for (int i = 1; i <= n + 1; i++) inv[i] = power(i, mod - 2);
    for (int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        son[u].push_back(v);
        son[v].push_back(u);
    }
    dfs1(root, 0);
    for (int i = 1; i <= n; i++) {
        key1[i] = 1, key2[i] = son[i].size() + 1;
    }
    for (int i = 1; i <= n; i++) id[i] = {a[i], i}, id[i + n] = {b[i], -i};
    sort(id + 1, id + 1 + 2 * n);
    reverse(id + 1, id + 1 + 2 * n);
    memset(tag1, 0, sizeof tag1);
    memset(tag2, 0, sizeof tag2);
    int ans = 0;
    for (int i = 1; i <= n * 2; i++) {
        int cur = id[i].second;
        if (cur < 0) {
            cur = -cur;
            if (cur == root) continue;
            
            int f = fa[cur];
            access(cur), splay(f);
            key1[cur] = 0;
            key2[f]--;
            up(f);
            assert(tag1[cur]);
            if (tag1[f] && tag1[cur]) {
                int sze1 = (1ll * t[f].ls2[0] * inv[key2[f]] % mod * inv[key2[f] + 1] + 1ll * t[f].ls2[1] * inv[key2[f]] % mod * inv[key2[f] - 1]) % mod;
                access(f), splay(f);
                int sze2 = t[f].s1;
                ans = (ans + 1ll * sze1 * sze2 % mod * id[i].first) % mod;
            }
        }
        else {
            tag1[cur] = 1;
            
            for (auto i : son[cur]) {
                if (tag1[i]) link(cur, i);
            }
            if (tag1[fa[cur]]) link(fa[cur], cur);
            access(cur), splay(cur);
            int sze1 = t[cur].cls2(key2[cur]) + 1;
            int sze2 = t[cur].s1;
            ans = (ans + 1ll * sze1 * sze2 % mod * id[i].first) % mod;
        }
    }
    cout << ans << endl;
}
int main() {
    freopen("dfs.in", "r", stdin);
    freopen("dfs.out", "w", stdout);
    int T;
    scanf("%d", &T);
    while (T--) rmain();
}