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

#define ll long long
#define ull unsigned long long
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,l,r) for(int i=(l);i>=(r);i--)
#define SZ(a) ((int)a.size())
#define pb push_back
#define fir first
#define sec second

inline int read(){
    int x=0,f=0;char ch=getchar();
    while(!isdigit(ch))f|=ch=='-',ch=getchar();
    while(isdigit(ch))x=10*x+ch-'0',ch=getchar();
    return f?-x:x;
}
template<typename T> void print(T x){
    if(x<0)putchar('-'),x=-x;
    if(x>=10)print(x/10);
    putchar(x%10+'0');
}
template<typename T> void print(T x,char ch){
    print(x),putchar(ch);
}

const int N=1000005;
const int mod=998244353;

vector<int> adj[N];
int a[N],id[N];
bool ok[N];
int tmps[N];
int f[N],g[N],need[N],son[N];
int ALIVE[N],DEAD[N];
int n,rt;

int fac[N],ifac[N],inv[N];
inline int qpow(int a,int b=mod-2){
    int res=1;
    while(b){
        if(b&1)res=1ll*res*a%mod;
        a=1ll*a*a%mod,b>>=1;
    }
    return res;
}
inline void init(int n){
    inv[1]=1;rep(i,2,n)inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
    fac[0]=1;rep(i,1,n)fac[i]=1ll*fac[i-1]*i%mod;
    ifac[n]=qpow(fac[n]);per(i,n,1)ifac[i-1]=1ll*ifac[i]*i%mod;
}
inline int C(int n,int m){
    return 1ll*fac[n]*ifac[n-m]%mod*ifac[m]%mod;
}

int dad[N];

void dfs1(int u,int fa){
    dad[u]=fa;
    for(auto v:adj[u]){
        if(v==fa)continue;
        need[u]++,son[u]++,DEAD[u]++;
        dfs1(v,u);
    }
}
void dfs2(int u){
    g[u]=ok[u];
    for(auto v:adj[u]){
        if(v==dad[u])continue;
        dfs2(v);
        g[u]&=g[v];
    }
}
int glo;
void calc(int u){
    int alive=0,dead=0;
    for(auto v:adj[u]){
        if(v==dad[u])continue;
        if(g[v])alive++;
        else dead++;
    }
    glo=(glo+mod-f[u])%mod;
    f[u]=1;
    for(auto v:adj[u]){
        if(v==dad[u])continue;
        if(g[v])alive--;
        else dead--;
        // printf("alive=%d,dead=%d\n",alive,dead);
        int P=inv[dead+1];
        f[u]=(f[u]+1ll*P*f[v])%mod;
        tmps[v]=1ll*P*f[v]%mod;
        // printf("f=%d\n",f[u]);
        // f[u]=1;
        // for(int p=0;p<=alive;p++)
        //     f[u]=(f[u]+1ll*fac[alive]*ifac[alive-p]%mod*fac[alive-p+dead]%mod*ifac[son[u]]%mod*f[v])%mod;
        // printf("f=%d\n",f[u]);
        if(g[v])alive++;
        else dead++;
    }
    glo=(glo+f[u])%mod;
}
void ins(int u){
    // dfs2(rt);
    int sav=u;
    for(int v=u;u;v=u,u=dad[u]){
        if(!need[u]&&ok[u]&&!g[u])g[u]=1,need[dad[u]]--,DEAD[u]--,ALIVE[u]++;
        if(ok[u]){
            if(1)calc(u);
            else{
                glo=(glo+mod-f[u])%mod;
                f[u]=(f[u]+mod-tmps[v])%mod;
                int alive=ALIVE[u],dead=DEAD[u];
                if(g[v])alive--;else dead--;
                tmps[v]=1ll*inv[dead+1]*f[v]%mod;
                f[u]=(f[u]+tmps[v])%mod;
                glo=(glo+f[u])%mod;
            }
        }
    }
}

int deg[N];
namespace CHAIN{
int pa[N],len,op;
int L[N],R[N];
void dfs3(int u,int fa){
    pa[++len]=a[u];
    if(u==rt){
        dfs3(adj[u][op],u);
    }else{
        for(auto v:adj[u])
            if(v!=fa)
                dfs3(v,u);
    }
}
void MAIN(){
    len=0,op=0;
    dfs3(rt,0);

    printf("len=%d\n",len);
    rep(i,1,len)printf("%d ",pa[i]);
    puts("");

    static int stk[N],top;
    stk[top=0]=0;
    rep(i,1,len){
        while(top&&pa[stk[top]]>pa[i])top--;
        L[i]=stk[top],stk[++top]=i;
    }
    stk[top=0]=len+1;
    per(i,len,1){
        while(top&&pa[stk[top]]>=pa[i])top--;
        R[i]=stk[top],stk[++top]=i;
    }
    int ans=0;
    rep(i,1,len){
        printf("L[%d]=%d,R[%d]=%d\n",i,L[i],i,R[i]);
        ans=(ans+1ll*pa[i]*(i-L[i])%mod*(R[i]-i))%mod;
    }
    printf("cur ans=%d\n",ans);

    len=0,op=1;
    dfs3(rt,0);

    printf("len=%d\n",len);
    rep(i,1,len)printf("%d ",pa[i]);
    puts("");

    stk[top=0]=0;
    rep(i,1,len){
        while(top&&pa[stk[top]]>pa[i])top--;
        L[i]=stk[top],stk[++top]=i;
    }
    stk[top=0]=len+1;
    per(i,len,1){
        while(top&&pa[stk[top]]>=pa[i])top--;
        R[i]=stk[top],stk[++top]=i;
    }
    ans=0;
    rep(i,1,len){
        printf("L[%d]=%d,R[%d]=%d\n",i,L[i],i,R[i]);
        ans=(ans+1ll*pa[i]*(i-L[i])%mod*(R[i]-i))%mod;
    }

    printf("%d\n",ans);
}
}

void solve(){
    glo=0;
    n=read(),rt=read();
    rep(i,1,n)adj[i].clear(),need[i]=son[i]=ok[i]=g[i]=f[i]=ALIVE[i]=DEAD[i]=deg[i]=tmps[i]=0;
    rep(i,1,n)a[i]=read();
    rep(i,1,n-1){
        int u=read(),v=read();
        adj[u].pb(v),adj[v].pb(u);
        deg[u]++,deg[v]++;
    }

    // bool oks=1;
    // rep(i,1,n)oks&=deg[i]<=2;
    // if(oks){
    //     CHAIN::MAIN();
    //     return;
    // }

    dad[rt]=0;
    dfs1(rt,0);
    iota(id+1,id+n+1,1);
    sort(id+1,id+n+1,[&](int x,int y){
        return a[x]>a[y];
    });
    id[n+1]=0;
    int ans=0;
    for(int i=1;i<=n;i++){
        int j=i;
        while(j<n&&a[id[j]]==a[id[j+1]])++j;
        rep(k,i,j){
            ok[id[k]]=1;
            ins(id[k]);
        }
        // rep(k,i,j){
        //     ins(id[k]);
        // }
        // printf("When a=%d\n",a[id[i]]);
        // rep(i,1,n)printf("   f[%d]=%d\n",i,f[i]);
        i=j;
        // int res=0;
        // for(int k=1;k<=n;k++)res=(res+f[k])%mod;
        ans=(ans+1ll*(a[id[i]]-a[id[i+1]])*glo)%mod;
    }
    printf("%d\n",ans);
}

int main(){
    init(N-5);
    // for(int alive=0;alive<=4;alive++){
    //     for(int dead=0;dead<=4;dead++){
    //         int tmp=0;
    //         for(int p=0;p<=alive;p++)
    //             tmp=(tmp+1ll*fac[alive]*ifac[alive-p]%mod*fac[alive-p+dead]%mod)%mod;
    //         printf("%d %d: %d\n",alive,dead,tmp);
    //     }
    // }
    freopen("dfs.in","r",stdin);
    freopen("dfs.out","w",stdout);
    int T=read();
    while(T--)solve();
    return 0;
}

/*
1
5 1
3 2 1 4 5
1 2
2 3
1 4
4 5
*/