#include<bits/stdc++.h>
#define CT cout << "\n-----------------------\n";
using namespace std;
typedef long long ll;
const int inf=1<<30;
const ll Inf=1ll<<60;
const int N=400005;
int mn[N],fa[N],a[N],b[N];
vector<int> e[N];
const ll mod=998244353;
const ll inv2=(mod+1)/2;
inline void reduce(int&x){
	(x>=mod)&&(x-=mod);
}
void dfs(int u,int f){
	mn[u]=a[u];
	fa[u]=f;
	for(int to:e[u]){
		if(to==f) continue;
		dfs(to,u);
		mn[u]=min(mn[u],mn[to]);
	}
}
int cnt[N],ans,n,rt,m;
void fixit(int u,int curm,int ano){
	curm=min(curm,a[u]);
	if(curm<ano) reduce(ans+=b[curm]);
	else ans=(ans+inv2*(b[curm]+b[ano]))%mod;
	for(int to:e[u]){
		if(to==fa[u]) continue;
		fixit(to,curm,ano);
	}
}
void solve(int u,int curm){
	curm=min(curm,a[u]);
	int curp=1,E=0;
	for(int i=1;i<curm;++i){
		for(int j=1;j<=cnt[i];++j){
			curp=inv2*curp%mod;
			reduce(E+=1ll*curp*b[i]%mod);
		}
	}
	ans=(ans+E+1ll*curp*b[curm])%mod;
	for(int to:e[u]){
		if(to==fa[u]) continue;
		cnt[mn[to]]++;
	}
	for(int to:e[u]){
		if(to==fa[u]) continue;
		cnt[mn[to]]--;
		solve(to,curm);
		cnt[mn[to]]++;
	}
	for(int to:e[u]){
		if(to==fa[u]) continue;
		cnt[mn[to]]--;
	}
}
bool cmp(int A,int B){
	return a[A]<a[B];
}
int f[N],siz[N],id[N];
map<int,int> ct[N];
int getf(int x){
	while(x!=f[x]) x=f[x]=f[f[x]];
	return x;
}
void mg(int x,int y){
	x=getf(x); y=getf(y);
	f[x]=y; siz[y]+=siz[x];
}
int rec[N],recp[N];
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int T; cin >> T;
	while(T--){
		cin >> n >> rt;
		for(int i=1;i<=n;++i) e[i].clear();
		for(int i=1;i<=n;++i){
			cin >> a[i];
			b[i]=a[i];
		}
		sort(b+1,b+n+1);
		m=unique(b+1,b+n+1)-b;
		b[m]=inf;
		for(int i=1;i<=n;++i)
			a[i]=lower_bound(b+1,b+m+1,a[i])-b;
		for(int i=1;i<n;++i){
			int x,y; cin >> x >> y;
			e[x].push_back(y);
			e[y].push_back(x);
		}
		dfs(rt,0);
		int mx=0;
		for(int i=1;i<=n;++i)
			mx=max(mx,int(e[i].size()));
		ans=0;
		if(n<=10){
			for(int i=1;i<=n;++i)
				solve(i,m+1);
			cout << ans << '\n';
			continue;
		}
		if(mx<=2){
			int qwq;
			if(e[rt].size()==2){
				qwq=a[rt]; a[rt]=0;
			}
			for(int i=1;i<=n;++i) ct[i].clear();
			for(int i=1;i<=n;++i) reduce(ans+=b[a[i]]);
			for(int i=1;i<=n;++i)
				f[i]=i,siz[i]=1,id[i]=i;
			sort(id+1,id+n+1,cmp);
			for(int i=1;i<=n;++i){
				int u=id[i];
				for(int to:e[u]){
					if(ct[u][to]) continue;
					ct[u][to]=ct[to][u]=i;
				}
			}
			for(int i=n;i>=1;--i){
				int u=id[i];
				int s1=0,s2=0;
				for(int to:e[u]){
					if(i>ct[u][to]) continue;
					if(!s1) s1=siz[getf(to)],mg(u,to);
					else s2=siz[getf(to)],mg(u,to);
				}
				reduce(ans+=(1ll*(s1+1)*(s2+1)+mod-1)%mod*b[a[u]]%mod);
			}
			if(e[rt].size()==2){
				a[rt]=qwq;
				int tmp=0;
				for(int to:e[rt])
					tmp^=to;
				for(int to:e[rt])
					fixit(to,a[rt],mn[tmp^to]);
			}
			cout << ans << '\n';
			continue;
		}else
		if(mx==n-1){
			for(int i=1;i<=n;++i)
				reduce(ans+=b[a[i]]);
			for(int to:e[rt])
				cnt[a[to]]++;
			int curp=1,E=0;
			for(int i=1;i<=m;++i){
				for(int j=1;j<=cnt[i];++j){
					curp=inv2*curp%mod;
					reduce(E+=1ll*curp*b[i]%mod);
				}
				rec[i]=E;
				recp[i]=curp;
			}
			for(int to:e[rt]){
				int v=min(a[to],a[rt]);
				ans=(ll(ans)+rec[v]+1ll*recp[v]*b[v])%mod;
			}
			cout << ans << '\n';
			continue;
		}
		for(int i=1;i<=n;++i)
			solve(i,m+1);
		cout << ans << '\n';
	}
	return 0;
}
/*
1
2 1
1 2
1 2
*/
