#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Mod=998244353;
const int inv2=(Mod+1)/2;
int T,n,rt,a[810000],pw[810000],iv[810000],deg[810000];
int num[810000],mn[810000],dp[810000],qq,pp[810000];
ll sum,lst; vector<int> vec[810000];
void dfs(int u,int f){
	mn[u]=a[u];
	for (int v:vec[u]){
		if (v==f) continue;
		dfs(v,u); mn[u]=min(mn[u],mn[v]);
	}
}
void dfs2(int u,int f,int c){
	if (a[u]<c) dp[u]=0;
	else dp[u]=(dp[u]+1)%Mod;
	sum=(dp[u]+sum)%Mod;
	int tot=0,all=0;
	for (int v:vec[u]){
		if (v==f) continue;
		tot+=(mn[v]>=c); all++;
	}
	for (int v:vec[u]){
		if (v==f) continue;
//		(tot-(mn[v]>=c)) (all-1)
		if (all==1) dp[v]=dp[u];
		else dp[v]=1ll*dp[u]*pw[tot-(mn[v]>=c)]%Mod*iv[all-1]%Mod;
		dfs2(v,u,c);
	}
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	pw[0]=1; for (int i=1;i<=800000;i++) pw[i]=1ll*pw[i-1]*2%Mod;
	iv[0]=1; for (int i=1;i<=800000;i++) iv[i]=1ll*iv[i-1]*inv2%Mod;
	cin>>T;
	while (T--){
		cin>>n>>rt; int cnt=0;
		for (int i=1;i<=n;i++) deg[i]=0;
		for (int i=1;i<=n;i++){
			cin>>a[i];
			num[++cnt]=a[i];
		}
		sort(num+1,num+cnt+1); cnt=unique(num+1,num+cnt+1)-num-1;
		int u,v;
		for (int i=1;i<n;i++){
			cin>>u>>v;
			deg[u]++; deg[v]++;
			vec[u].push_back(v);
			vec[v].push_back(u);
		}
		if (deg[rt]==n-1){
			ll ans=0,sum=0;
			for (int i=1;i<=n;i++) ans=(ans+a[i])%Mod;
			qq=0;
			for (int i=1;i<=n;i++)
				if (i!=rt) pp[++qq]=min(a[i],a[rt]);
			sort(pp+1,pp+qq+1);
			for (int i=1;i<=qq;i++){
				ans=(ans+sum+1ll*iv[i-1]*pp[i])%Mod;
				sum=(sum+1ll*iv[i]*pp[i])%Mod;
			}
			cout<<ans<<'\n';
			for (int i=1;i<=n;i++) vec[i].clear();
			continue;
		}
		dfs(rt,0); ll ans=0;
		lst=0;
		for (int i=cnt;i>=1;i--){
			for (int j=1;j<=n;j++) dp[j]=0;
			sum=0; dfs2(rt,0,num[i]);
			ans=(ans+1ll*(sum-lst+Mod)*num[i])%Mod;
			lst=sum;
		}
		cout<<ans<<'\n';
		for (int i=1;i<=n;i++) vec[i].clear();
	}
	return 0;
}

