#include <bits/stdc++.h>
using namespace std;
const int Mod=998244353,N=400000;
int t=0,n,T,root,a[400010],Link[400010],sum[400010],cnt[400010],fac[400010],inv[400010],ans;
long long f[400010];
struct dsa
{
	int v,nex;
}e[800010];
int read()
{
	char ch=getchar();
	int aqq=0,op=1;
	while (ch<'0'||ch>'9') {if (ch=='-') op=-1;ch=getchar();}
	while (ch>='0'&&ch<='9') aqq=aqq*10+int(ch-48),ch=getchar();
	return aqq*op;
}
void Insert(int x,int y) {e[++t].nex=Link[x];e[t].v=y;Link[x]=t;return ;}
void dfs(int now,int fa)
{
	sum[now]=a[now];cnt[now]=0;
	for (int i=Link[now];i;i=e[i].nex) {
		if (e[i].v==fa) continue;
		cnt[now]++;
		dfs(e[i].v,now);
		sum[now]+=sum[e[i].v];
	}
	return ;
}
void dfs1(int now,int fa)
{
	f[now]=a[now];
	for (int i=Link[now];i;i=e[i].nex) {
		if (e[i].v==fa) continue;
		dfs1(e[i].v,now);
		f[now]+=f[e[i].v]+1LL*a[now]*cnt[e[i].v]%Mod;
		f[now]%=Mod;
		f[now]+=1LL*sum[e[i].v]*(cnt[now]-1)%Mod*(1LL*cnt[now]*(cnt[now]-1)/2%Mod*inv[cnt[now]]%Mod)%Mod;
		f[now]%=Mod;
	}
	ans+=f[now];ans%=Mod;
	return ;
}
int ksm(int aa,int bb)
{
	int res=1;
	for (;bb;bb>>=1) {
		if (bb&1) res=1LL*res*aa%Mod;
		aa=1LL*aa*aa%Mod;
	}
	return res;
}
int main()
{
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	T=read();
	fac[0]=1;
	for (int i=1;i<=N;i++) fac[i]=1LL*fac[i-1]*i%Mod;
	inv[N]=ksm(fac[N],Mod-2);
	for (int i=N-1;i>=0;i--) inv[i]=1LL*inv[i+1]*(i+1)%Mod;
	for (int i1=1;i1<=T;i1++) {
		n=read();root=read();
		for (int i=1;i<=n;i++) a[i]=read();
		t=0;
		for (int i=1;i<=n;i++) Link[i]=0;
		for (int i=1;i<n;i++) {
			int x,y;
			x=read();y=read();
			Insert(x,y);Insert(y,x);
		}
		ans=0;
		dfs(root,0);
		dfs1(root,0);
		printf("%d\n",ans);
	}
	return 0;
}
