#include<bits/stdc++.h>
using namespace std;
const int md=998244353,N=400050;
typedef long long LL;
int T,head[N],cnt,n,rt,deg[N],a[N];
struct edge{
	int to,nxt;
}e[N*2];
inline int pow(int a,int b){
	int res=1;
	for(;b;b>>=1,a=(LL)a*a%md)if(b&1)res=(LL)res*a%md;
	return res;
}
namespace sub_chain{
	int b[N],t,ans;
	void dfs(int u,int p){
		b[++t]=a[u];
		for(int i=head[u];i;i=e[i].nxt)if(e[i].to!=p)
			dfs(e[i].to,u);
	}
	pair<int,int>d[N*4];
	void build(int l,int r,int o){
		if(l==r)d[o]=make_pair(b[l],l);
		else{
			const int mid=(l+r)/2;
			build(l,mid,o<<1);
			build(mid+1,r,o<<1|1);
			d[o]=min(d[o<<1],d[o<<1|1]);
		}
	}
	int DFS(int u,int p){
		int res=a[u];
		for(int i=head[u];i;i=e[i].nxt)
			if(e[i].to!=p)
				res=min(res,DFS(e[i].to,u));
		return res;
	}
	pair<int,int>query(int l,int r,int o,const int&L,const int&R){
		if(L<=l&&r<=R)return d[o];
		const int mid=(l+r)/2;
		if(L<=mid&&mid<R)
			return min(query(l,mid,o<<1,L,R),query(mid+1,r,o<<1|1,L,R));
		if(L<=mid)
			return query(l,mid,o<<1,L,R);
		return query(mid+1,r,o<<1|1,L,R);
	}
	void solve(int l,int r){
		if(l>r)return;
		auto it=query(1,t,1,l,r);
		int p=it.second;
		ans=(ans+(LL)(p-l+1)*(r-p+1)%md*it.first)%md;
		solve(l,p-1);
		solve(p+1,r);
	}
	void calc(){
		build(1,t,1);
		solve(1,t);
	}
	void work(){
		ans=0;
		vector<int>sn;
		for(int i=head[rt];i;i=e[i].nxt)sn.emplace_back(e[i].to);
		for(int i=0;i<(int)sn.size();++i){
			int v=sn[i];
			t=0;
			dfs(v,rt);
			calc();
			int ob=sn.size()==1?2e9:DFS(sn[i^1],rt),pm=a[rt];
			for(int j=1;j<=t;++j){
				pm=min(pm,b[j]);
				if(ob>=pm)ans=(ans+pm)%md;
				else ans=(ans+(ob+pm)*(md+1LL)/2)%md;
			}
		}
		ans=(ans+a[rt])%md;
		cout<<ans<<'\n';
	}
}
namespace sub1{
	vector<int>vc;
	int fa[N],dp[12][12],mn[N],ans,fac[N],iv[N];
	int tmp[12][12];
	inline int binom(int n,int m){return(LL)fac[n]*iv[m]%md*iv[n-m]%md;}
	void dfs0(int u,int p){
		mn[u]=a[u];
		for(int i=head[u];i;i=e[i].nxt)
			if(e[i].to!=p){
				fa[e[i].to]=u;
				dfs0(e[i].to,u);
				mn[u]=min(mn[u],mn[e[i].to]);
			}
	}
	void DFS(int u,int p){
		for(int i=1;i<=n;++i)
			ans=(ans+(LL)vc[i]*dp[u][i])%md;
		vector<int>sn;
		for(int i=head[u];i;i=e[i].nxt)if(e[i].to!=p)
			sn.emplace_back(e[i].to);
		int sz=sn.size();
		for(int v:sn){
			memset(tmp,0,sizeof tmp);
			for(int i=1;i<=n;++i){
				int x=min(a[v],i);
				tmp[0][x]=(tmp[0][x]+dp[u][i])%md;
			}
			for(int p:sn)if(p!=v){
				for(int i=n-1;i>=0;--i)
					for(int j=1;j<=n;++j){
						int nj=min(j,mn[p]);
						tmp[i+1][nj]=(tmp[i+1][nj]+(LL)tmp[i][j]*(i+1))%md;
					}
			}
			for(int i=0;i<sz;++i)
				for(int j=1;j<=n;++j){
					dp[v][j]=(dp[v][j]+(LL)tmp[i][j]*fac[sz-i-1]%md*iv[sz])%md;
				}
			DFS(v,u);
		}
	}
	void work(){
		ans=0;
		for(int i=*fac=1;i<=n;++i)fac[i]=(LL)fac[i-1]*i%md;
		iv[n]=pow(fac[n],md-2);
		for(int i=1;i<=n;++i)fa[i]=0;
		for(int i=n-1;~i;--i)iv[i]=(i+1LL)*iv[i+1]%md;
		vc.clear();
		vc.emplace_back(-1);
		for(int i=1;i<=n;++i)vc.emplace_back(a[i]);
		sort(vc.begin(),vc.end());
		vc.erase(unique(vc.begin(),vc.end()),vc.end());
		for(int i=1;i<=n;++i){
			a[i]=lower_bound(vc.begin(),vc.end(),a[i])-vc.begin();
		}
		dfs0(rt,0);
		for(int u=1;u<=n;++u){
			memset(dp,0,sizeof dp);
			dp[u][a[u]]=1;
			DFS(u,fa[u]);
		}
		cout<<ans<<'\n';
	}
}
namespace sub3{
	int fac[N],iv[N];
	inline int binom(int n,int m){return(LL)fac[n]*iv[m]%md*iv[n-m]%md;}
	int b[N],t;
	void work(){
		for(int i=*fac=1;i<=n;++i)fac[i]=(LL)fac[i-1]*i%md;
		iv[n]=pow(fac[n],md-2);
		for(int i=n-1;~i;--i)iv[i]=(i+1LL)*iv[i+1]%md;
		int ans=0;
		for(int i=head[rt];i;i=e[i].nxt)
			b[++t]=min(a[rt],a[e[i].to]);
		sort(b+1,b+n,greater<int>());
		ans=0;
		--n;
		for(int i=1;i<=n;++i){
			int s=0;
			for(int j=i;j<=n;++j)
				s=(s+(LL)b[j]*fac[j-1]%md*iv[j-i])%md;
			ans=(ans+(LL)i*fac[n-i]%md*s)%md;
		}
		ans=(LL)ans*iv[n]%md;
		ans=(ans+a[rt])%md;
		cout<<ans<<'\n';
	}
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	for(cin>>T;T--;){
		cin>>n>>rt;
		for(int i=1;i<=n;++i)head[i]=deg[i]=0;
		cnt=0;
		for(int i=1;i<=n;++i)cin>>a[i];
		for(int i=1;i<n;++i){
			int u,v;
			cin>>u>>v;
			e[++cnt]=(edge){v,head[u]},head[u]=cnt;
			e[++cnt]=(edge){u,head[v]},head[v]=cnt;
			++deg[u],++deg[v];
		}
		int mxdg=*max_element(deg+1,deg+n+1);
		if(n<=10)sub1::work();
		else if(mxdg<=2)sub_chain::work();
		//if(deg[rt]==n-1)sub3::work();
	}
	return 0;
}
