#include<bits/stdc++.h>
using namespace std;
const int N=4e5+7;
const int P=998244353;
int T,n,root,u,v,res,mn[N],deg[N],dep[N],a[N],fac[N],inv[N];
vector<int>vec[N],son[N];
int qpow(int a,int b){
	int res=1;
	while(b){
		if(b&1)
			res=1ll*res*a%P;
		a=1ll*a*a%P;
		b>>=1;
	}
	return res;
}
namespace line{
	int res,b[N],l[N],r[N];
	int s[N],top;
	void solve(){
		res=0;
		for(int i=1;i<=n;++i)
			b[dep[i]]=a[i];
		for(int i=1;i<=n;++i){
			while(top&&b[s[top]]>b[i]){
				r[s[top]]=i;
				s[top--]=0;
			}
			s[++top]=i;
		}
		while(top){
			r[s[top]]=n+1;
			s[top--]=0;
		}
		for(int i=n;i;--i){
			while(top&&b[s[top]]>=b[i]){
				l[s[top]]=i;
				s[top--]=0;
			}
			s[++top]=i;
		}
		while(top){
			l[s[top]]=0;
			s[top--]=0;
		}
		for(int i=1;i<=n;++i)
			res=(res+1ll*b[i]*(r[i]-i)%P*(i-l[i]))%P;
		printf("%d\n",res);
	}
}
void dfsa(int x,int f){
	mn[x]=a[x];
	for(auto y:vec[x]){
		if(y==f)
			continue;
		++deg[x];
		son[x].push_back(y);
		dep[y]=dep[x]+1;
		dfsa(y,x);
		mn[x]=min(mn[x],mn[y]);
	}
}
void dfsb(int x,int mul,int m){
	res=(res+1ll*m*mul)%P;
	sort(son[x].begin(),son[x].end());
	do{
		int cur=m;
		for(auto y:son[x]){
			dfsb(y,1ll*mul*inv[deg[x]]%P,min(cur,a[y]));
			cur=min(cur,mn[y]);
		}
	}while(next_permutation(son[x].begin(),son[x].end()));
}
void solve(){
	res=0;
	scanf("%d%d",&n,&root);
	for(int i=1;i<=n;++i)
		scanf("%d",a+i);
	for(int i=1;i<=n;++i){
		vec[i].clear();
		son[i].clear();
		deg[i]=0;
	}
	for(int i=1;i<n;++i){
		scanf("%d%d",&u,&v);
		vec[u].push_back(v);
		vec[v].push_back(u);
	}
	dep[root]=1;
	dfsa(root,0);
	int mx=0;
	for(int i=1;i<=n;++i)
		mx=max(mx,dep[i]);
	if(mx==n){
		line::solve();
		return;
	}
	fac[0]=1;
	for(int i=1;i<=n;++i)
		fac[i]=1ll*fac[i-1]*i%P;
	inv[n]=qpow(fac[n],P-2);
	for(int i=n-1;~i;--i)
		inv[i]=1ll*inv[i+1]*(i+1)%P;
	for(int i=1;i<=n;++i)
		dfsb(i,1,a[i]);
	printf("%d\n",res);
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	scanf("%d",&T);
	while(T--)
		solve();
	return 0;
}