#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 998244353;
const long long BIG = 1446803456761533460LL;
const int Big = 336860180;
const long long int INF = LONG_LONG_MAX;
const long long int adj4[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
const long long int adj8[8][2] = {{0, 1},   {0, -1}, {1, 0},  {-1, 0},
                                  {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
long long int gcd(long long int a, long long int b) {
  return b == 0 ? a : gcd(b, a % b);
}
long long int powMod(long long int a, long long int b) {
  long long int n = 1;
  long long int p = a;
  while (b > 0) {
    if (b % 2 == 1) {
      n *= p;
      n %= MOD;
    }
    p *= p;
    p %= MOD;
    b /= 2;
  }
  return n;
}
long long int modularInverse(long long int a) { return powMod(a, MOD - 2); }
stringstream sss;
const long long int maxn = 300010;
const long long int maxnlg = 20;
long long int n, k;
vector<long long int> g[maxn];
vector<long long int> cols[maxn];
long long int st[maxn], ft[maxn];
long long int H[maxn], pars[maxnlg][maxn];
long long int val[maxn];
long long int rt[maxn];
vector<long long int> gg[maxn];
long long int colrt[maxn];
long long int blk[maxn];
long long int dp[maxn][2];
long long int plt[maxn];
long long int tim = 0;
void dfs_lca(long long int x, long long int par) {
  st[x] = tim++;
  pars[0][x] = par;
  for (long long int i = (0); i < (maxnlg - 1); ++i) {
    pars[i + 1][x] = pars[i][pars[i][x]];
  }
  for (auto y : g[x]) {
    if (y != par) {
      H[y] = H[x] + 1;
      dfs_lca(y, x);
    }
  }
  ft[x] = tim;
}
long long int LCA(long long int a, long long int b) {
  if (st[a] > st[b]) swap(a, b);
  if (ft[a] >= ft[b]) return a;
  for (long long int i = (maxnlg)-1; i >= (0); --i) {
    if (ft[pars[i][a]] < ft[b]) {
      a = pars[i][a];
    }
  }
  return pars[0][a];
}
long long int root(long long int x) {
  return rt[x] == x ? x : rt[x] = root(rt[x]);
}
void dfs_merge(long long int x, long long int par) {
  for (auto y : g[x]) {
    if (y == par) continue;
    dfs_merge(y, x);
    val[x] += val[y];
  }
  if (val[x]) {
    rt[root(x)] = root(par);
  }
}
void dfs_dp(long long int x, long long int par) {
  for (auto y : gg[x]) {
    if (y == par) continue;
    dfs_dp(y, x);
  }
  long long int nn = gg[x].size();
  plt[0] = 1;
  for (long long int i = (0); i < (nn); ++i) {
    long long int v = gg[x][i];
    if (v == par) {
      plt[i + 1] = plt[i];
    } else {
      plt[i + 1] = plt[i] * (dp[v][0] + dp[v][1]) % MOD;
    }
  }
  if (blk[x]) {
    dp[x][0] = 0;
    dp[x][1] = plt[nn];
  } else {
    dp[x][0] = plt[nn];
    long long int prt = 1;
    for (long long int i = (nn)-1; i >= (0); --i) {
      long long int v = gg[x][i];
      if (v != par) {
        dp[x][1] = (dp[x][1] + (plt[i] * prt % MOD * dp[v][1])) % MOD;
        prt = (prt * (dp[v][0] + dp[v][1])) % MOD;
      }
    }
  }
}
void MAIN() {
  cin >> n >> k;
  for (long long int i = (0); i < (n); ++i) {
    long long int x;
    cin >> x;
    if (x != 0) {
      --x;
      cols[x].push_back(i);
    }
  }
  for (long long int i = (0); i < (n - 1); ++i) {
    long long int a, b;
    cin >> a >> b;
    --a, --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs_lca(0, 0);
  for (long long int i = (0); i < (k); ++i) {
    long long int a = cols[i][0];
    long long int b = cols[i][0];
    for (auto u : cols[i]) {
      val[u]++;
      if (st[u] < st[a]) a = u;
      if (st[u] > st[b]) b = u;
    }
    long long int c = LCA(a, b);
    val[c] -= cols[i].size();
  }
  iota(rt, rt + n, 0);
  dfs_merge(0, 0);
  for (long long int i = (0); i < (k); ++i) {
    for (auto u : cols[i]) {
      long long int colr = root(u);
      if (colrt[colr] && colrt[colr] != i + 1) {
        cout << (0) << '\n';
        return;
      }
      colrt[colr] = i + 1;
      blk[colr] = 1;
    }
  }
  for (long long int x = (0); x < (n); ++x) {
    for (auto y : g[x]) {
      long long int rx = root(x);
      long long int ry = root(y);
      if (rx != ry) {
        gg[rx].push_back(ry);
      }
    }
  }
  dfs_dp(root(0), -1);
  cout << (dp[root(0)][1]) << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  sss << R"(
6 2
0 2 0 0 0 1
1 2
2 3
3 4
4 5
4 6
7 3
0 1 0 2 2 3 0
1 3
1 4
1 5
2 7
3 6
4 7
    )";
  MAIN();
  return 0;
}
