#include<bits/stdc++.h>
#define pb push_back
using namespace std;
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(!isdigit(c)){
		if(c=='-') f=~f+1;c=getchar();
	}
	while(isdigit(c)){
		x=(x<<3)+(x<<1)+(c^48);
		c=getchar();
	}
	return f==-1?~x+1:x;
}
int T,n,rt;
int a[400010];
vector<int>vec[400010];
int ans;
const int mod=998244353;

inline void addx(int &x,int y){
	(x+=y)>=mod?x-=mod:x;
}
int qpow(int x,int k=mod-2){
	int res=1;
	while(k){
		if(k&1) res=1ll*res*x%mod;x=1ll*x*x%mod;k>>=1;
	}
	return res;
}
int inv[400010];
int mn[400010],Fa[400010];
void dfs1(int x,int fa){
	Fa[x]=fa;
	mn[x]=a[x];
//	printf("x:%d,mn:%d\n",x,mn[x]);
	for(int v:vec[x]) if(v!=fa){
		dfs1(v,x);mn[x]=min(mn[x],mn[v]);
	}
}
int ps[400010];
void dfs(int x,int val,int fa,int P){
	val=min(val,a[x]);addx(ans,1ll*val*P%mod);
	addx(ps[x],P);
//	printf("x:%d,val:%d,fa:%d,P:%d\n",x,val,fa,P);
	vector<int>son;
	for(int v:vec[x]) if(v!=fa){
		son.pb(v);
	}
	int n=son.size();
	int p=P;
//	printf("x:%d,n:%d\n",x,n);
	for(int i=1;i<=n;++i) p=1ll*inv[i]*p%mod;
	int idx[n+1];
	for(int i=0;i<n;++i) idx[i]=i;
	do{
//		for(int i=0;i<n;++i) printf("%d ",idx[i]);
//		printf("\n");
		int Va=val;
		for(int i=0;i<n;++i){
			dfs(son[idx[i]],Va,x,p);
			Va=min(Va,mn[son[idx[i]]]);
		}
	}while(next_permutation(idx,idx+n));
}
void solve(int x){
	dfs1(rt,0);
	dfs(x,a[x],Fa[x],1);
//	for(int i=1;i<=n;++i) printf("%d ",ps[i]);printf("\n");
}
int b[400010],c[400010];
int stk[400010],top;
void line(){
	vector<int>ln;
	int pr=0,nwp=0;
	for(int i=1;i<=n;++i) if(vec[i].size()==1){
		nwp=i;
	}
	while(ln.size()!=n){
		ln.pb(nwp);
		for(int v:vec[nwp]) if(v!=pr){
			pr=nwp,nwp=v;break ;
		}
	}
	vector<int>L1,L2;
	bool f=0;
	for(int i=0;i<n;++i){
		if(ln[i]==rt){
			f=1;continue ;
		}
		if(!f) L1.pb(ln[i]);
		else L2.pb(ln[i]);
	}
	for(int i=1;i<=n;++i) b[i]=a[i];
	sort(b+1,b+n+1);
	int m=unique(b+1,b+n+1)-b-1;
	for(int i=1;i<=n;++i) c[i]=lower_bound(b+1,b+m+1,a[i])-b;
	int n1=L1.size(),n2=L2.size();
	int nws=0;
	top=0;stk[++top]=n1;c[n+1]=0;
	L1.pb(n+1);L2.pb(n+1);
	for(int i=n1-1;i>=0;--i){
		while(top>1&&c[L1[i]]<c[L1[stk[top]]]){
			addx(nws,mod-1ll*(stk[top-1]-stk[top])*b[c[L1[stk[top]]]]%mod);
			--top;
		}
		stk[++top]=i;
		addx(nws,1ll*(stk[top-1]-stk[top])*b[c[L1[i]]]%mod);
		addx(ans,nws);
	}
	nws=0;
	top=0;stk[++top]=n2;
	for(int i=n2-1;i>=0;--i){
		while(top>1&&c[L2[i]]<c[L2[stk[top]]]){
			addx(nws,mod-1ll*(stk[top-1]-stk[top])*b[c[L2[stk[top]]]]%mod);
			--top;
		}
		stk[++top]=i;
		addx(nws,1ll*(stk[top-1]-stk[top])*b[c[L2[i]]]%mod);
		addx(ans,nws);
	}
	if(n1==0){
		int mn=a[rt];addx(ans,mn);
		for(int i=0;i<n2;++i){
			mn=min(mn,a[L2[i]]);addx(ans,mn);
		}
	}
	else if(n2==0){
		int mn=a[rt];addx(ans,mn);
		for(int i=0;i<n1;++i){
			mn=min(mn,a[L1[i]]);addx(ans,mn);
		}
	}
	else{
		int mn=a[rt];addx(ans,mn);
		for(int i=0;i<n1;++i){
			mn=min(mn,a[L1[i]]);addx(ans,1ll*mn*(mod+1>>1)%mod);
		}
		for(int i=0;i<n2;++i){
			mn=min(mn,a[L2[i]]);addx(ans,1ll*mn*(mod+1>>1)%mod);
		}
		mn=a[rt];
		for(int i=0;i<n2;++i){
			mn=min(mn,a[L2[i]]);addx(ans,1ll*mn*(mod+1>>1)%mod);
		}
		for(int i=0;i<n1;++i){
			mn=min(mn,a[L1[i]]);addx(ans,1ll*mn*(mod+1>>1)%mod);
		}
	}
	printf("%d\n",ans);
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	inv[1]=1;
	for(int i=2;i<400000;++i) inv[i]=qpow(i);
	T=read();
	while(T--){
		ans=0;
		n=read();rt=read();
		for(int i=1;i<=n;++i) a[i]=read(),vector<int>().swap(vec[i]);
		for(int i=1;i<n;++i){
			int u=read(),v=read();
			vec[u].pb(v),vec[v].pb(u);
		}
		int mxd=0;
		for(int i=1;i<=n;++i) mxd=max(mxd,(int)vec[i].size());
		if(mxd==2&&n>10){
			line();continue ;
		}
//		else continue ;
		for(int i=1;i<=n;++i){
			solve(i);
		}
		printf("%d\n",ans);
	}
}