#include <bits/stdc++.h>

using namespace std;
#define P 998244353

vector<int> e[400001];
int T, n, root, a[400001], u, v, mina[400001];
long long inv[400001];
int f[5001][5001], g[5001][5001];

bool cmp(int x, int y) { return mina[x] < mina[y]; }
void inc(int& x, long long y) { x = (x + y) % P; }

void dfs(int rt, int fa) {
	mina[rt] = rt;
	for (int i : e[rt])
		if (i != fa) {
			dfs(i, rt);
			if (a[mina[i]] < a[mina[rt]]) mina[rt] = mina[i];
		}
}

void DP(int rt, int fa) {
	for (int i = 0; i <= n; ++i) f[rt][i] = min(a[i], a[rt]);
	vector<int> son;
	for (int i : e[rt])
		if (i != fa) {
			son.push_back(i);
			DP(i, rt);
		}
	if (son.empty()) return;
	sort(son.begin(), son.end(), cmp);
	int m = son.size();
	for (int minn = 0; minn <= n; ++minn) {
		g[son[0]][minn] = f[son[0]][minn];
		for (int i = 1; i < m; ++i)
			g[son[i]][minn] = (g[son[i - 1]][minn] + f[son[i]][minn]) % P;
	}
	for (int minn = 0; minn <= n; ++minn) {
		int i = 0;
		while (i < m && a[mina[son[i]]] < a[minn]) {
			inc(f[rt][minn], inv[i + 1] * inv[i + 2] % P *
					(g[son[m - 1]][mina[son[i]]] - g[son[i]][mina[son[i]]]));
			if (i)
				inc(f[rt][minn], inv[i] * inv[i + 1] % P *
						g[son[i - 1]][mina[son[i]]]);
			++i;
		}
		if (i) {
			inc(f[rt][minn], inv[i] * g[son[i - 1]][minn]);
			inc(f[rt][minn], inv[i + 1] *
					(g[son[m - 1]][minn] - g[son[i - 1]][minn]));
		} else inc(f[rt][minn], g[son[m - 1]][minn]);
	}
}

int main() {
	freopen("dfs.in", "r", stdin);
	freopen("dfs.out", "w", stdout);
	inv[1] = 1;
	for (int i = 2; i <= 400000; ++i)
		inv[i] = (P - P / i) * inv[P % i] % P;
	scanf("%d", &T);
	a[0] = INT_MAX;
	while (T--) {
		scanf("%d%d", &n, &root);
		for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), e[i].clear();
		for (int i = 1; i < n; ++i) {
			scanf("%d%d", &u, &v);
			e[u].push_back(v);
			e[v].push_back(u);
		}
		dfs(root, 0);
		memset(f, 0, sizeof(f));
		memset(g, 0, sizeof(g));
		DP(root, 0);
		int ans = 0;
		for (int i = 1; i <= n; ++i) inc(ans, f[i][0]);
		printf("%d\n", ans);
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
