#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
inline int read() {
  int X = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') X = (X << 3) + (X << 1) + c - '0', c = getchar();
  return X * w;
}
void exgcd(long long a, long long b, long long& d, long long& x, long long& y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    exgcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}
long long inv(long long a, long long p) {
  long long d, x, y;
  exgcd(a, p, d, x, y);
  return d == 1 ? (x + p) % p : -1;
}
const int N = 3e5 + 5;
vector<int> g[N], c[N];
int par[N][22], dep[N];
void dfs(int v, int fa) {
  dep[v] = dep[fa] + 1;
  par[v][0] = fa;
  for (int i = 1; (1 << i) <= dep[fa]; i++)
    par[v][i] = par[par[v][i - 1]][i - 1];
  for (auto u : g[v]) {
    if (u == fa) continue;
    dfs(u, v);
  }
}
int lca(int x, int y) {
  if (dep[x] > dep[y]) swap(x, y);
  for (int i = 20; i >= 0; i--)
    if (dep[x] <= dep[y] - (1 << i)) y = par[y][i];
  if (x == y) return x;
  for (int i = 20; i >= 0; i--) {
    if (par[x][i] == par[y][i]) continue;
    x = par[x][i], y = par[y][i];
  }
  return par[x][0];
}
int n, k, a[N];
void slove() {
  for (int i = 1; i <= k; i++) {
    int lca_ = c[i][0];
    for (auto u : c[i]) lca_ = lca(lca_, u);
    for (auto u : c[i]) {
      if (u == lca_) continue;
      int v = u;
      while (v != lca_) {
        v = par[v][0];
        if (a[v] != 0 && a[v] != i) {
          cout << 0 << endl;
          exit(0);
        }
        if (a[v] == i) break;
        a[v] = i;
      }
    }
  }
}
long long dp[N][2];
void dfs2(int v, int fa) {
  long long tmp = 1;
  for (auto u : g[v]) {
    if (u == fa) continue;
    dfs2(u, v);
    tmp = tmp * (dp[u][0] + dp[u][1]) % mod;
  }
  if (a[v] == 0) {
    dp[v][0] = tmp;
    dp[v][1] = 0;
    for (auto u : g[v]) {
      if (u == fa) continue;
      long long x = tmp * inv(dp[u][0] + dp[u][1], mod) % mod;
      x = x * dp[u][1] % mod;
      dp[v][1] = (dp[v][1] + x) % mod;
    }
  } else {
    dp[v][0] = 0;
    dp[v][1] = tmp;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    c[a[i]].push_back(i);
  }
  int x, y;
  for (int i = 1; i < n; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(1, 0);
  slove();
  dfs2(1, 0);
  cout << dp[1][1];
}
