#include <bits/stdc++.h>
using namespace std;
const int N=400000,P=998244353;
int Power(int x,int y) {
	int res=1;
	while (y) {
		if (y&1) res=1ll*x*res%P;
		x=1ll*x*x%P,y>>=1;
	}
	return res;
}

int n,root,val[N+9],ord[N+9],Ans;
int v[N+9];
int mxpoint;
bool cmp(int o1,int o2) {return val[o1]<val[o2];}
vector <int> E[N+9];

void dfs(int now,int lst) {
	for (auto nxt:E[now]) {
		if (nxt==lst) continue;
	}
}

namespace subtask1 {
	int po;
	void solve() {
		if (mxpoint==root) {
			for (int i=1;i<=n;i++){ 
				if (ord[i]==root) {
					po=1ll*(n-i)*v[i]%P; Ans=(Ans+1ll*po*val[root])%P;
					break;
				} else po=(v[i]+1ll*((n-i-1ll)*v[i+1]%P)*v[i])%P; Ans=(Ans+1ll*po*val[ord[i]])%P;
			}
		} else {
			for (int i=1;i<=n;i++)
				if (ord[i]==root) {
					continue;
				} else if (ord[i]==mxpoint) {
					po=(n-i-1ll)*v[i]%P; Ans=(Ans+1ll*po*val[mxpoint])%P;
					break;
				} else {
					po=(1ll*i*v[i]+1ll*((n-i-2ll)*v[i+1]%P)*v[i])%P; Ans=(Ans+1ll*po*val[ord[i]])%P;
				}	
			for (int i=1;i<=n;i++)
				if (ord[i]==root || ord[i]==mxpoint) {
					po=1+(n-i-1ll)*v[i]%P; Ans=(Ans+1ll*po*val[ord[i]])%P;
					break;
				} else {
					po=(1ll*i*v[i]+1ll*((n-i-2ll)*v[i+1]%P)*v[i])%P; Ans=(Ans+1ll*po*val[ord[i]])%P;
				}
		}
	}
}

namespace subtask2 {
	int id[N+9],po;
	set <int> st;
	set <int>::iterator IT;
	void solve() {
		for (int i=1;i<=n;i++)
			if (E[i].size()==1) {
				int now=i,lst=0;
				for (int j=1;j<=n;j++) {
					id[now]=j;
					if (j==n) break;
					else if (E[now][0]==lst) lst=now,now=E[now][1];
					else lst=now,now=E[now][0];
				}
				break;
			}
		int left_bound=0,right_bound=n+1,mid=id[root];
		bool flag_left=0,flag_right=0;
		st.clear();
		for (int i=1;i<=n;i++) {
			int pos=id[ord[i]];
			if (ord[i]==root) {
				po=right_bound-left_bound-2;
			} else if (pos<mid) {
				flag_left=1,left_bound=max(left_bound,pos);
				int upper_bound=mid+1,lower_bound=0;
				IT=st.lower_bound(pos);
				if (IT!=st.end()) upper_bound=*IT;
				if (IT!=st.begin()) lower_bound=*prev(IT);
				if (upper_bound>mid) {
					if (flag_right) po=(1ll*(mid-pos)+v[2])*(pos-lower_bound)%P;
					else po=1ll*(upper_bound-pos)*(pos-lower_bound)%P-1;
				} else po=1ll*(upper_bound-pos)*(pos-lower_bound)%P-1;
			} else {
				flag_right=1,right_bound=min(right_bound,pos);
				int upper_bound=mid-1,lower_bound=n+1;
				IT=st.lower_bound(pos);
				if (IT!=st.end()) lower_bound=*IT;
				if (IT!=st.begin()) upper_bound=*prev(IT);
				if (upper_bound<mid) {
					if (flag_left) po=(1ll*(pos-mid)+v[2])*(lower_bound-pos)%P;
				} else po=1ll*(pos-upper_bound)*(lower_bound-pos)%P-1;
			}
			Ans=(Ans+1ll*po*val[ord[i]])%P;
			st.insert(pos);
		}
	}
}

namespace subtask3 {
	void solve() {
		
	}
}

void solve() {
	scanf("%d%d",&n,&root);
	Ans=0;
	for (int i=1;i<=n;i++) scanf("%d",val+i),ord[i]=i,Ans=(Ans+val[i])%P,E[i].clear();
	sort(ord+1,ord+1+n,cmp);
	for (int i=1;i<n;i++) {
		int u,v; scanf("%d%d",&u,&v);
		E[u].push_back(v),E[v].push_back(u);
	}
	mxpoint=1;
	for (int i=2;i<=n;i++) if (E[mxpoint].size()<E[i].size()) mxpoint=i;
	if (E[mxpoint].size()==n-1) subtask1::solve();
	else if (E[mxpoint].size()==2) subtask2::solve();
	else subtask3::solve();
	printf("%d\n",Ans);
}

int main() {
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	for (int i=1;i<=N;i++) v[i]=Power(i,P-2);
	int T; cin>>T;
	while (T--) solve();
	return 0;
}
