#include <bits/stdc++.h>
using namespace std;
const int N = 300 * 1000 + 13;
const int LOGN = 19;
const int MOD = 998244353;
int add(int a, int b) {
  a += b;
  if (a >= MOD) a -= MOD;
  if (a < 0) a += MOD;
  return a;
}
int mul(int a, int b) { return a * 1ll * b % MOD; }
int a[N];
vector<int> g[N];
int up[N][LOGN];
int d[N];
void init(int v, int p = -1) {
  for (auto u : g[v])
    if (u != p) {
      up[u][0] = v;
      for (int i = 1; i < LOGN; ++i) up[u][i] = up[up[u][i - 1]][i - 1];
      d[u] = d[v] + 1;
      init(u, v);
    }
}
int lca(int v, int u) {
  if (d[v] < d[u]) swap(v, u);
  for (int i = LOGN - 1; i >= 0; --i)
    if (d[up[v][i]] >= d[u]) v = up[v][i];
  if (v == u) return v;
  for (int i = LOGN - 1; i >= 0; --i)
    if (up[v][i] != up[u][i]) {
      v = up[v][i];
      u = up[u][i];
    }
  return up[v][0];
}
int l[N];
int dp[N][2];
int dfs(int v, int p = -1) {
  vector<pair<int, int>> cur;
  for (auto u : g[v])
    if (u != p) {
      int c = dfs(u, v);
      if (c != 0) cur.push_back(make_pair(c, u));
    }
  vector<int> vals;
  for (int i = 0; i < int(cur.size()); i++)
    if (cur[i].first > 0) vals.push_back(cur[i].first);
  sort(vals.begin(), vals.end());
  vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
  if (int(vals.size()) > 1) {
    puts("0");
    exit(0);
  }
  if (a[v] != 0 && !vals.empty() && vals[0] != a[v]) {
    puts("0");
    exit(0);
  }
  if (a[v] == 0 && cur.empty()) return 0;
  if (a[v] == 0 && vals.empty()) {
    vector<int> pr(1, 1), su(1, 1);
    for (int i = 0; i < int(cur.size()); i++)
      pr.push_back(
          mul(pr.back(), add(dp[cur[i].second][0], dp[cur[i].second][1])));
    for (int i = int(cur.size()) - 1; i >= 0; --i)
      su.push_back(
          mul(su.back(), add(dp[cur[i].second][0], dp[cur[i].second][1])));
    reverse(su.begin(), su.end());
    dp[v][1] = 0;
    for (int i = 0; i < int(cur.size()); i++)
      dp[v][1] =
          add(dp[v][1], mul(mul(pr[i], su[i + 1]), dp[cur[i].second][1]));
    dp[v][0] = add(dp[v][0], pr[cur.size()]);
    return -1;
  }
  dp[v][1] = 1;
  for (auto it : cur) {
    if (it.first == -1)
      dp[v][1] = mul(dp[v][1], add(dp[it.second][0], dp[it.second][1]));
    else
      dp[v][1] = mul(dp[v][1], dp[it.second][1]);
  }
  if (a[v] == 0) return vals[0];
  return (l[a[v]] == v ? -1 : a[v]);
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < int(n); i++) scanf("%d", &a[i]);
  for (int i = 0; i < int(n - 1); i++) {
    int v, u;
    scanf("%d%d", &v, &u);
    --v, --u;
    g[v].push_back(u);
    g[u].push_back(v);
  }
  init(0);
  memset(l, -1, sizeof(l));
  for (int i = 0; i < int(n); i++)
    if (a[i] != 0) l[a[i]] = (l[a[i]] == -1 ? i : lca(l[a[i]], i));
  for (int i = 1; i <= k; ++i) {
    if (a[l[i]] != 0 && a[l[i]] != i) {
      puts("0");
      exit(0);
    }
    a[l[i]] = i;
  }
  dfs(0);
  printf("%d\n", dp[0][1]);
  return 0;
}
