#include<bits/stdc++.h>
using namespace std;
#define O 998244353
#define N 800000
int qkpw(int ds,int ix){
	int re=1;while(ix){
		if(ix&1)re=1ll*ds*re%O;
		ix>>=1;ds=1ll*ds*ds%O;
	}return re;
}
int T,n,rt,A[N+5],fn[N+5],ifn[N+5];
inline int iv(int x){return 1ll*fn[x-1]*ifn[x]%O;}
int las[N+5],nxt[N*2+5],cd[N*2+5],ct;
inline void con(int x,int y){
	nxt[++ct]=las[x];las[x]=ct;cd[ct]=y;
}
vector<int>kid[N+5];
int dfn[N+5],dft;
int sz[N+5];
int Ma[N+5];
int Ga[N+5];
void dfs0(int u,int g){
	dfn[u]=++dft;sz[u]=1;Ma[u]=A[u];Ga[u]=g;
	for(int i=las[u],v;i;i=nxt[i])
	if(cd[i]!=g){
		v=cd[i];kid[u].push_back(v);
		dfs0(v,u);sz[u]+=sz[v];
		Ma[u]=min(Ma[u],Ma[v]);
	}
	
}
vector<int>Ms[N+5];
int pt[N+5];
int work(int u,int v){
	static int pa[N+5],ph;
	ph=0;int p=v,t=Ga[v],pws=1,sm=A[u];int ans=0;
	while(p!=u){
		++ph;pa[ph]=t;Ms[ph].clear();pt[ph]=0;sm=min(sm,A[p]);
		for(int i=0,ed=kid[t].size();i<ed;i++)
		if(kid[t][i]!=p)Ms[ph].push_back(Ma[kid[t][i]]);
		sort(Ms[ph].begin(),Ms[ph].end());p=t;t=Ga[t];
//		pws=1ll*pws*ifn[Ms[ph].size()]%O;
	}
	while(true){
		int tm=sm,ep;
		for(int i=1;i<=ph;i++)
		if(pt[i]<Ms[i].size()){
			if(Ms[i][pt[i]]<tm)
			tm=Ms[i][pt[i]],ep=i;
		}
		if(tm==sm){
			(ans+=1ll*pws*tm%O)%=O;break;
		}
		++pt[ep];
		pws=1ll*iv(pt[ep]+1)*pws%O;
		(ans+=1ll*pws*tm%O)%=O;
		pws=1ll*pt[ep]*pws%O;
	}
	return ans;
}
long long fc(int x,int y){
	return 1ll*x*qkpw(y,O-2)%O;
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	fn[0]=1;for(int i=1;i<=800000;i++)fn[i]=1ll*fn[i-1]*i%O;
	ifn[800000]=qkpw(fn[800000],O-2);for(int i=800000;i>=1;i--)ifn[i-1]=1ll*ifn[i]*i%O;
	ios::sync_with_stdio(false);
	cin>>T;while(T--){
		cin>>n>>rt;memset(las,0,(n+2)*4);ct=0;
		for(int i=1;i<=n;i++)
		cin>>A[i],kid[i].clear();
		for(int i=1,x,y;i<n;i++)
		cin>>x>>y,con(x,y),con(y,x);
		dfs0(rt,0);int Ans=0;
		for(int i=1;i<=n;i++)
		for(int j=1,tmp;j<=n;j++){
			if(i==j){
				(Ans+=A[i]%O)%=O;
				continue;
			}
			if(dfn[i]<=dfn[j]&&dfn[i]+sz[i]>dfn[j]){
				tmp=work(i,j);
				(Ans+=tmp)%=O;
			}
			
		}
		cout<<Ans<<'\n';
	}
	return 0;
} 
