#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i) 
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define sz(a) ((int) (a).size())
#define vi vector < int > 
#define me(a, x) memset(a, x, sizeof(a)) 
#define ll long long
using namespace std;
const int N = 1e6 + 7, M = 1e7, mod = 998244353;

#define Add(x, y) (x + y >= mod ? x + y - mod : x + y)

int inv[N];

int n, root;
int a[N], arr[N], mn[N];

vi e[N];
int hd[N], tag[M], sum[M], cnt[M], ch[M][2], tot;

inline void mut(int x, int y) {
	if(x) tag[x] = (ll) tag[x] * y % mod, sum[x] = (ll) sum[x] * y % mod, cnt[x] = (ll) cnt[x] * y % mod;
}

inline void push(int x) {
	if(tag[x] != 1) mut(ch[x][0], tag[x]), mut(ch[x][1], tag[x]), tag[x] = 1;
}

inline void upd(int x) {
	sum[x] = Add(sum[ch[x][0]], sum[ch[x][1]]);
	cnt[x] = Add(cnt[ch[x][0]], cnt[ch[x][1]]);
} 

void add(int &x, int L, int R, int p, int w) {
	if(!x) x = ++tot, tag[x] = 1;
	if(L == R) return (sum[x] += (ll) arr[p] * w % mod) %= mod, cnt[x] = Add(cnt[x], w), void ();
	int mid = (L + R) >> 1; 
	push(x);
	p <= mid ? add(ch[x][0], L, mid, p, w) : add(ch[x][1], mid + 1, R, p, w);
	upd(x);
}

int merge(int x, int y) {
	if(!x || !y) return x | y;
	(sum[x] += sum[y]) %= mod, (cnt[x] += cnt[y]) %= mod;
	push(x), push(y);
	ch[x][0] = merge(ch[x][0], ch[y][0]);
	ch[x][1] = merge(ch[x][1], ch[y][1]);
	return x;
}

void mu(int x, int L, int R, int l, int r, int w) {
	if(!x) return ;
	if(l <= L && R <= r) return mut(x, w), void ();
	int mid = (L + R) >> 1;
	push(x);
	if(l <= mid) mu(ch[x][0], L, mid, l, r, w);
	if(r > mid) mu(ch[x][1], mid + 1, R, l, r, w);
	upd(x);
}

int query(int x, int L, int R, int l, int r) {
	if(!x) return 0;
	if(l <= L && R <= r) return cnt[x];
	push(x);
	int mid = (L + R) >> 1, ret = 0;
	if(l <= mid) ret = query(ch[x][0], L, mid, l, r);
	if(r > mid) ret += query(ch[x][1], mid + 1, R, l, r);
	return ret >= mod ? ret - mod : ret;
}

int Get(int o, int l, int r) {
	return l > r ? 0 : query(o, 1, n, l, r);
}

int son[N], v[N], goa[N], gob[N], all;
pair < int, int > slv(int l, int r) {
	if(l == r) {
//		L(i, 1, all) {
//			if(i != l) {
//				if(v[i] < n) {
//					if(i < l) (goa[i] += query(hd[son[l]], 1, n, v[i] + 1, n)) %= mod;
//					else (gob[i] += query(hd[son[l]], 1, n, v[i] + 1, n)) %= mod;
//				}
//			}
//		}
		int S = Get(hd[son[l]], v[l] + 1, v[l + 1]);
		if(v[l] < v[l + 1]) 
			mu(hd[son[l]], 1, n, v[l] + 1, v[l + 1], inv[l]); 
//		cnt = 0;
//		L(i, 1, all) {
//			if(i != l) cnt += 1;
//			if(v[i] < v[i + 1]) 
//				mu(hd[son[l]], 1, n, v[i] + 1, v[i + 1], inv[cnt + 1]);
//		}
		return make_pair(hd[son[l]], S);
	}
	int mid = (l + r) >> 1;
	auto xl = slv(l, mid), xr = slv(mid + 1, r);
	int ml = xl.first, mr = xr.first;
	int suml = xl.second, sumr = xr.second;
	int Tl = Get(ml, v[mid + 1] + 1, n);
	int Tr = Get(mr, v[r + 1] + 1, n);
	
	L(i, l, mid) (goa[i] += ((ll) Get(mr, v[i] + 1, v[mid + 1]) + sumr + Tr) % mod) %= mod;
	L(i, mid + 1, r) (gob[i] += Get(ml, v[i] + 1, n)) %= mod;
	
	(suml += Get(ml, v[mid + 1] + 1, v[r + 1])) %= mod;
	(sumr += Get(mr, v[l] + 1, v[mid + 1])) %= mod;
	
	L(i, l, mid) if(v[i] < v[i + 1]) mu(mr, 1, n, v[i] + 1, v[i + 1], inv[i + 1]); 
	L(i, mid + 1, r) if(v[i] < v[i + 1]) mu(ml, 1, n, v[i] + 1, v[i + 1], inv[i]); 
	return make_pair(merge(ml, mr), (suml + sumr) % mod);	
}

int getcle(int &x, int L, int R, int l, int r) {
	if(!x) return 0;
	if(l <= L && R <= r) {
		int ret = cnt[x];
		return sum[x] = cnt[x] = 0, x = 0, ret;
	}
	push(x);
	int mid = (L + R) >> 1, ret = 0;
	if(l <= mid) (ret += getcle(ch[x][0], L, mid, l, r)) %= mod;
	if(r > mid) (ret += getcle(ch[x][1], mid + 1, R, l, r)) %= mod;
	upd(x);
	return ret;
}

int ns;
void dfs(int x, int fa) {
	mn[x] = a[x];
	for(const int &v : e[x]) if(v != fa) dfs(v, x), mn[x] = min(mn[x], mn[v]);
	all = 0;
	for(const int &v : e[x]) if(v != fa) son[++all] = v;
	sort(son + 1, son + all + 1, [&] (int x, int y) {
		return mn[x] < mn[y];
	});
	L(i, 1, all) v[i] = mn[son[i]], goa[i] = gob[i] = 0;
//	L(i, 1, all) cout << v[i] << ",";
//	cout << '\n';
	v[all + 1] = n;
	hd[x] = all ? slv(1, all).first : 0;
	L(i, 1, all) add(hd[x], 1, n, v[i], ((ll) goa[i] * inv[i + 1] + (ll) gob[i] * inv[i - 1]) % mod * inv[i] % mod);
//	cout << x << " : " << cnt[hd[x]] << '\n'; 
	int lmao = getcle(hd[x], 1, n, a[x], n);
//	cout << "lmao = " << lmao << '\n';
	add(hd[x], 1, n, a[x], (1 + lmao) % mod);
	(ns += sum[hd[x]]) %= mod;
//	cout << x << " : " << cnt[hd[x]] << '\n';
}

void Main() {
	cin >> n >> root, ns = 0;
	L(i, 1, n) cin >> a[i], arr[i] = a[i];
	L(i, 1, n - 1) {
		int u, v;
		cin >> u >> v;
		e[u].emplace_back(v);
		e[v].emplace_back(u);
	}
	sort(arr + 1, arr + n + 1);
	L(i, 1, n) a[i] = lower_bound(arr + 1, arr + n + 1, a[i]) - arr;
	dfs(root, 0);
	cout << ns << '\n';
	L(i, 1, tot) sum[i] = cnt[i] = 0, tag[i] = 0, ch[i][0] = ch[i][1] = 0;
	L(i, 1, n) e[i].clear(), hd[i] = 0;
	tot = 0;
}

int main() {
//	return system("fc dfs.out dfs_ex2.ans"), 0;
	freopen("dfs.in", "r", stdin);
	freopen("dfs.out", "w", stdout);
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	inv[1] = 1;
	L(i, 2, N - 7) inv[i] = (ll) inv[mod % i] * (mod - mod / i) % mod;
	int T;
	cin >> T;
	while(T--) Main();
	return 0;
}
/*
1
4 1
1 3 2 3
1 2
1 3
1 4
*/
