#include<bits/stdc++.h>
#ifdef xay5421
#define D(...) fprintf(stderr,__VA_ARGS__)
#else
#define D(...) ((void)0)
#endif
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define each(x,v) for(auto&x:v)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
using namespace std;
template<class T>
void rd(T&x){
	int f=0,c;
	while(!isdigit(c=getchar()))f^=!(c^45);
	x=(c&15);
	while(isdigit(c=getchar()))x=x*10+(c&15);
	if(f)x=-x;
}
const int P=998244353;
inline int ad(int x,int y){return x+y>=P?x+y-P:x+y;}
inline int su(int x,int y){return x-y<0?x-y+P:x-y;}
inline int mu(int x,int y){return 1ULL*x*y%P;}
inline void uad(int&x,int y){(x+=y)>=P&&(x-=P);}
inline void usu(int&x,int y){(x-=y)<0&&(x+=P);}
const int N=400005;
int T,n,root,a[N],id[N],fa[N],son[N],bad_son[N],fac[N],inv[N],ifac[N];
vector<int>e[N];
void dfs1(int k1,int k2){
	fa[k1]=k2;
	each(x,e[k1])if(x!=k2)++son[k1],dfs1(x,k1);
}
bool vis[N];
bool sub[N];
void ban(int x){
	vis[x]=1;
	while(x){
		if(!sub[x]){
			sub[x]=1;
			if(x!=root)++bad_son[fa[x]];
			x=fa[x];
		}else{
			break;
		}
	}
}
int dp[N];
void dfs2(int k1,int k2){
	if(vis[k1])dp[k1]=0;
	else dp[k1]=1;
	each(x,e[k1])if(x!=k2){
		dfs2(x,k1);
		if(!vis[x])uad(dp[k1],mu(dp[x],inv[bad_son[k1]+1-int(sub[x])]));
	}
	if(vis[k1])dp[k1]=0;
}
int calc(){
	dfs2(root,0);
	int sum=0;
	rep(i,1,n)if(!vis[i])uad(sum,dp[i]);
	return sum;
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	fac[0]=fac[1]=inv[0]=inv[1]=ifac[0]=ifac[1]=1;
	rep(i,2,N-1)fac[i]=1LL*fac[i-1]*i%P,inv[i]=1LL*(P-P/i)*inv[P%i]%P,ifac[i]=1LL*ifac[i-1]*inv[i]%P;
	rd(T);
	while(T--){
		rd(n),rd(root);
		fill(vis+1,vis+n+1,0);
		fill(sub+1,sub+n+1,0);
		fill(son+1,son+n+1,0);
		fill(bad_son+1,bad_son+n+1,0);
		rep(i,1,n)rd(a[i]),id[i]=i;
		sort(id+1,id+n+1,[&](int lhs,int rhs){return a[lhs]<a[rhs];});
		rep(i,1,n)e[i].clear();
		rep(i,2,n){
			int u,v;
			rd(u),rd(v);
			e[u].pb(v);
			e[v].pb(u);
		}
		dfs1(root,0);
		int ans=0;
		int ret=calc();
		rep(i,0,n-1){
			if(i){
				ban(id[i]);
				int k1=id[i];
				while(k1){
					if(k1!=id[i]&&vis[k1])break;
					int old=dp[k1];
					usu(ret,dp[k1]);
					
					if(vis[k1])dp[k1]=0;
					else dp[k1]=1;
					each(x,e[k1])if(x!=fa[k1]){
						if(!vis[x])uad(dp[k1],mu(dp[x],inv[bad_son[k1]+1-int(sub[x])]));
					}
					if(vis[k1])dp[k1]=0;
					
					uad(ret,dp[k1]);
					if(dp[k1]==old)break;
					k1=fa[k1];
				}
			}
			uad(ans,mu(ret,su(a[id[i+1]]%P,a[id[i]]%P)));
		}
		printf("%d\n",ans);
	}
	return 0;
}
