#include <bits/stdc++.h>
using namespace std;
#define R(i, a, b) for (int i = (a); i < (b); ++i)
#define L(i, a, b) for (int i = (b) - 1; i >= (a); --i)
using i64 = long long;
constexpr int M = 998244353;
struct mint {
  int x;
  const int operator()() {
    return x;
  }
  mint(int _x = 0) : x(_x) {}
};
mint operator+(const mint &a, const mint &b) {
  const int x = a.x + b.x;
  return x < M ? x : x - M;
}
mint operator-(const mint &a, const mint &b) {
  const int x = a.x - b.x;
  return x < 0 ? x + M : x;
}
mint operator*(const mint &a, const mint &b) {
  return i64(a.x) * b.x % M;
}
mint &operator+=(mint &a, const mint &b) {
  a = a + b; return a;
}
mint &operator*=(mint &a, const mint &b) {
  a = a * b; return a;
}
mint pw(mint a, int x = M - 2) {
  mint res = 1;
  for (; x; x >>= 1, a *= a) if (x & 1) res *= a;
  return res;
}
constexpr int xn = 400007;
int n, rt, a[xn], w[xn], nw;
int d[xn], fa[xn], mn[xn], sz[xn];
mint f[xn], g[xn];
mint fac[xn], ifac[xn], inv[xn];
vector<int> G[xn];
mint binom(int n, int m) {
  return 0 <= m and m <= n ? fac[n] * ifac[m] * ifac[n - m] : 0;
}
void dfs0(int u) {
  d[u] = G[u].size();
  for (const int &v : G[u]) {
    G[v].erase(find(G[v].begin(), G[v].end(), u));
    fa[v] = u; dfs0(v);
  }
}
void dfssz(int u) {
  mn[u] = a[u]; sz[u] = 1;
  for (const int &v : G[u]) {
    dfssz(v); mn[u] = min(mn[u], mn[v]); sz[u] += sz[v];
  }
}
void dfs1(int u, int x) {
  g[u] = a[u] >= x;
  for (const int &v : G[u]) {
    dfs1(v, x); g[u] += g[v] * f[v];
  }
}
mint solvew(int i) {
  // cerr << "solvew(" << i << ")\n";
  R(u, 0, n) {
    if (!d[u]) continue;
    if (a[u] < i) {
      for (const int &v : G[u]) f[v] = 0;
      continue;
    }
    int cnt = 0;
    for (const int &v : G[u]) if (mn[v] >= i) cnt += 1;
    mint s0 = 0, s1 = 0;
    R(i, 0, cnt + 1) s0 += fac[d[u] - 1 - i] * ifac[cnt - i];
    s0 *= ifac[d[u] - 1] * fac[cnt] * inv[d[u]];
    cnt -= 1;
    R(i, 0, cnt + 1) s1 += fac[d[u] - 1 - i] * ifac[cnt - i];
    s1 *= ifac[d[u] - 1] * fac[cnt] * inv[d[u]];
    // cerr << "u = " << u << " cnt = " << cnt << " s1 = " << s1() << '\n'; // @
    for (const int &v : G[u]) f[v] = mn[v] >= i ? s1 : s0;
  }
  // R(u, 0, n) cerr << "f[" << u << "] = " << f[u]() << '\n'; // @
  dfs1(rt, i);
  return accumulate(g, g + n, mint(0));
}
void solveTsub0() {
  nw = 0; R(u, 0, n) w[nw++] = a[u];
  sort(w, w + nw); nw = unique(w, w + nw) - w;
  R(u, 0, n) a[u] = lower_bound(w, w + nw, a[u]) - w;
  dfssz(rt);
  mint res = w[0] * accumulate(sz, sz + n, mint(0));
  R(i, 1, nw) res += (w[i] - w[i - 1]) * solvew(i);
  cout << res() << '\n';
}
void solveT1() {
  vector<int> A;
  for (int u = rt; d[u]; u = G[u][0]) A.push_back(a[u]);
  vector<int> s = {-1};
  mint res = 0, sum = 0;
  R(r, 0, n + 1) {
    res += sum;
    if (r == n) break;
    for (; ~s.back() and A[s.back()] >= A[r]; ) {
      int i = s.back(); s.pop_back();
      sum -= mint(i - s.back()) * A[i];
    }
    sum += mint(r - s.back()) * A[r];
    s.push_back(r);
  }
  cout << res() << '\n';
}
void solveT() {
  cin >> n >> rt; --rt;
  R(u, 0, n) cin >> a[u]; 
  R(i, 0, n - 1) {
    int u, v; cin >> u >> v; --u; --v;
    G[u].push_back(v); G[v].push_back(u);
  }
  fa[rt] = -1; dfs0(rt);
  if (n <= 5000) solveT0(); else solveT1();
  R(u, 0, n) G[u].clear();
}
int main() {
  freopen("dfs.in", "r", stdin); freopen("dfs.out", "w", stdout);
  ios::sync_with_stdio(false); cin.tie(0);
  fac[0] = ifac[0] = 1;
  R(i, 1, xn) {
    inv[i] = i == 1 ? 1 : (M - M / i) * inv[M % i];
    fac[i] = fac[i - 1] * i;
    ifac[i] = ifac[i - 1] * inv[i];
  }
  int nT; cin >> nT; R(T, 0, nT) solveT();
  return 0;
}