#include <bits/stdc++.h>
// #define int long long
using namespace std;
inline int read(void) {
    int x=0,sgn=1; char c=getchar();
    while(c< '0'||'9'< c) {(c=='-'&&(sgn=0)); c=getchar();}
    while('0'<=c&&c<='9') {x=x*10+c-'0'; c=getchar();}
    return sgn ? x : -x;
}
void write(int x) {
    if(x<0) return putchar('-'),write(-x),void();
    if(x>9) write(x/10),x%=10;
    return putchar('0'+x),void();
}
const int mod=998244353;
int ksm(int x,int a) {
    if(a==0) return 1;
    if(a==1) return x;
    int tmp=ksm(x,a>>1);
    tmp=1ll*tmp*tmp%mod;
    (a&1) && (tmp=1ll*tmp*x%mod);
    return tmp;
}
int n,rot,lim;
long long Ans;
vector <int> E[10005];
int p[10005],val[10005],rnk[10005];
int ret[10005],siz[10005];
int sum[10005];
int inv[10005];
void dfs0(int x,int f) {
    siz[x]=1;
    for(int v : E[x]) {
        if(v==f) continue;
        dfs0(v,x);
        siz[x]+=siz[v];
    }
}
void dfs(int x,int f) {
    // cout<<"startdfs"<<x<<' '<<f<<endl;
    ret[x]=sum[x]=(val[x]>lim);
    int cnt0=0,cnt1=0;
    for(int v : E[x]) {
        if(v==f) continue;
        dfs(v,x);
        ret[x]&=ret[v];
        if(ret[v]) ++cnt1;
        else ++cnt0;
    }
    cnt1=inv[cnt0+1];
    cnt0=inv[cnt0];
    if(val[x]>lim) {
        for(int v : E[x]) {
            if(v==f) continue;
            if(ret[v]) sum[x]=(sum[x]+1ll*cnt1*sum[v])%mod;
            else if(val[v]>lim) sum[x]=(sum[x]+1ll*cnt0*sum[v])%mod;
        }
    }
}
inline void work() {
    n=read(); rot=read(); Ans=0; p[0]=0;
    for(int i=1; i<=n; ++i) rnk[i]=val[i]=read(),p[i]=0;
    for(int i=1,x,y; i<n; ++i) {
        x=read(); y=read();
        E[x].push_back(y);
        E[y].push_back(x);
    }
    dfs0(rot,0);
    // if(E[rot].size()==n-1) {
    //     return ;
    // }
    sort(rnk+1,rnk+n+1);
    int m=unique(rnk+1,rnk+n+1)-rnk-1;
    for(int i=1; i<=n; ++i) (p[0]+=siz[i])>=mod && (p[0]-=mod);
    // cout<<"end1\n";
    for(int i=1; i<m; ++i) {
        // cout<<"start"<<' '<<i<<' ';
        lim=rnk[i];
        // cout<<lim<<endl;
        dfs(rot,0);
        for(int u=1; u<=n; ++u) {
            (p[i]+=sum[u])>=mod && (p[i]-=mod);
            // cout<<sum[u]<<' ';
        }
        // cout<<endl;
    } 
    // cout<<"+++++++++"<<endl;
    for(int i=1; i<=m; ++i) {
        (Ans+=1ll*(p[i-1]-p[i])*rnk[i])%=mod;
        // cout<<p[i-1]<<' ';
    }
    // cout<<p[m]<<endl;
    write((Ans+mod)%mod); puts("");
    for(int i=1; i<=n; ++i) E[i].clear();
    // cout<<"+++++++++"<<endl;
    return ;
}
signed main() {
    freopen("dfs.in","r",stdin);
    // freopen("dfs_ex2.in","r",stdin);
    freopen("dfs.out","w",stdout);
    for(int i=1; i<=10000; ++i) inv[i]=ksm(i,998244351);
    int T=read();
    while(T--) work();
    return 0;
}