#include<bits/stdc++.h>
#define pb push_back
#define SZ(x) ((int)((x).size()))
template<typename _Tp>void read(_Tp &x){
	char ch=getchar();bool f=0;while(!isdigit(ch))f|=ch=='-',ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
	if(f)x=-x;
}
template<typename _Tp,typename ...Args>void read(_Tp &x,Args &...args){read(x),read(args...);}
const int N=400005,mod=998244353;typedef long long ll;
template<typename _Tp1,typename _Tp2>inline void add(_Tp1 &a,_Tp2 b){a=a+b>=mod?a+b-mod:a+b;}
template<typename _Tp1,typename _Tp2>inline void sub(_Tp1 &a,_Tp2 b){a=a<b?a+mod-b:a-b;}
ll ksm(ll a,ll b=mod-2){ll res=1;while(b){if(b&1)res=res*a%mod;a=a*a%mod,b>>=1;}return res;}
ll inv[N];int val[N],a[N];std::vector<int> e[N];
int dfn[N],siz[N],id,top[N],son[N],dep[N],Fa[N],ord[N];
void dfs1(int x,int fa){
	siz[x]=1,dep[x]=dep[fa]+1,Fa[x]=fa,val[x]=a[x];
	for(auto v:e[x])if(v!=fa){
		dfs1(v,x),siz[x]+=siz[v],val[x]=std::min(val[x],val[v]);
		if(siz[v]>siz[son[x]])son[x]=v;
	}
}
void dfs2(int x,int topf){
	top[x]=topf,dfn[x]=++id,ord[id]=x;if(son[x])dfs2(son[x],topf);
	for(auto v:e[x])if(!top[v])dfs2(v,v);
}
std::vector<int> vec[N];int t[N],cur[N],s[N],ss[N],res,mn,tot[N];
void dfs3(int x,int fa,int tot){
	if(a[x]>mn)add(tot,ss[x]),add(res,1LL*tot*s[x]%mod);else tot=0;
	int sum=0;for(auto v:e[x])if(v!=fa)sum+=cur[v];
	for(auto v:e[x])if(v!=fa){
		int tmp=sum-cur[v];
		ss[v]=1LL*ss[x]*(tmp+1)%mod,s[v]=1LL*s[x]*inv[tmp+1]%mod,dfs3(v,x,tot);
	}
}
namespace CHAIN{
int b[N],m,f[N][20],Log2[N],ans;
inline int _min(int x,int y){return b[x]<b[y]?x:y;}
void dfs4(int x,int fa){
	b[++m]=a[x];
	for(auto v:e[x])if(v!=fa)dfs4(v,x);
}
int ask(int l,int r){
	int k=Log2[r-l+1];
	return _min(f[l][k],f[r-(1<<k)+1][k]);
}
void solve(int l,int r){
	if(l>r)return;
	int mid=ask(l,r);
	add(ans,1LL*(mid-l+1)*(r-mid+1)%mod*b[mid]%mod);
	solve(l,mid-1),solve(mid+1,r);
}
void MAIN(int n,int rt){
	for(int i=2;i<=n;++i)Log2[i]=Log2[i>>1]+1;
	ans=0;
	for(auto v:e[rt]){
		b[m=1]=a[rt],dfs4(v,rt);
		for(int i=m;i>=1;--i){f[i][0]=i;for(int j=1;j<20&&i+(1<<(j-1))<=m;++j)f[i][j]=_min(f[i][j-1],f[i+(1<<(j-1))][j-1]);}
		solve(1,m);
	}
	printf("%d\n",ans);
}
}
void MAIN(){
	int n,rt;read(n,rt);id=0;
	for(int i=0;i<=n;++i)e[i].clear(),vec[i].clear(),dfn[i]=siz[i]=top[i]=son[i]=dep[i]=Fa[i]=0,cur[i]=0;
	for(int i=1;i<=n;++i)read(a[i]),t[i]=a[i];
	for(int i=1,x,y;i<n;++i)read(x,y),e[x].pb(y),e[y].pb(x);
	if(n==1){printf("%d\n",a[1]%mod);return;}
	std::sort(t+1,t+n+1);int pos=std::unique(t+1,t+n+1)-t-1;
	for(int i=1;i<=n;++i)a[i]=std::lower_bound(t+1,t+pos+1,a[i])-t;
	dfs1(rt,0),dfs2(rt,rt);
	for(int i=1;i<=n;++i)if(i!=rt)vec[val[i]].pb(i);
	int ans=0;
	for(int i=0;i<pos;++i){
		for(auto it:vec[i])cur[it]=1;
		mn=i,ss[rt]=1,s[rt]=1,res=0;
		for(int j=1;j<=n;++j){
			int x=ord[j];tot[x]=tot[Fa[x]];
			if(a[x]>mn)add(tot[x],ss[x]),add(res,1ULL*tot[x]*s[x]%mod);else tot[x]=0;
			int sum=0;for(auto v:e[x])if(v!=Fa[x])sum+=cur[v];
			for(auto v:e[x])if(v!=Fa[x]){
				int tmp=sum-cur[v];
				ss[v]=1ULL*ss[x]*(tmp+1)%mod,s[v]=1ULL*s[x]*inv[tmp+1]%mod;
			}
		}
		add(ans,1LL*(t[i+1]-t[i])*res%mod);
	}
	printf("%d\n",ans);
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	inv[0]=inv[1]=1;for(int i=2;i<N;++i)inv[i]=inv[mod%i]*(mod-mod/i)%mod;
	int _;read(_);
	while(_--)MAIN();
	return 0;
}
