#include<bits/stdc++.h>
#define I inline
#define RI register int
#define rep(i,a,b) for(RI i=a;i<=b;++i)
#define dow(i,a,b) for(RI i=a;i>=b;--i)
#define edg(i,u,v) for(RI v,i=head[u];v=e[i].to,i;i=e[i].next)
using namespace std;
const int N=400005,mo=998244353;
I int add(int a,int b){ return a+b>=mo?a+b-mo:a+b; }
I int sub(int a,int b){ return a-b<0?a-b+mo:a-b; }
I int mul(int a,int b){ return 1ll*a*b%mo; }
I int fast(int a,int b=mo-2){ RI sum=1;while(b) b&1&&(sum=mul(sum,a)),a=mul(a,a),b>>=1;return sum; }
struct edge{ int to,next; } e[N<<1];
int T,n,rt,sum[N],a[N],x,st[N],top,y,sz[N],vis[N],q[N],Fa[N],cnt,P,Q,siz[N],fa[N],son[N],head[N],inv[N],frac[N],ifac[N],tag[N];
pair<int,int> PI[N];
I int find(int u){ return u==Fa[u]?u:Fa[u]=find(Fa[u]); }
I void dfs(int u,int f){ st[++top]=u,sz[u]=1,fa[u]=f;edg(i,u,v) if(v^f) dfs(v,u),++son[u],sz[u]+=sz[v]; }
I void ins(int x){ tag[x]=1;while(x) sum[x]=(siz[x]+=1)==sz[x],x=fa[x]; }
I void add_e(int from,int to){ e[++cnt]=(edge){ to,head[from]},head[from]=cnt; }
I void dfs2(int u,int f,int now){
	if(!tag[u]) return;
	RI b=-1;vis[u]=1,q[++q[0]]=u,now=add(now,1),P+=now;
	edg(i,u,v) if(sum[v]) ++b;RI res=0,n=son[u],pre=now,C=mul(frac[b],ifac[n]);
	rep(j,0,b) res=add(res,mul(C,mul(ifac[b-j],mul(frac[n-j],inv[n-j]))));now=mul(now,res);
	edg(i,u,v) if(sum[v]&&v^f) dfs2(v,u,now); 
	now=pre,res=0,++b;rep(j,0,b) res=add(res,mul(C,mul(ifac[b-j],mul(frac[n-j],inv[n-j]))));now=mul(now,res);
	edg(i,u,v) if(!sum[v]&&v^f) dfs2(v,u,now);
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	frac[0]=1;rep(i,1,N-1) frac[i]=mul(frac[i-1],i);ifac[N-1]=fast(frac[N-1]);
	dow(i,N-2,0) ifac[i]=mul(ifac[i+1],i+1);rep(i,0,N-1) inv[i+1]=mul(ifac[i+1],frac[i]);
	scanf("%d",&T);
	while(T--){
		memset(head,0,sizeof(head)),cnt=0;RI ans=0;
		scanf("%d%d",&n,&rt);rep(i,1,n) scanf("%d",&a[i]),PI[i]=make_pair(a[i],i),Fa[i]=i;
		rep(i,1,n-1) scanf("%d%d",&x,&y),add_e(x,y),add_e(y,x);top=0;dfs(rt,0);
		sort(PI+1,PI+n+1,greater<pair<int,int> > ());RI mj=1;
		while(mj<=n){
			ins(PI[mj].second),++mj;
			while(PI[mj].first==PI[mj-1].first) ins(PI[mj].second),++mj;
			Q=P,P=0;rep(i,1,top) if(!vis[st[i]]) dfs2(st[i],0,0);
			ans=add(ans,mul(sub(P,Q),PI[mj-1].first));
			while(q[0]) vis[q[0]]=0,q[0]--;
		}
		printf("%d\n",ans);
	}
	return 0;
}