#include <set>
#include <cstdio>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
constexpr int mod=998244353;
constexpr int max_n=400000;
int a[max_n+1];
pair<int,int>t[max_n+1];
vector<int>g[max_n+1];
int deg[max_n+1],dep[max_n+1];
int val[max_n+1];
int pos[max_n+1];
void dfs1(const int u,const int p=0){
	dep[u]=dep[p]+1;
	pos[a[u]]=dep[u];
	for(int v:g[u])
		if(v!=p)
			dfs1(v,u);
}
void test(){
	int n,rt;
	scanf("%d%d",&n,&rt);
	for(int i=1; i<=n; ++i)
		scanf("%d",a+i);
	for(int i=n,u,v; --i; g[u].push_back(v),g[v].push_back(u)){
		scanf("%d%d",&u,&v);
		++deg[u],++deg[v];
	}
	bool flg=deg[rt]<=1;
	for(int i=1; i<=n; ++i)
		if(deg[i]>2){
			flg=0;
			break;
		}
	if(flg){
		for(int i=1; i<=n; ++i)
			t[i].first=a[i],t[i].second=i;
		sort(t+1,t+n+1);
		for(int i=1; i<=n; ++i)
			val[i]=t[i].first,a[t[i].second]=i;
		dfs1(rt);
		set<int>st;
		int ans=0;
		for(int i=1; i<=n; ++i){
			const int p=pos[i];
			st.insert(p);
			set<int>::iterator it=st.find(p);
			const int a=it==st.begin()?p:p-(*prev(it));
			++it;
			const int b=it==st.end()?n-p+1:*it-p;
			ans=(ans+ll(a)*b%mod*val[i])%mod;
		}
		printf("%d\n",ans);
	}
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	int t;
	scanf("%d",&t);
	for(; t; test(),--t);
	return 0;
}
