#include <bits/stdc++.h>

#include "ModInt.hpp"

using MInt = Oct::ModInt<int, 998244353, long long>;

const int N = 400005;
const int M = 1 << 20;

int T, n, Root, Clock;
int a[N], Rk[N], Deep[N], Son[N], Dfn[N], Size[N], SonNum[N], NowSonNum[N], Used[N], FindF[N];
int rDfn[N], Fa[N], DEnd[N], Maxed[N];
int Min[M], Sub[M];

MInt Dp[N], Sum1[N], Sum2[N], DpNow[N], DpNext[N];

std::vector<int> To[N];

inline void Dfs1(int u, int f) {
    Size[u] = 1, Deep[u] = Deep[Fa[u] = f] + 1;
    std::vector<int> Sons;
    for (auto v : To[u]) {
        if (v == f) continue;
        Sons.push_back(v);
        Dfs1(v, u);
        Size[u] += Size[v];
        if (Size[v] > Size[Son[u]]) Son[u] = v;
    }
    To[u] = Sons, SNu[u] = Sons.size();
}

inline void Dfs2(int u, int t) {
    rDfn[Dfn[u] = ++Clock] = u, Top[u] = t;
    if (Son[u]) Dfs2(Son[u], t);
    for (auto v : To[u]) {
        if (v == Son[u]) continue;
        Dfs2(v, v);
    }
    DEnd[u] = Clock;
}

#define FAdd(F, a) \
    inline void F(int x, MInt v) { \
        while (x <= n) a[x] += v, x += x & -x; \
    }
FAdd(FAdd1, Sum1)
FAdd(FAdd2, Sum2)
#undef FAdd

#define FSum(F, a) \
    inline MInt F(int x) { \
        MInt Ret(0); \
        while (x) Ret += a[x], x &= x - 1; \
    }
FSum(FSum1, Sum1)
FSum(FSum2, Sum2)
#undef FSum

inline void PushDown(int u) {
    if (!Sub[u]) return;
    Min[u << 1] -= Sub[u], Min[u << 1 | 1] -= Sub[u];
    Sub[u << 1] += Sub[u], Sub[u << 1 | 1] += Sub[u];
    Sub[u] = 0;
}

inline void PushUp(int u) { Min[u] = std::min(Min[u << 1], Min[u << 1 | 1]); }

inline void Build(int u, int l, int r) {
    if (l == r) return void((Min[u] = Size[u], Sub[u] = 0));
    int Mid = l + r >> 1;
    Build(u << 1, l, Mid), Build(u << 1, Mid + 1, r);
    PushUp(u);
}

inline void SegSub(int u, int l, int r, int x, int y, int v) {
    if (l >= x && r <= y) return void(Min[u] -= v, Sub[u] += v);
    PushDown(u);
    int Mid = l + r >> 1;
    if (x <= Mid) SegSub(u << 1, l, Mid, x, y, v);
    if (y > Mid) SegSub(u << 1 | 1, Mid + 1, r, x, y, v);
    PushUp(u);
}

inline void FindZero(int u, int l, int r, std::function<void(int)> Func) {
    if (Min[u]) return;
    if (l == r) return Func(l);
    int Mid = l + r >> 1;
    FindZero(u << 1, l, Mid, Func), FindZero(u << 1 | 1, Mid + 1, r, Func);
}

inline void Find(int u) { return FindF[u] == u ? u : (FindF[u] = Find(FindF[u])); }

inline void Modify(int u, MInt v) { FAdd1(u, v - Dp[u]), FAdd2(u, (v - Dp[u]) * Deep[u]), Dp[u] = v; }

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    std::cin >> T;
    while (T--) {
        std::cin >> n >> Root;
        for (int i = 1; i <= n; ++i) std::cin >> a[i], Rk[i] = i, FindF[i] = i;
        for (int i = 1, u, v; i < n; ++i) {
            std::cin >> u >> v;
            To[u].push_back(v);
            To[v].push_back(u);
        }
        Dfs1(Root, Root), Dfs2(Root, Root);
        Build(1, 1, n);
        MInt Ans = 0;
        std::sort(Rk + 1, Rk + n + 1, [](int x, int y) { return a[x] < a[y]; });
        for (auto u : Rk) {
            int tu = u, TT = Find(u);
            while (Deep[Top[tu]] <= Deep[TT]) {
                if (u == Top[tu]) {
                    Ans += (Deep[u] - Deep[TT]) * (Maxed[u] ? DpNow : DpNext)[Fa[u]];
                    u = Fa[u];
                } else {
                    Ans += FSum2(Dfn[u] - 1) - FSum2(Dfn[Top[u]]);
                    Ans -= (FSum1(Dfn[u] - 1) - FSum1(Dfn[u] - 1)) * Deep[TT];
                    u = Fa[Top[u]];
                }
            }
            SegSub(1, 1, n, Dfn[u], DEnd[u], 1);
            FindZero(1, 1, n, std::function<void(int)>)
        }
    }
    return 0;
}