#include<bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=4e5+5,mod=998244353;
vector<int> e[N],vec[N];
int inv[N],a[N],b[N],ans[N],fa[N],vis[N],fl[N],f[N],X[N],sum[N][2];
void add(int &x,int y){ x+=y; if(x>=mod) x-=mod;}
void rdfs(int u,int fath){
	if(!vis[u]) f[u]=1;
	for(int v:e[u])
		if(v!=fath){
			rdfs(v,u);
			if(!vis[u]) f[u]=(f[u]+1ll*inv[X[u]-fl[v]]*f[v])%mod;
		}
}
void dfs(int u,int fath){
	f[u]=1,fa[u]=fath,sum[u][0]=sum[u][1]=0;
	for(int v:e[u])
		if(v!=fath) dfs(v,u),add(f[u],f[v]);
	sum[u][0]=(f[u]+mod-1)%mod,X[u]=1;
}
void init(int n){
	inv[1]=1;
	for(int i=2;i<=n;++i)
		inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
}
void solve(){
	int n,rt;
	cin>>n>>rt;
	for(int i=1;i<=n;++i) fl[i]=vis[i]=0,vec[i].clear(),e[i].clear();
	for(int i=1;i<=n;++i)
		cin>>a[i],b[i]=a[i];
	sort(b+1,b+n+1);
	int tot=unique(b+1,b+n+1)-b-1;
	for(int i=1;i<=n;++i)
		a[i]=lower_bound(b+1,b+n+1,a[i])-b,vec[a[i]].pb(i);
	for(int i=1;i<n;++i){
		int u,v;
		cin>>u>>v;
		e[u].pb(v),e[v].pb(u);
	}
	dfs(rt,0),ans[1]=0;
	for(int i=1;i<=n;++i) add(ans[1],f[i]);
	int now=ans[1];
	for(int i=1;i<=tot;++i){
		for(int u:vec[i]){
			vis[u]=1;
			for(int x=u;x;x=fa[x]){
				add(sum[fa[x]][fl[x]],mod-f[x]),add(now,mod-f[x]);
				if(!fl[x]) ++X[fa[x]],fl[x]=1;
				if(vis[x]) f[x]=0;
				else f[x]=(1ll*sum[x][0]*inv[X[x]]+1ll*sum[x][1]*inv[X[x]-1]+1)%mod;
				add(sum[fa[x]][fl[x]],f[x]),add(now,f[x]);
			}
		}
		ans[i+1]=now;
	}
	for(int i=1;i<=tot;++i) add(ans[i],mod-ans[i+1]);
	int sum=0;
	for(int i=1;i<=tot;++i)
		sum=(sum+1ll*ans[i]*b[i])%mod;
	cout<<sum<<"\n";
}
signed main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	init(4e5);
	int T=1;
	cin>>T;
	while(T--) solve();
}
