#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=400005,M=998244353;
int ne[N<<1],fi[N],zz[N<<1],tot,a[N],sz[N],fa[N],G[N],TTT,Inv[N],n,x,y,root,flag[N],Flag[N],cnt[N],f[N],S1[N],S2[N],S3[N],S,son[N],top[N],rk[N];
set<int> SS;
struct Tree{
	int l,r,num1,num2,flag0,flag1,flag2;
}T[N*4];
void jb(int x,int y){
	ne[++tot]=fi[x];
	fi[x]=tot;
	zz[tot]=y;
}
inline int ksm(int x,int y){
	int ans=1;
	for (;y;y>>=1,x=(ull)x*x%M)
		if (y&1)ans=(ull)ans*x%M;
	return ans;
}
inline int cmp(int x,int y){
	return a[x]<a[y];
}
void dfs(int x,int y){
	sz[x]=1;fa[x]=y;
	for (int i=fi[x];i;i=ne[i])
		if (zz[i]!=y){
			dfs(zz[i],x);
			sz[x]+=sz[zz[i]];
		}
	S1[x]=sz[x]-1;
}
void dfs2(int x,int y){
	rk[x]=++tot;top[x]=y;
	if (son[x])dfs2(son[x],y);
	for (int i=fi[x];i;i=ne[i])
		if (zz[i]!=fa[x]&&zz[i]!=son[x])dfs2(zz[i],zz[i]);
}
void build(int x,int l,int r){
	T[x].l=l;T[x].r=r;T[x].num1=r-l+1;
	T[x].num2=0;T[x].flag1=T[x].flag0=1;T[x].flag2=0;
	if (l==r)return;
	int mid=(l+r)/2;
	build(x*2,l,mid);
	build(x*2+1,mid+1,r);
}
inline void reduce(int &x){
	if (x>=M)x-=M;
}
inline void update(int x){
	reduce(T[x].num1=T[x*2].num1+T[x*2+1].num1);
	reduce(T[x].num2=T[x*2].num2+T[x*2+1].num2);
}
inline void add1(int x,int z1,int z2){
	T[x].flag2=(ull)T[x].flag2*z2%M;
	T[x].flag0=(ull)T[x].flag0*z2%M;
	T[x].flag1=(ull)T[x].flag1*z1%M;
	T[x].num1=(ull)T[x].num1*z1%M;
}
inline void add2(int x,int y){
	T[x].num2=(T[x].num2+(ull)y*T[x].num1)%M;
	reduce(T[x].flag2+=T[x].flag2+y);
}
inline void down(int x){
	if (T[x].flag1)add1(x*2,T[x].flag1,T[x].flag0),add1(x*2+1,T[x].flag1,T[x].flag0);
	if (T[x].flag2)add2(x*2,T[x].flag2),add2(x*2+1,T[x].flag2);
	T[x].flag1=T[x].flag0=1;T[x].flag2=0;
}
inline int find1(int x,int y){
	if (T[x].l==T[x].r)return T[x].num1;
	int mid=(T[x].l+T[x].r)/2;
	down(x);
	if (y<=mid)return find1(x*2,y);
	return find1(x*2+1,y);
}
inline int find2(int x,int y){
	if (T[x].l==T[x].r)return T[x].num2;
	int mid=(T[x].l+T[x].r)/2;
	down(x);
	if (y<=mid)return find2(x*2,y);
	return find2(x*2+1,y);
}
inline void insert1(int x,int l,int r,int z1,int z2){
	if (T[x].l>r||l>T[x].r)return;
	if (T[x].l>=l&&T[x].r<=r){
		add1(x,z1,z2);
		return;
	}
	down(x);
	insert1(x*2,l,r,z1,z2);
	insert1(x*2+1,l,r,z1,z2);
	update(x);
}
inline void insert2(int x,int l,int r,int z){
	if (T[x].l>r||l>T[x].r)return;
	if (T[x].l>=l&&T[x].r<=r){
		add2(x,z);
		return;
	}
	down(x);
	insert2(x*2,l,r,z);
	insert2(x*2+1,l,r,z);
	update(x);
}
inline void insert3(int x,int y){
	if (T[x].l==T[x].r){
		T[x].num2=0;
		return;
	}
	down(x);
	int mid=(T[x].l+T[x].r)/2;
	if (y<=mid)insert3(x*2,y);
	else insert3(x*2+1,y);
	update(x);
}
void Update(int x){
	int D=(sz[x]-G[x]+M)%M;
	G[x]=sz[x];Flag[x]=1;
	(S+=D)%=M;
	SS.insert(-rk[x]);
	while (x!=root&&!flag[x]){
		flag[x]=1;
		int y=fa[x];
		if (Flag[y])return;
		int t=find2(1,rk[y]);
		S3[y]=(S3[y]+(ll)t*cnt[y])%M;
		(D+=t)%=M;
		cnt[y]++;
		if (cnt[y]>1)insert1(1,1,rk[y],(ll)(cnt[y]-1)*Inv[cnt[y]]%M,(ll)cnt[y]*Inv[cnt[y]-1]%M);
		insert3(1,rk[y]);
		S1[y]-=sz[x];
		D=S1[y]*(((ll)cnt[y]*Inv[cnt[y]+1]-(ll)(cnt[y]-1)*Inv[cnt[y]])%M+M)%M;
		if (cnt[y]>1)D=(D+(ll)S2[y]*(((ll)(cnt[y]-1)*Inv[cnt[y]]-(ll)(cnt[y]-2)*Inv[cnt[y]-1])%M+M))%M,D=(D+(ll)S3[y]*(Inv[cnt[y]]-Inv[cnt[y]-1]+M))%M;
		D=(D+(ll)G[x]*Inv[cnt[y]])%M;
		(S3[y]+=G[x])%=M;
		S2[y]+=sz[x];
		x=y;
		(G[x]+=D)%=M;
		(S+=D)%=M;
	}
	while (x!=root){
		D=(ll)D*ksm(find1(1,rk[x]),M-2)%M;
		int fx=top[x],t=-*SS.lower_bound(-rk[x]+1);
		if (t>=rk[fx]){
			insert2(1,t+1,rk[x]-1,D);
			return;
		}
		insert2(1,rk[fx],rk[x]-1,D);
		D=(ll)D*find1(1,rk[fx])%M;
		x=fx;
		x=fa[x];
		if (Flag[x])break;
		(S3[x]+=D)%=M;
		G[x]=(G[x]+(ll)D*Inv[cnt[x]])%M;
		S=(S+(ll)D*Inv[cnt[x]])%M;
		D=(ll)D*Inv[cnt[x]]%M;
	}
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	scanf("%d",&TTT);
	Inv[0]=1;
	for (int i=1;i<N;i++)Inv[i]=ksm(i,M-2);
	while (TTT--){
		memset(fi,0,sizeof fi);
		memset(G,0,sizeof G);
		memset(cnt,0,sizeof cnt);
		memset(Flag,0,sizeof Flag);
		memset(flag,0,sizeof flag);
		memset(S1,0,sizeof S1);
		memset(S2,0,sizeof S2);
		memset(S3,0,sizeof S3);
		SS.clear();SS.insert(0);
		scanf("%d%d",&n,&root);
		for (int i=1;i<=n;i++)scanf("%d",&a[i]),f[i]=i;
		sort(f+1,f+n+1,cmp);
		for (int i=1;i<n;i++){
			scanf("%d%d",&x,&y);
			jb(x,y);jb(y,x);
		}
		tot=0;
		dfs(root,0);
		dfs2(root,root);
		build(1,1,n);
		S=0;
		int last=0,ans=0;
		for (int i=1;i<=n;i++){
			Update(f[i]);
			ans=(ans+(ll)((S+T[1].num2)%M-last+M)*a[f[i]])%M;
			last=(S+T[1].num2)%M;
			
		}
		printf("%d\n",(ans+M)%M);
	}
	return 0;
}
/*
   最后一场oi考试了呢
   去年noi结束后就精神退役了吧，现在事实也要退役了
   放飞自我了，只肝了个t3
   不知道什么时候能脱单呢
   不知道会不会有人来看呢
   GG
*/
