#include<bits/stdc++.h>
using namespace std;

#define Mod(x) (x>=P)&&(x-=P)
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)
#define erep(i,a) for(int i=hd[a],y;y=to[i],i;i=nxt[i])

typedef long long ll;
void Max(int &x,int y){x<y&&(x=y);}
void Min(int &x,int y){x>y&&(x=y);}

bool f1;
char IO;
int rd(int res=0){
	bool f=0;
	while(IO=getchar(),IO<48||IO>57)
		f|=IO=='-';
	do res=(res<<1)+(res<<3)+(IO^48);
	while(isdigit(IO=getchar()));
	return f?-res:res;
}
const int M=4e5+10,P=998244353;
int C[M],tot,n,A[M],rt;
int stk[M];
vector<int>G[M];
void dfs(int x,int f){
	C[++tot]=A[x];
	for(int y:G[x])if(y!=f)dfs(y,x);
}
bool f2;
int main(){
	cerr<<(&f1-&f2)/1024.0/1024<<endl;
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	rep(cas,1,rd()){
		n=rd(),rt=rd(),tot=0;
		rep(i,1,n)A[i]=rd();
		rep(i,2,n){
			int a=rd(),b=rd();
			G[a].push_back(b);
			G[b].push_back(a);
		}
		dfs(rt,0);
		int top=0,sum=0,ans=0;
		stk[0]=n+1;
		drep(i,n,1){
			while(top&&C[stk[top]]>C[i])
				sum=(sum+P-1ll*(stk[top-1]-stk[top])*C[stk[top]]%P)%P,top--;
			stk[++top]=i;
			sum+=(stk[top-1]-i)*C[i]%P,Mod(sum);
			ans+=sum,Mod(ans);
		}
		printf("%d\n",ans);
		rep(i,1,n)G[i].clear();
	}
	return 0;
}
