#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10,M=1e6+10,mod=998244353;
int maxx,stk[N][3],n,root,h[N],idx,siz[N],w[N],res,fa[N];
struct edge{int v,nex;}e[M];
void add(int u,int v){e[++idx]=edge{v,h[u]};h[u]=idx;}
int mul(int a,int b){int res=1;while(b){if(b&1) res=1ll*res*a%mod;a=1ll*a*a%mod;b>>=1;}return res;}
void dfs(int u,int f)
{
	fa[u]=f;siz[u]=0;
	for(int i=h[u];i;i=e[i].nex)
	{
		int v=e[i].v;if(v==f) continue;
		dfs(v,u);siz[u]++;
	}
	maxx=max(maxx,siz[u]);
}
bool check(int x,int y)
{
	while(y) 
	{
		if(y==x) return true;
		y=fa[y];
	}
	return false;
}
int tt,l[N],r[N],a[N];
void dfs1(int u)
{
	a[++tt]=w[u];
	for(int i=h[u];i;i=e[i].nex)
	{
		int v=e[i].v;if(v!=fa[u]) dfs1(v);
	}
}
int solve()
{
	int res=0;dfs1(root);
	int top=0;
	for(int i=1;i<=n;i++)
	{
		while(top&&a[stk[top][0]]>a[i]) r[stk[top--][0]]=i;
		stk[++top][0]=i;
	}
	while(top) r[stk[top--][0]]=n+1;
	top=0;
	for(int i=n;i>=1;i--)
	{
		while(top&&a[stk[top][0]]>=a[i]) l[stk[top--][0]]=i;
		stk[++top][0]=i;
	}
	while(top) l[stk[top--][0]]=0;
	for(int i=1;i<=n;i++) res=(res+1ll*a[i]*(r[i]-i)%mod*(i-l[i])%mod)%mod;
	return res;
}
int main()
{
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	int T;scanf("%d",&T);
	while(T--)
	{
		memset(h,0,sizeof(h));idx=0;res=0;scanf("%d%d",&n,&root);
		for(int i=1;i<=n;i++) scanf("%d",&w[i]),res=(1ll*res+2ll*w[i])%mod;
		for(int u,v,i=1;i<n;i++) scanf("%d%d",&u,&v),add(u,v),add(v,u);
		dfs(root,0);int top=0;
		/*for(int i=1;i<=n;i++)
		    for(int j=1;j<=n;j++)
		        if(check(i,j)) 
		        {
		        	stk[++top][0]=i;
		        	stk[top][1]=1;
		        	stk[top][2]=w[i];
		        	while(top)
		        	{
		        		int u=stk[top][0],k=stk[top][1],val=stk[top][2];top--;
		        		if(u==j)
						{
							res=(res+1ll*val*mul(k,mod-2)%mod)%mod;
							continue;
						}
		        		for(int i=h[u];i;i=e[i].nex)
		        		{
		        			int v=e[i].v;if(v==fa[u]) continue;
		        			stk[++top][0]=v;
				        	stk[top][1]=1ll*k*siz[u]%mod;
				        	stk[top][2]=min(val,w[v]);
						}
					}
				}*/
		if(maxx>1) printf("%d\n",((res-w[root])%mod+mod)%mod);
		else printf("%d\n",solve());
	}
	return 0;
}
/*
1
5 3
1 1 3 4 5
3 4
4 1
1 5
5 2
*/