#include<bits/stdc++.h>
#define mod 998244353
#define inv 499122177
using namespace std;
int n,root,len,nex[10000000],wen[10000000],hea[10000000],f[5005][5005],a[10000000],ans,p[1000000],b[1000000],id[100000],m1,ind[100000];
bool cmp(int x,int y)
{
	return a[x]<a[y];
}
void add(int x,int y)
{
	++len;
	nex[len]=hea[x];
	wen[len]=y;
	hea[x]=len;
}
void mo(int &x)
{
	if(x>=mod) x-=mod;
}
void dfs(int x,int y)
{
	b[x]=x;
	f[x][id[x]]=1;
	for(int i=hea[x];i;i=nex[i])
		if(wen[i]!=y)
		{
			dfs(wen[i],x);
			int sum=0;
			if(a[b[wen[i]]]<a[b[x]]) b[x]=b[wen[i]];
			for(int j=id[x];j<=m1;j++) mo(sum+=f[wen[i]][j]),f[wen[i]][j]=0;
			f[wen[i]][id[x]]=sum;
		}
	for(int i=hea[x];i;i=nex[i])
		for(int j=hea[x];j;j=nex[j])
			if(i!=j&&wen[i]!=y&&wen[j]!=y)
			{
				int sum=0;
				for(int k=id[b[wen[j]]]+1;k<=m1;k++) mo(sum+=f[wen[i]][k]),f[wen[i]][k]=1ll*f[wen[i]][k]*inv%mod;
				f[wen[i]][id[b[wen[j]]]]=(f[wen[i]][id[b[wen[j]]]]+1ll*sum*inv%mod)%mod;
			}
	for(int i=hea[x];i;i=nex[i])
		if(wen[i]!=y)
			for(int j=1;j<=m1;j++) mo(f[x][j]+=f[wen[i]][j]);
	for(int j=1;j<=m1;j++) mo(ans+=1ll*f[x][j]*a[ind[j]]%mod); 
}
void solve()
{
	scanf("%d%d",&n,&root);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]),hea[i]=0,p[i]=i;
	sort(p+1,p+n+1,cmp);
	m1=0;
	for(int i=1;i<=n;i++)
	{
		if(i==1||a[p[i]]!=a[p[i-1]]) ++m1,ind[m1]=p[i];
		id[p[i]]=m1;
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m1;j++) f[i][j]=0;
	for(int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	ans=0;
	dfs(root,0);
	printf("%d\n",ans);
}
int main()
{
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	int t;
	scanf("%d",&t);
	while(t)
	{
		--t;
		solve();
	}
	return 0;
}
