#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pc putchar
#define chkmx(a,b) (a)=max((a),(b))
#define chkmn(a,b) (a)=min((a),(b))
#define lowbit(x) ((x)&-(x))
using namespace std;
template<class T>void read(T&x){x=0;char c=getchar();bool f=false;for(;!isdigit(c);c=getchar())f|=(c=='-');for(;isdigit(c);c=getchar())x=x*10+(c-'0');if(f)x=-x;}
template<class T,class ...Arg>void read(T&x,Arg&...arg){read(x);read(arg...);}
template<class T>void write(T x){if(x<0)pc('-'),x=-x;if(x>=10)write(x/10);pc(x%10+'0');}
template<class T,class ...Arg>void write(T x,Arg ...arg){write(x);pc(' ');write(arg...);}
template<class ...Arg>void writeln(Arg ...arg){write(arg...);pc('\n');}
const int mod=998244353;
struct mint{
	int x;
	mint(int o=0){x=o%mod;}
	mint&operator+=(mint b){return x+=b.x,(x>=mod&&(x-=mod)),*this;}
	mint&operator-=(mint b){return x-=b.x,(x<0&&(x+=mod)),*this;}
	mint&operator*=(mint b){return(x=1ll*x*b.x%mod),*this;}
	mint&operator^=( int b){mint a=*this;x=1;while(b)(b&1)&&(*this*=a,1),a*=a,b>>=1;return*this;}
	mint&operator/=(mint b){return*this*=(b^=mod-2);}
	friend mint operator+(mint a,mint b){return a+=b;}
	friend mint operator-(mint a,mint b){return a-=b;}
	friend mint operator*(mint a,mint b){return a*=b;}
	friend mint operator^(mint a, int b){return a^=b;}
	friend mint operator/(mint a,mint b){return a/=b;}
	mint operator-(){return 0-*this;}
};
const int N=4e5+10;
int T,n,root,a[N],fa[N],deg[N];vector<int>e[N];
mint inv[N];
void dfs(int u){
	for(auto v:e[u])if(v!=fa[u])
		fa[v]=u,dfs(v);
}
namespace solve0{
	//暴力计算
	int t;
	mint ans=0;
	void dfs(int u,int st,int mn,mint p){
		if(u==t)return ans+=p*mn,void();
		int sons=0;
		for(auto v:e[u])if(v!=fa[u]&&!(st>>v&1))sons++;
		for(auto v:e[u])if(v!=fa[u]&&!(st>>v&1))dfs(v,st|(1<<v),min(mn,a[v]),p*inv[sons]);
		if(!sons)dfs(fa[u],st,mn,p);
	}
	void work(){
		ans=0;
		for(t=1;t<=n;t++){
			int s=t;
			while(s)dfs(s,1<<s,a[s],1),s=fa[s];
		}
		writeln(ans.x);
	}
}
namespace solve1{
	const int M=19;
	pair<int,int>rmq[N][M];
	pair<int,int>qry(int l,int r){
		int i=__lg(r-l+1);
		return min(rmq[l][i],rmq[r-(1<<i)+1][i]);
	}
	int dfn[N],idfn[N],cnt;
	void dfs(int u){
		idfn[dfn[u]=++cnt]=u;
		for(auto v:e[u])if(!dfn[v])dfs(v);
	}
	mint ans;
	void build(int l,int r){
		if(l>r)return;
		auto res=qry(l,r);
		ans+=mint(1)*(res.se-l+1)*(r-res.se+1)*res.fi;
		build(l,res.se-1);build(res.se+1,r);
	}
	void work(){
		ans=cnt=0;
		for(int i=1;i<=n;i++)memset(rmq[i],0x3f,sizeof rmq[i]);
		for(int i=1;i<=n;i++)if(deg[i]==1){dfs(i);break;}
		for(int i=1;i<=n;i++)rmq[dfn[i]][0]=mp(a[i],dfn[i]);
		for(int j=1;j<M;j++)for(int i=1;i+(1<<j)-1<=n;i++)
			rmq[i][j]=min(rmq[i][j-1],rmq[i+(1<<(j-1))][j-1]);
		//计算根的贡献
		ans=a[root];
		for(int i=1;i<=n;i++)if(i!=root){
			if(deg[root]==1)ans+=qry(min(dfn[root],dfn[i]),max(dfn[root],dfn[i])).fi;
			else{
				if(dfn[i]<dfn[root])ans+=qry(dfn[i],dfn[root]).fi*inv[2]+qry(dfn[i],n).fi*inv[2];
				else ans+=qry(dfn[root],dfn[i]).fi*inv[2]+qry(1,dfn[i]).fi*inv[2];
			}
		}
		build(1,dfn[root]-1);build(dfn[root]+1,n);
		writeln(ans.x);
	}
}
signed main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	for(int i=1;i<N;i++)inv[i]=mint(1)/i;
	read(T);
	while(T--){
		read(n,root);
		for(int i=1;i<=n;i++)read(a[i]),deg[i]=0,fa[i]=0,e[i].clear();
		for(int i=1,u,v;i<n;i++)read(u,v),e[u].pb(v),e[v].pb(u),deg[u]++,deg[v]++;
		dfs(root);
		int mxdeg=0;for(int i=1;i<=n;i++)chkmx(mxdeg,deg[i]);
		if(n<=10)solve0::work();
		else if(mxdeg==2)solve1::work();
	}
	return 0;
}
