#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int mu=998244353,N=400005;
int to[N<<1],edge,Next[N<<1],last[N],dep[N],siz[N],a[N],b[N],cnt;
int mi[N],dp[5005][5005],inv[N],n,rt,x,y,T;
void reduce(int &x){
	x+=x>>31&mu;
}
vector<int> e[N];
void add(int x,int y){
	to[++edge]=y;
	Next[edge]=last[x];
	last[x]=edge;
}
void dfs(int x,int y){
	mi[x]=a[x],siz[x]=1;
	for (int i=last[x];i;i=Next[i]){
		int u=to[i];
		if (u==y) continue;
		dfs(u,x);
		e[x].push_back(u);
		siz[x]+=siz[u];
		mi[x]=std::min(mi[x],mi[u]);
	}
}
void DP(int x){
	dp[x][a[x]]=1;
	for (auto u:e[x]) DP(u);
	for (auto u:e[x]){
		int pu=0,p=1,nn=0,sum=0;
		for (int k=mi[u];k<=a[u];k++){
			reduce(sum+=dp[u][k]-mu);
			int t=dp[u][k];
			if (!t) continue;
			while (pu<(int)e[x].size() && mi[e[x][pu]]<=k){
				pu++;
				if (e[x][pu-1]==u) continue;
				nn++;
				int tp=(ll)inv[nn+1]*inv[nn]%mu;
				reduce(p-=tp);
			}
			reduce(dp[x][k]+=(ll)p*t%mu-mu);
		}
		int k=mi[u],now=0;
		for (auto v:e[x]){
			while (k<mi[v]) reduce(sum-=dp[u][k]),k++;
			if (v==u) continue;
			now++;
			int tp=(ll)inv[now+1]*inv[now]%mu;
			reduce(dp[x][mi[v]]+=(ll)sum*tp%mu-mu);
		}
	}
	for (int i=a[x]+1;i<=cnt;i++){
		reduce(dp[x][a[x]]+=dp[x][i]-mu);
		dp[x][i]=0;
	}
}
bool cmp(int x,int y){
	return mi[x]<mi[y];
}
void solve(){
	edge=0;
	for (int i=1;i<=n;i++) last[i]=0,e[i].clear();
	scanf("%d%d",&n,&rt);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]),b[i]=a[i];
	std::sort(b+1,b+n+1);
	cnt=std::unique(b+1,b+n+1)-b-1;
	for (int i=1;i<=n;i++) a[i]=std::lower_bound(b+1,b+cnt+1,a[i])-b;
	for (int i=1;i<n;i++){
		scanf("%d%d",&x,&y);
	//	d[x]++,d[y]++;
		add(x,y),add(y,x);
	}
	dfs(rt,0);
	for (int i=1;i<=n;i++)
		std::sort(e[i].begin(),e[i].end(),cmp);
	DP(rt);
	int ans=0;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=cnt;j++)
			reduce(ans+=(ll)dp[i][j]*b[j]%mu-mu),dp[i][j]=0;
	printf("%d\n",ans);
}
int ksm(int x,int y){
	int ret=1;
	for (;y;y>>=1,x=(ll)x*x%mu)
		if (y&1) ret=(ll)ret*x%mu;
	return ret;
}
int main(){
	freopen("dfs_ex1.in","r",stdin);
	inv[1]=inv[0]=1;
	for (int i=2;i<400000;i++)
		inv[i]=ksm(i,mu-2);
	scanf("%d",&T);
	while (T--) solve();
}
