#include <bits/stdc++.h>

int init_io = []{
#ifndef CM_DEBUG
	freopen("dfs.in", "r", stdin);
	freopen("dfs.out", "w", stdout);
#endif
	return 0;
}();

const int MOD = 998244353;
const int inv2 = (MOD + 1) / 2;
const int N = 400005;
std::vector<int> e[N];
std::vector<int> ws;
int w[N], n, r;

int ans[N];
bool prob[N];
void dfs_calc(int u, int f, int cw)
{
	for (int v : e[u]) if (v != f)
		dfs_calc(v, u, cw);
	if (w[u] < cw)
	{
		ans[u] = 0;
		prob[u] = false;
	}
	else
	{
		ans[u] = 1;
		prob[u] = true;

		int sw = 1;
		for (int v : e[u]) if (v != f)
			if (!prob[v])
				sw = (int64_t)inv2 * sw % MOD;

		for (int v : e[u]) if (v != f)
		{
			int cur_sw = prob[v] ? sw : (sw + sw) % MOD;
			ans[u] = (ans[u] + (int64_t)cur_sw * ans[v]) % MOD;
			prob[u] &= prob[v];
		}
	}
}

void brute()
{
	int last_w = 0;
	int tot = 0;
	for (int cw : ws)
	{
		dfs_calc(r, 0, cw);
		int cur_tot = 0;
		for (int i = 1; i <= n; i++)
		{
			// printf("cw = %d, ans[%d] = %d\n", cw, i, ans[i]);
			cur_tot = (cur_tot + ans[i]) % MOD;
		}
		tot = (tot + (int64_t)(cw - last_w) * cur_tot) % MOD;
		last_w = cw;
	}
	printf("%d\n", tot);
}

bool is_chain()
{
	for (int i = 1; i <= n; i++)
		if (e[i].size() > 2)
			return false;
	if (e[r].size() >= 2)
		return false;
	return true;
}

std::vector<int> ww;
void dfs_get_ww(int u, int f)
{
	ww.push_back(w[u]);
	for (int v : e[u]) if (v != f) dfs_get_ww(v, u);
}
void solve_chain()
{
	ww.clear();
	dfs_get_ww(r, 0);

	static int lb[N], rb[N];
	std::vector<int> st;
	for (int i = 0; i < n; i++)
	{
		while (!st.empty() && ww[st.back()] > ww[i])
		{
			rb[st.back()] = i;
			st.pop_back();
		}
		if (st.empty()) lb[i] = -1;
		else lb[i] = st.back();
		st.push_back(i);
	}
	while (!st.empty())
	{
		rb[st.back()] = n;
		st.pop_back();
	}

	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		// printf("lb[%d] = %d, rb[%d] = %d\n", i, lb[i], i, rb[i]);
		ans = (ans + (int64_t)(i - lb[i]) * (rb[i] - i) % MOD * ww[i]) % MOD;
	}
	printf("%d\n", ans);
}

void rmain()
{
	scanf("%d%d", &n, &r);
	ws.clear();
	for (int i = 1; i <= n; i++)
	{
		e[i].clear();
		scanf("%d", &w[i]);
		ws.push_back(w[i]);
	}
	for (int i = 0; i < n - 1; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		e[u].push_back(v);
		e[v].push_back(u);
	}
	std::sort(ws.begin(), ws.end());
	// ws.erase(std::unique(ws.begin(), ws.end()), ws.end());

	if (is_chain())
		solve_chain();
	else
		brute();
}

int main()
{
	int t;
	scanf("%d", &t);
	for (int i = 0; i < t; i++)
		rmain();
	return 0;
}
