#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long mod = 998244353;
const long long linf = 0x3f3f3f3f3f3f3f3f;
const int N = 300100;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int a[N];
vector<int> g[N];
int pai[N];
int nivel[N];
int comp[N];
vector<int> h[N];
vector<int> cur;
long long dp[N][2];
bool ok[N];
void dfs(int x, int p = -1, int u = 0) {
  pai[x] = p;
  nivel[x] = u;
  for (auto v : g[x]) {
    if (v == p) continue;
    dfs(v, x, u + 1);
  }
}
int lca(int x, int y) {
  while (x != y) {
    if (nivel[x] > nivel[y]) {
      cur.push_back(x);
      x = pai[x];
    } else {
      cur.push_back(y);
      if (comp[y]) return x;
      y = pai[y];
    }
  }
  cur.push_back(x);
  return x;
}
void calc(int u) {
  if (comp[u]) ok[u] = 1;
  vector<int> cur;
  for (auto v : g[u]) {
    if (v == pai[u]) continue;
    calc(v);
    ok[u] |= ok[v];
    if (ok[v]) cur.push_back(v);
  }
  if (comp[u]) {
    dp[u][1] = 1;
    for (auto v : cur) {
      dp[u][1] = (dp[u][1] * (dp[v][0] + dp[v][1])) % mod;
    }
  } else {
    dp[u][0] = 1;
    int n = int(cur.size());
    for (auto v : cur) {
      dp[u][0] = (dp[u][0] * (dp[v][0] + dp[v][1])) % mod;
    }
    vector<long long> pref(n + 2), suf(n + 2);
    pref[0] = 1;
    suf[n + 1] = 1;
    for (int i = 1; i <= n; i++) {
      int v = cur[i - 1];
      int w = cur[n - i];
      pref[i] = (pref[i - 1] * (dp[v][0] + dp[v][1])) % mod;
      suf[n + 1 - i] = (suf[n + 2 - i] * (dp[w][0] + dp[w][1])) % mod;
    }
    for (int i = 1; i <= n; i++) {
      int v = cur[i - 1];
      dp[u][1] =
          (dp[u][1] + dp[v][1] * (pref[i - 1] * suf[i + 1] % mod) % mod) % mod;
    }
  }
}
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i], h[a[i]].push_back(i);
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(1);
  for (int i = 1; i <= k; i++) {
    int v = h[i][0];
    if (comp[v]) {
      cout << "0\n";
      return;
    }
    comp[v] = i;
    for (int j = 1; j < int(h[i].size()); j++) {
      cur.clear();
      v = lca(v, h[i][j]);
      for (auto p : cur) {
        if (comp[p] && comp[p] != i) {
          cout << "0\n";
          return;
        }
        comp[p] = i;
      }
    }
  }
  calc(1);
  cout << dp[1][1] << "\n";
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  ;
  int t_ = 1;
  while (t_--) solve();
  return 0;
}
