#include<bits/stdc++.h>
#define For(i,x,y) for (int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (int i=(x);i<(y);i++)
#define Dow(i,x,y) for (int i=(x);i>=(y);i--)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define siz(x) ((int)x.size())
#define fil(a,b) memset(a,b,sizeof a)
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
typedef vector<int> poly;
inline ll read(){
	ll x=0,f=1;char c=getchar();
	while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
	if (c=='-') f=-1,c=getchar();
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}

const int N = 4e5+10, mod = 998244353;
int n,rt,a[N],id[N],inv[N],top,stk[N],l[N],r[N];
poly e[N];

int ans,s,f[N];
bool flag[N],fl[N],fl2[N];
inline void dfs(int u,int fa,int now){
	fl[u]=flag[u],fl2[u]=(u==now);
	for (auto v:e[u]) if (v!=fa){
		dfs(v,u,now),fl[u]|=fl[v],fl2[u]|=fl2[v];	
	}
	if (flag[u]){
		f[u]=0;
		return;
	}
	int cnt1=0,cnt0=0;
	for (auto v:e[u]) if (v!=fa){
		if (fl[v]) ++cnt1;
			else ++cnt0;
	}
	if (!fl2[u]){
		f[u]=1;
		for (auto v:e[u]) if (v!=fa){
			if (fl[v]) --cnt1;
			f[u]=(f[u]+1ll*f[v]*inv[cnt1+1])%mod;
			if (fl[v]) ++cnt1;
		}
	} else if (now==u){
		f[u]=1,s=(s+1)%mod;
		for (auto v:e[u]) if (v!=fa){
			if (fl[v]) --cnt1;
			int t=1ll*f[v]*inv[cnt1+1]%mod;
			s=(s+t)%mod;
			f[u]=(f[u]+t)%mod;
			if (fl[v]) ++cnt1;
		}
	} else {
		f[u]=0;
		bool ff=0;
		for (auto v:e[u]) if (v!=fa&&fl2[v]){
			if (fl[v]) ff=1;
			if (fl[v]) --cnt1;
				else --cnt0;
			int t=1ll*f[v]*inv[cnt1+1]%mod;
			s=(s+t)%mod;
			f[u]=(f[u]+t)%mod;
		}
		for (auto v:e[u]) if(v!=fa&&!fl2[v]){
			if (ff) break;
			if (fl[v]) --cnt1;
			int t=1ll*f[v]*inv[cnt1+2]%mod*inv[cnt1+1]%mod;
			s=(s+t)%mod;
			f[u]=(f[u]+t)%mod;
			if (fl[v]) ++cnt1;
		}
	}
}

poly vec;
inline void Dfs(int u,int fa){
	vec.pb(a[u]);
	for (auto v:e[u]) if (v!=fa) Dfs(v,u);
}

inline void solve(){
	n=read(),rt=read();
	For(i,1,n) a[i]=read(),id[i]=i;
	inv[0]=inv[1]=1;
	For(i,2,n) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
	sort(id+1,id+1+n,[=](int x,int y){
		return a[x]<a[y];
	});
	For(i,1,n) e[i].clear();
	For(i,1,n-1){
		int x=read(),y=read();
		e[x].pb(y),e[y].pb(x);
	}
	if (n>5000&&siz(e[rt])==n-1){
		int ans=0,p=0;
		For(i,1,n){
			if (id[i]==rt){p=i;break;}
			int s=1;
			s=(s+inv[i])%mod;
			s=(s+1ll*(n-i-1)*inv[i]%mod*inv[i+1])%mod;
			ans=(ans+1ll*a[id[i]]*s)%mod;
		}
		ans=(ans+1ll*a[rt]*(1+1ll*(n-p)*inv[p]%mod))%mod;
		For(i,p+1,n) ans=(ans+a[id[i]])%mod;
		printf("%d\n",ans);	
		return;
	}
	int cnt2=0,cnt1=0;
	For(i,1,n) cnt2+=(siz(e[i])==2),cnt1+=(siz(e[i])==1);
	if (n>5000&&cnt2==n-2&&cnt1==2){
		for (auto v:e[rt]){
			vec.clear(),vec.pb(a[rt]),Dfs(v,rt);
			top=0,stk[0]=-1;
			FOR(i,0,siz(vec)){
				while (top&&vec[i]<vec[stk[top]]) --top;
				l[i]=stk[top]+1,stk[++top]=i;
			}
			top=0,stk[0]=siz(vec);
			Dow(i,siz(vec)-1,0){
				while (top&&vec[i]<=vec[stk[top]]) --top;
				r[i]=stk[top]-1,stk[++top]=i;
			}
			FOR(i,0,siz(vec)) ans=(ans+1ll*(i-l[i]+1)*(r[i]-i+1)%mod*vec[i])%mod;
		}
		if (siz(e[rt])==2) ans=(ans+mod-a[rt])%mod;
		printf("%d\n",ans);
		return;
	}
	ans=0;
	For(i,1,n) flag[i]=0;
	For(i,1,n){
		s=0,dfs(rt,0,id[i]),flag[id[i]]=1;
		ans=(ans+1ll*s*a[id[i]])%mod;
	}
	printf("%d\n",ans);
}

int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	int T=read();
	while (T--) solve();
}
/*
1
6 1
1 5 2 3 6 4
1 2
2 3
3 6
1 4
4 5
*/
