#include<bits/stdc++.h>
using namespace std;
constexpr int N=400000,mod=998244353;
inline void reduce(int &x){if(x>=mod) x-=mod;}
long long qpow(long long a,long long b){
	long long m=1;
	while(b){
		if(b&1) m=m*a%mod;
		b>>=1;
		a=a*a%mod;
	}
	return m;
}
int a[N+10];
struct EDGE{
	int to,next;
}e[(N<<1)+10];
int head[N+10],etop;
int sum,dp[N+10],deg[N+10],degc[N+10];
void adde(int u,int v){
	e[etop].to=v;
	e[etop].next=head[u];
	head[u]=etop++;
}
int fa[N+10];
struct _{
	int x,val;
	bool operator<(const _ &ob)const{
		return val<ob.val;
	}
}b[N+10];
int btop;
void dfs(int x){
	int i;
	for(i=head[x];~i;i=e[i].next){
		if(e[i].to==fa[x]) continue;
		fa[e[i].to]=x;
		++deg[x];
		dfs(e[i].to);
	}
}
int vis[N+10],sons[N+10],sons_[N+10];
void work(int x){
	vis[x]=1;
	dp[x]=(sons[x]*qpow(deg[x]-degc[x]+1,mod-2)+sons_[x]*qpow(deg[x]-degc[x],mod-2)+1)%mod;
	reduce(sum+=dp[x]);
	if(deg[x]==degc[x]) vis[x]=2;
	if(vis[x]==2) ++degc[fa[x]];
	if(vis[x]==1) reduce(sons_[fa[x]]+=dp[x]);
	else reduce(sons[fa[x]]+=dp[x]);
	while(fa[x]){
		x=fa[x];
		if(!vis[x]) return;
		if(vis[x]==1) reduce(sons_[fa[x]]+=mod-dp[x]);
		else reduce(sons[fa[x]]+=mod-dp[x]);
		reduce(sum+=mod-dp[x]);
		if(degc[x]==deg[x]) vis[x]=2,++degc[fa[x]];
		dp[x]=(sons[x]*qpow(deg[x]-degc[x]+1,mod-2)+sons_[x]*qpow(deg[x]-degc[x],mod-2)+1)%mod;
		reduce(sum+=dp[x]);
		if(vis[x]==1) reduce(sons_[fa[x]]+=dp[x]);
		else reduce(sons[fa[x]]+=dp[x]);
	}
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	int T;
	cin>>T;
	while(T--){
		int n,rt;
		cin>>n>>rt;
		memset(head,255,n+1<<2);
		memset(dp,0,n+1<<2);
		memset(sons,0,n+1<<2);
		memset(sons_,0,n+1<<2);
		memset(deg,0,n+1<<2);
		memset(degc,0,n+1<<2);
		memset(vis,0,n+1<<2);
		sum=0;
		etop=0;
		int i;
		for(i=1;i<=n;++i) cin>>a[i];
		for(i=1;i<n;++i){
			int u,v;
			cin>>u>>v;
			adde(u,v);
			adde(v,u);
		}
		fa[rt]=0;
		dfs(rt);
		btop=n;
		for(i=1;i<=n;++i) b[i]={i,a[i]};
		sort(b+1,b+1+n);
		int ans=0;
		for(i=n;i>=1;--i){
			work(b[i].x);
			ans=(ans+(long long)sum*(b[i].val-b[i-1].val+mod))%mod;
		}
		cout<<ans<<'\n';
	}
	return 0;
}
