#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define setIO(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
#define m_p make_pair
#define sz(x) (int)(x.size())
#define out(x) cerr << #x << " = " << x << " "
#define outln(x) cerr << #x << " = " << x << endl
#define outarr(x, l, r) {cerr << #x"[" << l << " ~ " << r << "] = "; for (int _ = l; _ <= r; ++_) cerr << x[_] << " "; cerr << endl;}

const int mod = 998244353, inv2 = 499122177;
struct Z {
	int x;
	Z() {}
	Z(int _):x(_) {}
};
Z operator + (const Z &a, const Z &b) {const int o = a.x + b.x; return o >= mod ? o - mod : o;}
Z operator - (const Z &a, const Z &b) {const int o = a.x - b.x + mod; return o >= mod ? o - mod : o;}
Z operator * (const Z &a, const Z &b) {const int o = (int)(1LL * a.x * b.x % mod); return o;}
Z& operator += (Z &a, const Z &b) {return a = a + b;}
Z& operator -= (Z &a, const Z &b) {return a = a - b;}
Z& operator *= (Z &a, const Z &b) {return a = a * b;}
template <class T> Z qpow(Z a, T b) {
	Z ans = 1;
	while (b) {
		if (b & 1) ans = ans * a;
		a = a * a;
		b >>= 1;
	}
	return ans;
}
Z operator / (const Z &a, const Z &b) {return a * qpow(b, mod - 2);}
Z& operator /= (Z &a, const Z &b) {return a = a / b;}
ostream& operator << (ostream &out, const Z &a) {out << a.x; return out;}

const int N = 400005;
int n, rt;
int a[N], fa[N];
array<int, 2> b[N];
int dfn[N], tim = 0, Out[N], lis[N];
vector<int> G[N], H[N];
bool ban[N], exi[N];
int sum[N];
Z inv[N];

bool in_sub(int x, int y) {
	return dfn[y] <= dfn[x] && dfn[x] <= Out[y];
}

void dfs(int x, int f) {
	fa[x] = f; dfn[x] = ++tim; lis[tim] = x;
	for (int to : G[x]) if (to != f) {
		H[x].push_back(to);
		dfs(to, x);
	}
	Out[x] = tim;
}

void init() {
	scanf("%d%d", &n, &rt); tim = 0;
	for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), G[i].clear(), H[i].clear();
	for (int i = 1, u, v; i < n; ++i) {
		scanf("%d%d", &u, &v);
		G[u].emplace_back(v);
		G[v].emplace_back(u);
	}
	for (int i = 1; i <= n; ++i) b[i] = {a[i], i};
	for (int i = 1; i <= n + 1; ++i) inv[i] = Z(1) / i;
	sort(b + 1, b + n + 1);
	dfs(rt, 0);
}

bool check_chain() {
	for (int i = 1; i <= n; ++i) if (sz(H[i]) > 1) return 0;
	return 1;
}

namespace chain {
	int suf[N], pre[N], c[N], sta[N], top = 0;
	void main() {
		int m = 0;
		for (int i = rt; ; i = H[i][0]) {
			c[++m] = a[i];
			if (H[i].empty()) break;
		}
		top = 0;
		for (int i = 1; i <= m; ++i) {
			while (top && c[sta[top]] > c[i]) --top;
			pre[i] = top ? sta[top] : 0;
			sta[++top] = i;
		}
		top = 0;
		for (int i = m; i >= 1; --i) {
			while (top && c[sta[top]] >= c[i]) --top;
			suf[i] = top ? sta[top] : m + 1;
			sta[++top] = i;
		}
		Z ans = 0;
		for (int i = 1; i <= n; ++i) {
			ans += c[i] * (suf[i] - i) * (i - pre[i]);
		}
		printf("%d\n", ans.x);
	}
}

namespace solver1 {
	Z dp[N][2];
	void dfs3(int x) {
		dp[x][exi[x]] = 1;
		for (int to : H[x]) {
			dfs3(to);
			dp[x][exi[x]] += dp[to][1] * inv[sum[x]];
			dp[x][exi[x]] += dp[to][0] * inv[sum[x] + 1];
		}
		if (ban[x]) dp[x][0] = dp[x][1] = 0;
	}

	void update(int x) {
		ban[x] = 1;
		for (int i = x; i; i = fa[i]) {
			if (!exi[i]) exi[i] = 1, sum[fa[i]] += 1;
		}
	}

	Z calc() {
		for (int i = 1; i <= n; ++i) {
			dp[i][0] = 0;
			dp[i][1] = 0;
		}
		dfs3(rt);
		Z ans = 0;
		for (int i = 1; i <= n; ++i) {
			ans += dp[i][0];
			ans += dp[i][1];
		}
		return ans;
	}

	void main() { 
		Z ans = 0; int pre = 0;
		for (int i = 1; i <= n; ++i) ban[i] = exi[i] = 0;
		for (int i = 1; i <= n; ++i) sum[i] = 0;
		for (int z = 1; z <= n; ++z) {
			int pnt = b[z][1], val = b[z][0];
			Z tot = calc();
			update(pnt);
			ans += tot * (val - pre);
			pre = val;
		}
		printf("%d\n", ans.x);
	}
}

void solve() {
	if (n <= 5000) {
		solver1::main();
		return;
	}
	if (check_chain()) {
		chain::main();
		return;
	}
	solver1::main();
}

int main() {
	setIO("dfs");
	int t; scanf("%d", &t);
	while (t--) {
		init();
		solve();
	}
	return 0;
}
