#include<bits/stdc++.h>
#define pb push_back
using namespace std;
char gc(){
	static const int S=1<<21;static char *L,*R,buf[S];
	return L==R&&(R=(L=buf)+fread(buf,1,S,stdin),L==R)?EOF:*L++;
}
int read(){
	int red=0,f=1;char ch=gc();
	while(ch<'0'||ch>'9'){ if(ch=='-') f=-f;ch=gc();}
	while(ch>='0'&&ch<='9') red=red*10+ch-48,ch=gc();
	return red*f;
}
const int N=4e5+5,TT=998244353;
typedef long long LL;
bool flg_lian=1;
int T,n,root,a[N],b[N],du[N];LL fac[N],ifac[N],ans;
vector<int>V[N];
void clear(){
	memset(du,0,sizeof du);ans=0;flg_lian=1;
	for(int i=1;i<=n;i++) V[i].clear();
}
LL QSM(LL x,LL y){
	LL s=1;
	while(y){
		if(y&1) s=s*x%TT;
		x=x*x%TT,y>>=1;
	}
	return s;
}
int sam[N],t[N],Fa[N];
LL C(LL x,LL y){return fac[x]*ifac[y]%TT*ifac[x-y]%TT;}
bool cmp(int x,int y){return a[x]>a[y];}
void dfs(int x,int fa=0,LL faW=1ll){
	sort(V[x].begin(),V[x].end());
	int k=0;t[0]=0;
	for(auto v:V[x]) if(v^fa) t[++t[0]]=v;
	t[t[0]+1]=-233;
	for(int i=t[0],S=0;i;i--){
		if(a[t[i+1]]!=a[t[i]]) S=0;else S++;
		sam[t[i]]=S;
	}
	for(auto v:V[x]) if(v^fa){
		k+=sam[v];int len=du[x]-(x!=root);
		LL now=C(len,k)*fac[len-k-1]%TT*fac[k]%TT*faW%TT*ifac[len]%TT;
		ans=(ans+now*a[v]%TT)%TT;dfs(v,x,now);
		k-=sam[v];
		k++;
	}
}
void Getfa(int x,int fa=0){Fa[x]=fa;for(int v:V[x]) if(v^fa) Getfa(v,x);}
void Dfs(int x,int fa,int type){
	if(type==-1) a[x]=b[x];
	for(int v:V[x]) if(v^fa){
		if(type==1) a[v]=min(a[v],a[x]);
		Dfs(v,x,type);
	}
}
int id[N],st[N],top,L[N],R[N];
void BL(){
	Getfa(root);
	int x=root;
	for(int i=1;i<=n;i++){
		id[i]=x;
		for(int v:V[x]) if(v^Fa[x]){x=v;break;}
	}
	st[top=0]=0;
	for(int i=1;i<=n;i++){
		while(top&&a[id[st[top]]]>=a[id[i]]) top--;
		L[i]=st[top]+1;st[++top]=i;
	}
	st[top=0]=n+1;
	for(int i=n;i;i--){
		while(top&&a[id[st[top]]]>=a[id[i]]) top--;
		R[i]=st[top]-1;st[++top]=i;
		ans=(ans+(LL)(i-L[i]+1)*(R[i]-i)%TT*a[id[i]]%TT)%TT;
	}
	printf("%lld\n",ans);
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	T=read();
	for(int i=fac[0]=1;i<N;i++) fac[i]=fac[i-1]*i%TT;
	ifac[N-1]=QSM(fac[N-1],TT-2);
	for(int i=N-2;~i;i--) ifac[i]=ifac[i+1]*(i+1)%TT;
	while(T--){
		clear();n=read(),root=read();a[0]=1e9;
		for(int i=1;i<=n;i++) a[i]=b[i]=read(),ans=(ans+a[i])%TT;
		for(int i=1;i<n;i++){
			int x=read(),y=read();
			V[x].pb(y),V[y].pb(x);du[x]++,du[y]++;
			if(du[x]>2||du[y]>2) flg_lian=0;
		}
		if(flg_lian==1){BL();continue;}
		Getfa(root);
		for(int i=1;i<=n;i++){
			Dfs(i,Fa[i],1);
			dfs(i,Fa[i]);
			Dfs(i,Fa[i],-1);
		}
		printf("%lld\n",ans);
	}
	return 0;
}
