#include<bits/stdc++.h>
using namespace std;
const int N=4e5+5,mod=998244353;
int t,n,rt,m,a[N],b[N],x,y,fa[N],inv[N],vis[N],ans;
vector<int>v[N];
namespace link{
	int top,s[N],L[N],R[N],id[N];
	void dfs(int x,int d){
		id[d]=x;
		for(int y:v[x])
			if(y!=fa[x]) fa[y]=x,dfs(y,d+1);
	}
	void work(){
		for(int i=1;i<=n;i++) scanf("%d",&a[i]),v[i].clear(),fa[i]=0;
		for(int i=1;i<n;i++){
			scanf("%d%d",&x,&y);
			v[x].push_back(y),v[y].push_back(x);
		}
		dfs(rt,1);
		for(int i=1;i<=n;i++) b[i]=a[id[i]];
		top=0;
		for(int i=1;i<=n;i++){
			while(top&&b[s[top]]>b[i]) R[s[top--]]=i;
			s[++top]=i;
		}
		while(top) R[s[top--]]=n+1;
		for(int i=n;i>=1;i--){
			while(top&&b[s[top]]>=b[i]) L[s[top--]]=i;
			s[++top]=i;
		}
		while(top) L[s[top--]]=0;
		for(int i=1;i<=n;i++) ans=(ans+1ll*(i-L[i])*(R[i]-i)%mod*b[i]%mod)%mod;
		printf("%d\n",ans);
	}
}
void dfs(int x){
	for(int y:v[x])
		if(y!=fa[x]) fa[y]=x,dfs(y);
}
void dfs2(int x,int s,int t,int p,int mn){
	vis[x]=1,mn=min(mn,a[x]);
	if(x==t){vis[x]=0,ans=(ans+1ll*p*b[mn]%mod)%mod;return ;}
	int cnt=0;
	for(int y:v[x]) if(!vis[y]&&y!=fa[x]) cnt++;
	for(int y:v[x])
		if(!vis[y]&&y!=fa[x]) dfs2(y,s,t,1ll*p*inv[cnt]%mod,mn);
	if(!cnt&&x!=s) dfs2(fa[x],s,t,p,mn);
	vis[x]=0;
}
signed main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&rt),inv[1]=1,ans=0;
		for(int i=2;i<=n;i++) inv[i]=1ll*inv[mod%i]*(mod-mod/i)%mod;
		if(n>10){link::work();continue;}
		for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i],v[i].clear(),fa[i]=0;
		sort(b+1,b+1+n),m=unique(b+1,b+1+n)-b-1;
		for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+1+m,a[i])-b;
		for(int i=1;i<n;i++){
			scanf("%d%d",&x,&y);
			v[x].push_back(y),v[y].push_back(x);
		}
		dfs(rt);
		for(int i=1;i<=n;i++)
			for(int x=i;x;x=fa[x]) dfs2(x,x,i,1,1e9);
		printf("%d\n",ans);
	}
	return 0;
}
