/*
after dusk passed,
there is a starry sky.
*/
#include <bits/stdc++.h>
#define m_k make_pair
#define inf 0x3f3f3f3f
#define mod 998244353
#define int long long
#define len(a) (int)a.size()
using namespace std;
const int N=5*1e5+100;
int T,n,rt,a[N],b[N],w,fa[N],inv[N],vi[N];
int val[N];
vector <int> e[N];
inline void add(int &a,int b){a=((a+b>=mod)?a+b-mod:a+b);}
inline void del(int &a,int b){a=((a-b<0)?a-b+mod:a-b);}
inline void mul(int &a,int b){a=a*b%mod;}
inline int m_pow(int a,int b)
{
	int ans=1;
	while (b)
	{
		if (b&1) mul(ans,a);
		b>>=1;
		mul(a,a);
	}
	return ans;
}
inline int read()
{
	int f=1,x=0;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
	return x*f;
}
void dfs(int x,int father)
{
	fa[x]=father;
	for (int u:e[x]) if (u!=fa[x]) dfs(u,x);
}
void dfs1(int x,int p,int fr,int MIN)
{
	int cnt=0;
	for (int u:e[x]) if (u!=fa[x]&&!vi[u]) cnt++;
	if (!cnt)
	{
		if (x==fr) return;
		dfs1(fa[x],p,fr,MIN);
		return;
	}
	for (int u:e[x]) if (u!=fa[x]&&!vi[u])
	{
		vi[u]=1;
		add(val[u],p*inv[cnt]%mod*b[min(MIN,a[u])]%mod);
		dfs1(u,p*inv[cnt]%mod,fr,min(MIN,a[u]));
		vi[u]=0;
	}
}
signed main()
{
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	T=read();
	while (T--)
	{
		n=read();rt=read();
		w=0;
		for (int i=1;i<=n;i++) a[i]=read(),b[++w]=a[i];
		sort(b+1,b+1+w);w=unique(b+1,b+1+w)-b-1;
		for (int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+1+w,a[i])-b;
		for (int i=1;i<=n;i++) e[i].clear();
		for (int i=1;i<n;i++)
		{
			int u=read(),v=read();
			e[u].push_back(v);e[v].push_back(u);
		}
		inv[1]=1;
		for (int i=2;i<=n;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod;
		dfs(rt,rt);
		int ans=0;
		for (int i=1;i<=n;i++)
		{
			for (int j=1;j<=n;j++) vi[j]=val[j]=0;
			vi[i]=1;
			dfs1(i,1,i,a[i]);
			add(val[i],b[a[i]]);
			for (int j=1;j<=n;j++) add(ans,val[j]);
		}
		printf("%lld\n",ans);
	}
}