#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using db = double;
using pii = pair<int, int>;

const int N = 1e6 + 5;
const ll P = 998244353;

ll qpow(ll a, ll b)
{
	ll res = 1;
	for (; b; b >>= 1, a = a * a % P)
		if (b & 1) res = res * a % P;
	return res;
}

ll fac[N], ivf[N];

void init(int n)
{
	fac[0] = 1;
	for (int i = 1; i <= n; i++)
		fac[i] = fac[i - 1] * i % P;
	ivf[n] = qpow(fac[n], P - 2);
	for (int i = n - 1; i >= 0; i--)
		ivf[i] = ivf[i + 1] * (i + 1) % P;
}

int n, root;
int a[N];
pii t[N];
vector<int> e[N];
int fa[N], sz[N];

void dfs1(int u)
{
	sz[u] = 1;
	for (int v : e[u])
		if (v != fa[u])
			fa[v] = u, dfs1(v), sz[u] += sz[v];
}

int nex;
ll f[N];
bool dan[N], qin[N];
ll sum0[N], sum1[N];
ll op;

void dfs2(int u)
{
	dan[u] = a[u] < a[nex];
	int ns = 0, ss = 0;
	for (int v : e[u])
		if (v != fa[u])
		{
			dfs2(v);
			ns++, ss += !dan[v];			
			dan[u] |= dan[v];
		}
	if (dan[u]) return;
	if (!ns) {
		f[u] = 1; return;
	}
	ll sum0 = 0, sum1 = 0;
	ns--;
	for (int i = 1; i <= min(ss + 1, ns); i++)
		sum0 = (sum0 + fac[ns - i + 1] * ivf[ss - i + 1]) % P;
	sum0 = sum0 * fac[ss] % P * ivf[ns] % P;
	ss--;
	cout << ss << endl;
	for (int i = 1; i <= ss + 1; i++)
		sum1 = (sum1 + fac[ns - i + 1] * ivf[ss - i + 1]) % P;
	sum1 = sum1 * fac[ss] % P * ivf[ns] % P;
	if (qin[u] && u != nex)
	{
		for (int v : e[u])
			if (v != fa[u] && qin[v])
			{
				if (dan[v])
					f[u] = (f[u] + sum0 * sz[v] % P * f[v]) % P;
				else
					f[u] = (f[u] + sum1 * sz[v] % P * f[v]) % P;
			}
		return;
	}
	f[u] = 1;
	for (int v : e[u])
		if (v != fa[u])
		{
			if (dan[v])
				f[u] = (f[u] + sum0 * sz[v] % P * f[v]) % P;
			else
				f[u] = (f[u] + sum1 * sz[v] % P * f[v]) % P;
		}
}

void task()
{
	scanf("%d%d", &n, &root);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]),
		t[i] = {a[i], i};
	for (int i = 1; i < n; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		e[u].push_back(v);
		e[v].push_back(u);
	}
	sort(t + 1, t + n + 1);
	for (int i = 1; i <= n; i++)
		a[t[i].second] = i;
	fa[root] = 0, dfs1(root);
	ll ans = 0;
	for (int x = 1; x <= n; x++)
	{
		nex = x;
		for (int i = 1; i <= n; i++)
			dan[i] = f[i] = qin[i] = 0;
		for (int i = x; i; i = fa[i])
			qin[i] = 1;
		op = 0;
		dfs2(root);
		for (int i = x; i; i = fa[i])
			op = (op + f[i]) % P;
		ans = (ans + op * t[a[x]].first) % P;
	}
	printf("%lld\n", ans);
}

int main()
{
	init(1e6);
	int T;
	scanf("%d", &T);
	for (; T--;) task();
}

/*
4
3 3
3 3 4
3 1
3 2

*/