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

template <typename T>
inline void read(T &f) {
	f = 0; int fu = 0; char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') fu = 1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		f = f * 10 + (c - '0');
		c = getchar();
	}
	if (fu) f = -f;
}

const int md = 998244353;

inline int add(int x, int y) {
	if (x + y >= md) return x + y - md;
	return x + y;
}

inline int sub(int x, int y) {
	if (x < y) return x - y + md;
	return x - y;
}

inline int mul(int x, int y) { return 1ull * x * y % md; }

inline int fpow(int x, int y) {
	int ans = 1;
	while (y) {
		if (y & 1) ans = mul(ans, x);
		y >>= 1; x = mul(x, x);
	}
	return ans;
}

const int N = 4e5 + 5;

vector <int> adj[N];
int fa[N], cnt[N], ban[N], bans[N], a[N], dep[N], id[N], inv[N], seq[N], val[N];
int T, n, rt, ans, all, len;

bool cmp(int i, int j) { return a[i] < a[j]; }

void dfs1(int u) {
	seq[++len] = u;
	for (auto v : adj[u]) {
		if (v == fa[u]) continue;
		dep[v] = dep[u] + 1; fa[v] = u; dfs1(v);
	}
}

int dfs2(int u) {
	int sums = 1;
	for (auto v : adj[u]) {
		if (v == fa[u]) continue;
		int coef = (bans[v] ? inv[cnt[u]] : inv[cnt[u] + 1]);
		int tmp = dfs2(v);
		sums = add(sums, mul(tmp, coef));
	}
	if (!ban[u]) all = add(all, sums);
	return ban[u] ? 0 : sums;
}

int main() {
	freopen("dfs.in", "r", stdin);
	freopen("dfs.out", "w", stdout);
	for (int i = 1; i <= 400000; i++) inv[i] = fpow(i, md - 2);
	read(T);
	while (T--) {
		read(n); read(rt); ans = 0;
		for (int i = 1; i <= n; i++) {
			adj[i].clear();
			ban[i] = bans[i] = cnt[i] = 0;
		}
		for (int i = 1; i <= n; i++) read(a[i]), id[i] = i;
		sort(id + 1, id + n + 1, cmp);
		for (int i = 1; i < n; i++) {
			int u, v;
			read(u); read(v);
			adj[u].push_back(v);
			adj[v].push_back(u);
		}
		if ((int)adj[rt].size() == n - 1 && n > 5000) {
			int ans = 0, cnt = 0;
			for (int i = 1; i <= n; i++) ans = add(ans, a[i] % md);
			ans = add(ans, mul(n - 1, a[id[1]]));
			for (int _ = 1; _ <= n; _++) {
				int u = id[_];
				if (u == rt) break;
				++cnt;
				ans = add(ans, mul(mul(n - 1 - cnt, inv[cnt + 1]), a[id[_ + 1]] - a[id[_]]));
			}
			printf("%d\n", ans);
			continue;
		}
		len = 0; fa[rt] = 0; dep[rt] = 1; dfs1(rt); all = 0;
		for (int i = 1; i <= n; i++) all = add(all, dep[i]);
		ans = mul(a[id[1]], all);
		for (int _ = 1; _ < n; _++) {
			int u = id[_];
			ban[u] = 1;
			while (!bans[u] && u != rt) {
				bans[u] = 1;
				++cnt[fa[u]];
				u = fa[u];
			}
			all = 0;
			for (int j = 1; j <= len; j++) val[j] = 1;
			for (int j = len; j >= 1; j--) {
				int u = seq[j];
				if (ban[u]) continue;
				all = add(all, val[u]);
				if (!ban[fa[u]]) {
					if (bans[u]) val[fa[u]] = add(val[fa[u]], mul(val[u], inv[cnt[fa[u]]]));
					else val[fa[u]] = add(val[fa[u]], mul(val[u], inv[cnt[fa[u]] + 1]));
				}
			}
			ans = add(ans, mul(all, a[id[_ + 1]] - a[id[_]]));
		}
		printf("%d\n", ans);
	}
	return 0;
}
