#include<iostream>
#include<cstdio>
#include<vector>
#define int long long
using namespace std;
const int N=4e5+10;
const int p=998244353;
int a[N];
int T,n,rt;
int cnt,A[N];
int st[N],l[N],r[N];
vector <int> e[N];
void dfs(int x,int fa)
{
	A[++cnt]=a[x];
	for(int i:e[x]) if(i!=fa) dfs(i,x);
}
signed main()
{
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	scanf("%lld",&T);
	while(T--)
	{
		scanf("%lld%lld",&n,&rt);
		for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
		for(int i=1;i<n;i++)
		{
			int x,y;
			scanf("%lld%lld",&x,&y);
			e[x].push_back(y),e[y].push_back(x);
		}
		dfs(rt,0);
		for(int i=1,top=0;i<=n;i++)
		{
			while(top&&A[i]<A[st[top]]) top--;
			!top?l[i]=1:l[i]=st[top]+1;st[++top]=i;
		}
		for(int i=n,top=0;i>=1;i--)
		{
			while(top&&A[i]<A[st[top]]) top--;
			!top?r[i]=n:r[i]=st[top]-1;st[++top]=i;
		}
		int ans=0;
		for(int i=1;i<=n;i++)
			ans=(ans+A[i]*(i-l[i]+1)%p*(r[i]-i+1)%p)%p;
		printf("%lld\n",ans);
		cnt=0;for(int i=1;i<=n;i++) e[i].clear();
	}
	return 0;
}
