#include <bits/stdc++.h>
using namespace std;
const int N = 400005,P = 998244353;
using ll = long long;
int n,rt,ff[N],cnt[N],a[N];bool vis[N];vector<int> v[N];
ll qpow(ll a,ll b)
{
    ll ret = 1;
    while (b)
    {
        if (b & 1) ret = ret * a % P;
        a = a * a % P;b >>= 1;
    }
    return ret;
}
ll val[N],ans;
void dfs(int pos,int fa)
{
    ff[pos] = fa;for (auto &i : v[pos]) if (i != fa) dfs(i,pos);
}
void settag(int pos)
{
    if (vis[pos]) return;
    vis[pos] = 1;++cnt[ff[pos]];settag(ff[pos]);
}
int c[N];int idx;
void dfs0(int pos,int fa) {c[++idx] = a[pos];for (auto &i : v[pos]) if (i != fa) dfs0(i,pos);}
int main ()
{
    freopen("dfs.in","r",stdin);
    freopen("dfs.out","w",stdout);
    ios::sync_with_stdio(false);
    int T;cin >> T;
    while (T--)
    {
        cin >> n >> rt;ans = 0;for (int i = 1;i <= n;i++) cin >> a[i],v[i].clear();
        fill(cnt + 1,cnt + n + 1,0);fill(vis + 1,vis + n + 1,0);
        int t1,t2;for (int i = 1;i < n;i++) {cin >> t1 >> t2;v[t1].push_back(t2);v[t2].push_back(t1);}
        dfs(rt,0);vector<pair<int,int>> num;for (int i = 1;i <= n;i++) num.push_back({a[i],i});
        sort(num.begin(),num.end());
        if (int(v[rt].size()) == n - 1)
        {
            ll ans = 0;for (int i = 1;i <= n;i++) ans += a[i];
            ans %= P;
            vector<pair<int,int>> num;for (int i = 1;i <= n;i++) if (i != rt) num.push_back({min(a[i],a[rt]),i});
            sort(num.begin(),num.end());
            int cnt = n - 1,p = 0;
            for (auto &i : num)
            {
                --cnt;++p;
                ans = (ans + i.first * cnt % P * qpow(p + 1,P - 2) % P * qpow(p,P - 2)) % P;
                ans = (ans + i.first * qpow(p,P - 2)) % P;
            }
            cout << ans % P << '\n';
            continue;
        }
        int mxdeg = 0;for (int i = 1;i <= n;i++) mxdeg = max(mxdeg,int(v[i].size()));
        if (mxdeg == 2)
        {
            idx = 0;dfs0(rt,0);static int l[N],r[N],stk[N];
            int top = 0;stk[top] = 0;
            for (int i = 1;i <= n;i++)
            {
                while (top && c[stk[top]] >= c[i]) --top;
                l[i] = stk[top] + 1;stk[++top] = i;
            }
            top = 0;stk[top] = n + 1;
            for (int i = n;i > 0;--i)
            {
                while (top && c[stk[top]] > c[i]) --top;
                r[i] = stk[top] - 1;stk[++top] = i;
            }
            ll ans = 0;for (int i = 1;i <= n;i++) ans = (ans + ll(r[i] - i + 1) * (i - l[i] + 1) % P * c[i]) % P;
            cout << ans << '\n';continue;
        }
        cout << ans << '\n';
    }
    return 0;
}