


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
typedef long long ll;
typedef std::pair<int,int> pii;
typedef unsigned un;
ll read()
{
	ll x=0;bool f=0;int c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=1;c=getchar();}
	while(c>='0'&&c<='9')x=x*10+(c-'0'),c=getchar();
	return f?-x:x;
}

const int mod = 998244353,inv2=(mod+1)>>1;
struct Mint
{
	int x;
	Mint(){x=0;}
	Mint(ll y)
	{
		if(0<=y&&y<mod)x=y;
		else x=(y%mod+mod)%mod;
	}
	void operator+= (Mint you){x+=you.x;if(x>=mod)x-=mod;}
	void operator-= (Mint you){x-=you.x;if(x<0)x+=mod;}
	void operator*= (Mint you){x=ll(x)*you.x%mod;}
};
Mint operator+ (Mint a,Mint b){int t=a.x+b.x;return t>=mod?t-mod:t;}
Mint operator- (Mint a,Mint b){int t=a.x-b.x;return t<0?t+mod:t;}
Mint operator* (Mint a,Mint b){return ll(a.x)*b.x%mod;}

const int MAXN = 800011;
Mint fac[MAXN],Ifac[MAXN],inv[MAXN];
void init(int n)
{
	fac[0]=1;
	for(int i=1;i<=n;++i)fac[i]=fac[i-1]*i;
	inv[1]=1;
	for(int i=2;i<=n;++i)inv[i]=Mint(mod-mod/i)*inv[mod%i];
	Ifac[0]=1;
	for(int i=1;i<=n;++i)Ifac[i]=Ifac[i-1]*inv[i];
}
Mint DecPower(int n,int m){return n>=0&&m>=0&&n>=m?fac[n]*Ifac[n-m]:0;}
std::vector<int>g[MAXN];
int fa[MAXN],a[MAXN];
int fx[MAXN],diff=0;

Mint res=0;
int sum0[MAXN],type[MAXN];
void dfs1(int u,int fa)
{
	sum0[u]= type[u]==0;
	for(auto v:g[u])
		if(v!=fa)dfs1(v,u),sum0[u]+=sum0[v];
}
void dfs2(int u,Mint pre,int fa)
{
	pre+=1;
	//printf("u=%d,pre=%d\n",u,pre.x);
	if(type[u])res+=pre;
	else pre=0;
	int c=0,deg=0;
	for(auto v:g[u])
		if(v!=fa)
		{
			c+=sum0[v]==0;
			++deg;
		}
	Mint s0=0,s1=0;
	for(int k=0;k<c;++k)s0+=DecPower(c-1,k)*fac[deg-1-k];
	for(int k=0;k<=c;++k)s1+=DecPower(c,k)*fac[deg-1-k];
	//printf("u=%d,s0=%d,s1=%d\n",u,s0.x,s1.x);
	pre*=Ifac[deg];
	for(auto v:g[u])
		if(v!=fa)dfs2(v,pre*(sum0[v]?s1:s0),u);
}
void solve()
{
	diff=0;
	int n=read(),R=read();
	for(int i=1;i<=n;++i)fx[++diff]=a[i]=read(),g[i].clear();
	std::sort(fx+1,fx+diff+1),diff=std::unique(fx+1,fx+diff+1)-fx-1;
	for(int i=1;i<=n;++i)a[i]=std::lower_bound(fx+1,fx+diff+1,a[i])-fx;
	for(int i=1;i<n;++i){int u=read(),v=read();g[u].emplace_back(v),g[v].emplace_back(u);}
	Mint ans=0;
	for(int x=1;x<=diff;++x)
	{
		for(int u=1;u<=n;++u)type[u]= a[u]>=x;
		res=0;
		dfs1(R,0),dfs2(R,0,0);
		ans+=res*(fx[x]-fx[x-1]);
		//printf("fx[x]=%d,add %d\n",fx[x],(res*(fx[x]-fx[x-1])).x);
	}
	printf("%d\n",ans.x);
}
int main()
{
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	init(400000);
	int task=read();
	while(task--)solve();
	return 0;
}
