#include<bits/stdc++.h>
#define ll long long
#define Mod(x) ((x>=MOD)&&(x-=MOD))
using namespace std;
const int MOD = 998244353;
const int MAXN = 4e5+5;
const ll INV2 = (MOD+1)/2;
ll fac[MAXN],f[MAXN],W[MAXN],A[MAXN];
vector <int> e[MAXN];
int dfn[MAXN],td,ed[MAXN],n,root,d[MAXN];
ll qpw(ll x,ll b)
{
	ll r=1;
	for(;b;b>>=1,x=x*x%MOD) if(b&1) r=r*x%MOD;
	return r;
}
void predfs(int p,int fa)
{
	dfn[p]=++td;W[p]=A[p];
	for(int i=0;i<e[p].size();++i)
	{
		int to=e[p][i];
		if(to==fa) continue;
		predfs(to,p);
		W[p]=min(W[p],W[to]);Mod(W[p]);
	}
	ed[p]=td;
}
bool in(int p,int son)
{
	return dfn[p]<=dfn[son]&&ed[son]<=ed[p];
}
bool check_lian()
{
	int cnt=0;
	for(int i=1;i<=n;++i) if(i!=root&&d[i]==1) ++cnt;
	return cnt==1;
}
bool check_juhua()
{
	return d[root]=n-1;
}
namespace pt5
{
	int st[MAXN],top,id[MAXN],L[MAXN],R[MAXN];
	ll Ans=0;
	void Solve()
	{
		Ans=0;
		for(int i=1;i<=n;++i) id[dfn[i]]=i;
		for(int i=1;i<=n;++i) L[i]=0,R[i]=n+1;
		for(int i=1;i<=n;++i)
		{
			while(top&&A[id[i]]<=A[id[st[top]]])
			{
				R[st[top]]=i;
				--top;
			}
			if(top) L[i]=st[top];	
			st[++top]=i;
		}
		for(int i=1;i<=n;++i)
		{
			Ans+=(R[i]-i)*(i-L[i])%MOD*A[id[i]]%MOD;
			Mod(Ans);
		}
		printf("%lld\n",Ans);
	}
}
namespace pt10
{
	ll df[MAXN],B[MAXN],Ans,tot;
	ll C(int n,int m)
	{
		return fac[n]*df[m]%MOD*df[n-m]%MOD;
	}
	void Solve()
	{
		Ans=0;
		df[n]=qpw(fac[n],MOD-2);
		for(int i=0;i<n;++i) df[i]=df[i+1]*(i+1)%MOD;
		for(int i=1;i<=n;++i) if(A[i]>A[root]) A[i]=A[root];
		for(int i=1;i<=n;++i) if(i!=root) B[++tot]=A[i];
		sort(B+1,B+1+tot);
		for(int i=1;i<=n;++i)
		{
			if(i==root) continue;
			int p=lower_bound(B+1,B+1+tot,A[p])-B;
			int cnt=tot-p+1;
			Ans+=C(n-1,cnt)*fac[cnt-1]%MOD*cnt[n-1-cnt]%MOD;
			Mod(Ans);
		}
	}
}
int main()
{
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);


	fac[0]=1;
	for(int i=1;i<=400000;++i) fac[i]=fac[i-1]*i%MOD;
	int T;scanf("%d",&T);
	while(T--)
	{
		scanf("%d %d",&n,&root);td=0;
		for(int i=1;i<=n;++i) e[i].clear();
		for(int i=1;i<=n;++i) scanf("%lld",&A[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);
			++d[u];++d[v];
		}
		predfs(root,0);
		if(check_lian()) pt5::Solve();
		if(check_juhua()) pt10::Solve();
	}
	return 0;
}
