#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=4e5+10;
const int mod=998244353;
int T,n,root,ans;
int a[N];
vector<int> e[N];
int ksm(int a,int l)
{
	int res=1;
	for(;l;l>>=1,a=a*a%mod)
		if(l&1) res=res*a%mod;
	return res; 
}
int inv(int x){return ksm(x,mod-2);}
namespace link
{
	int L[N],R[N];
	vector<int> s;
	void dfs(int u,int fa)
	{
		s.push_back(a[u]);
		for(int v:e[u])
			if(v!=fa) dfs(v,u); 
	}
	void main()
	{
		s.clear();s.push_back(0);
		dfs(root,root);
		for(int i=1;i<=n;i++) L[i]=R[i]=i;
		for(int i=1;i<=n;i++) 
			while(L[i]>1 && s[i]<=s[L[i]-1]) L[i]=L[L[i]-1];
		for(int i=n;i>=1;i--)
			while(R[i]<n && s[i]<=s[R[i]+1]) R[i]=R[R[i]+1];
		int ans=0;
//		cout<<"!"<<s[1]<<endl;
		for(int i=1;i<=n;i++)
			ans=(ans+s[i]*(i-L[i]+1)%mod*(R[i]-i+1)%mod)%mod;
		cout<<ans<<endl;
	}
}
namespace flower
{
	vector<int> s;
	void main()
	{
		int ans=a[root];
		if(n==1) return cout<<ans<<endl,void();
		s.clear();;
		for(int i=1;i<=n;i++)
			if(i!=root && a[i]<=a[root]) s.push_back(a[i]);
		sort(s.begin(),s.end());
		for(int i=0;i<s.size();i++)
		{
			ans=((ans+(n-2-i)*inv(ksm(2,i+1))%mod+2)*s[i]%mod);
		}
		cout<<ans<<endl;
	}
}
void solve()
{
	ans=0;
	cin>>n>>root;
	for(int i=1;i<=n;i++) cin>>a[i],e[i].clear();
	for(int i=1,u,v;i<n;i++)
	{
		cin>>u>>v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	if(e[root].size()==n-1) flower::main();
	else link::main();
}
signed main()
{
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	cin>>T;
	while(T--) solve();
}
