#include<bits/stdc++.h>
using namespace std;
template<class T>inline void read(T &x){
	x=0;
	char c=getchar();
	while(!isdigit(c))c=getchar();
	while(isdigit(c))x=x*10+(c&15),c=getchar();
}
const int P=998244353;
inline int ksm(int a,int b,int s=1){
	for(;b;b>>=1,a=(long long)a*a%P)if(b&1)s=(long long)s*a%P;
	return s;
}
inline int &add(int &a,int b){
	return (a+=b)>=P?a-=P:a;
}
inline int &sub(int &a,int b){
	return (a-=b)<0?a+=P:a;
}
#define N 400001
int n=400000,rt,a[N],mi[N],fa[N],inv[N],ans,dep[N],sz[N];
vector<int>e[N];
inline void dfs1(int x,int y){
	dep[x]=dep[y]+1,fa[x]=y,mi[x]=a[x],sz[x]=1;
	for(auto &i:e[x]){
		if(i==y)continue;
		dfs1(i,x),mi[x]=min(mi[x],mi[i]),sz[x]+=sz[i];
	}
}
inline bool check1(){
	int cnt1=0,cnt2=0;
	for(int x=1;x<=n;x++){
		int deg=e[x].size();
		if(deg==1)++cnt1;else if(deg==2)++cnt2;else return 0;
	}
	return cnt1==2 || n==1;
}
inline bool check2(){
	int deg=e[rt].size();
	return deg==n-1;
}
namespace subtask1{
	int stk[N],top;
	inline void dfs(int x,int y,int s,int t){
		s=min(s,a[x]),t=min(t,a[x]);
		add(ans,(long long)(s+t)*inv[2]%P);
		for(auto &i:e[x]){
			if(i==y)continue;
			dfs(i,x,s,t);
		}
	}
	inline int main(){
		if(e[rt].size()==2){
			int x=e[rt][0],y=e[rt][1];
			dfs(x,rt,a[rt],min(a[rt],mi[y])),dfs(y,rt,a[rt],min(a[rt],mi[x]));
			add(ans,a[rt]);
		}
		else dfs(rt,rt,a[rt],a[rt]);
		for(int x=1;x<=n;x++){
			if(e[x].size()==1){
				dep[0]=dep[x]+1;
				top=0;
				int sum=0;
				for(int no=x;no!=rt;no=fa[no]){
					while(top && a[stk[top]]>=a[no])sub(sum,(long long)a[stk[top]]*(dep[stk[top-1]]-dep[stk[top]])%P),--top;
					add(sum,(long long)a[no]*(dep[stk[top]]-dep[no])%P);
					add(ans,sum);
					stk[++top]=no;
				}
			}
		}
		dep[0]=0;
		printf("%d\n",ans);
		return 0;
	}
}
namespace subtask2{
	int vis[N],deg[N];
	inline void dfs(int x,int rt,int v,int p){
		if(!vis[x])add(ans,(long long)v*p%P);
		if(deg[x]==0 && x==rt)return;
		++vis[x];
		if(deg[x]==0 && x!=rt)dfs(fa[x],rt,v,p);
		else
		for(auto &i:e[x]){
			if(i==fa[x])continue;
			if(vis[i])continue;
			--deg[x];
			dfs(i,rt,min(v,a[i]),p*inv[deg[x]+1]);
			++deg[x];
		}
		--vis[x];
	}
	inline int main(){
		for(int i=1;i<=n;i++)deg[i]=(int)e[i].size()-(i!=rt);
		for(int i=1;i<=n;i++)dfs(i,i,a[i],1);
		printf("%d\n",ans);
		return 0;
	}
}
namespace subtask3{
	inline int main(){
		
		return 0;
	}
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	inv[1]=1;
	for(int i=2;i<=n;i++)inv[i]=(long long)(P-P/i)*inv[P%i]%P;
	int T;
	read(T);
	while(T--){
		read(n),read(rt),ans=0;
		for(int i=1;i<=n;i++)read(a[i]),e[i].clear();
		for(int i=1,x,y;i<n;i++)read(x),read(y),e[x].push_back(y),e[y].push_back(x);
		dfs1(rt,0);
		for(int i=1;i<=n;i++)sort(e[i].begin(),e[i].end(),[](const int &x,const int &y)->bool{return a[x]<a[y];});
		if(check1())subtask1::main();else
		if(check2())subtask3::main();
	}
	return 0;
}
