Problem: You are given an undirected tree of n vertices. 

Some vertices are colored blue, some are colored red and some are uncolored. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.

You choose an edge and remove it from the tree. Tree falls apart into two connected components. Let's call an edge nice if neither of the resulting components contain vertices of both red and blue colors.

How many nice edges are there in the given tree?

Input

The first line contains a single integer n (2 ≤ n ≤ 3 ⋅ 10^5) — the number of vertices in the tree.

The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2) — the colors of the vertices. a_i = 1 means that vertex i is colored red, a_i = 2 means that vertex i is colored blue and a_i = 0 means that vertex i is uncolored.

The i-th of the next n - 1 lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ n, v_i ≠ u_i) — the edges of the tree. It is guaranteed that the given edges form a tree. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.

Output

Print a single integer — the number of nice edges in the given tree.

Examples

Input


5
2 0 0 1 2
1 2
2 3
2 4
2 5


Output


1


Input


5
1 0 0 0 2
1 2
2 3
3 4
4 5


Output


4


Input


3
1 1 2
2 3
1 3


Output


0

Note

Here is the tree from the first example:

<image>

The only nice edge is edge (2, 4). Removing it makes the tree fall apart into components \{4\} and \{1, 2, 3, 5\}. The first component only includes a red vertex and the second component includes blue vertices and uncolored vertices.

Here is the tree from the second example:

<image>

Every edge is nice in it.

Here is the tree from the third example:

<image>

Edge (1, 3) splits the into components \{1\} and \{3, 2\}, the latter one includes both red and blue vertex, thus the edge isn't nice. Edge (2, 3) splits the into components \{1, 3\} and \{2\}, the former one includes both red and blue vertex, thus the edge also isn't nice. So the answer is 0.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
long long dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
long long dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};
class pa3 {
 public:
  long long x;
  long long y, z;
  pa3(long long x = 0, long long y = 0, long long z = 0) : x(x), y(y), z(z) {}
  bool operator<(const pa3& p) const {
    if (x != p.x) return x < p.x;
    if (y != p.y) return y < p.y;
    return z < p.z;
  }
  bool operator>(const pa3& p) const {
    if (x != p.x) return x > p.x;
    if (y != p.y) return y > p.y;
    return z > p.z;
  }
  bool operator==(const pa3& p) const {
    return x == p.x && y == p.y && z == p.z;
  }
  bool operator!=(const pa3& p) const {
    return !(x == p.x && y == p.y && z == p.z);
  }
};
class pa4 {
 public:
  long long x;
  long long y, z, w;
  pa4(long long x = 0, long long y = 0, long long z = 0, long long w = 0)
      : x(x), y(y), z(z), w(w) {}
  bool operator<(const pa4& p) const {
    if (x != p.x) return x < p.x;
    if (y != p.y) return y < p.y;
    if (z != p.z) return z < p.z;
    return w < p.w;
  }
  bool operator>(const pa4& p) const {
    if (x != p.x) return x > p.x;
    if (y != p.y) return y > p.y;
    if (z != p.z) return z > p.z;
    return w > p.w;
  }
  bool operator==(const pa4& p) const {
    return x == p.x && y == p.y && z == p.z && w == p.w;
  }
};
class pa2 {
 public:
  long long x, y;
  pa2(long long x = 0, long long y = 0) : x(x), y(y) {}
  pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); }
  pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); }
  bool operator<(const pa2& p) const { return y != p.y ? y < p.y : x < p.x; }
  bool operator>(const pa2& p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const pa2& p) const {
    return abs(x - p.x) == 0 && abs(y - p.y) == 0;
  }
  bool operator!=(const pa2& p) const {
    return !(abs(x - p.x) == 0 && abs(y - p.y) == 0);
  }
};
string itos(long long i) {
  ostringstream s;
  s << i;
  return s.str();
}
long long Gcd(long long v, long long b) {
  if (v == 0) return b;
  if (b == 0) return v;
  if (v > b) return Gcd(b, v);
  if (v == b) return b;
  if (b % v == 0) return v;
  return Gcd(v, b % v);
}
long long mod;
long long extgcd(long long a, long long b, long long& x, long long& y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long d = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
pair<long long, long long> operator+(const pair<long long, long long>& l,
                                     const pair<long long, long long>& r) {
  return {l.first + r.first, l.second + r.second};
}
pair<long long, long long> operator-(const pair<long long, long long>& l,
                                     const pair<long long, long long>& r) {
  return {l.first - r.first, l.second - r.second};
}
ostream& operator<<(ostream& os, const vector<long long>& VEC) {
  for (auto v : VEC) os << v << " ";
  return os;
}
ostream& operator<<(ostream& os, const pair<long long, long long>& PAI) {
  os << PAI.first << " : " << PAI.second;
  return os;
}
ostream& operator<<(ostream& os, const pa3& PAI) {
  os << PAI.x << " : " << PAI.y << " : " << PAI.z;
  return os;
}
ostream& operator<<(ostream& os, const pa4& PAI) {
  os << PAI.x << " : " << PAI.y << " : " << PAI.z << " : " << PAI.w;
  return os;
}
ostream& operator<<(ostream& os,
                    const vector<pair<long long, long long>>& VEC) {
  for (auto v : VEC) os << v << " ";
  return os;
}
long long beki(long long wa, long long rr, long long warukazu) {
  if (rr == 0) return 1 % warukazu;
  if (rr == 1) return wa % warukazu;
  wa %= warukazu;
  if (rr % 2 == 1)
    return ((long long)beki(wa, rr - 1, warukazu) * (long long)wa) % warukazu;
  long long zx = beki(wa, rr / 2, warukazu);
  return (zx * zx) % warukazu;
}
long long pr[1100000];
long long inv[1100000];
long long comb(long long nn, long long rr) {
  if (rr < 0 || rr > nn || nn < 0) return 0;
  long long r = pr[nn] * inv[rr];
  r %= mod;
  r *= inv[nn - rr];
  r %= mod;
  return r;
}
void gya(long long ert) {
  pr[0] = 1;
  for (long long i = 1; i <= ert; i++) {
    pr[i] = ((long long)pr[i - 1] * i) % mod;
  }
  inv[ert] = beki(pr[ert], mod - 2, mod);
  for (long long i = ert - 1; i >= 0; i--) {
    inv[i] = (long long)inv[i + 1] * (i + 1) % mod;
  }
}
vector<long long> G[300030], G2[300030];
vector<long long> ob;
long long fg = 0;
long long dp[20][300020];
long long dep[300020];
long long lca(long long a, long long b) {
  if (dep[a] < dep[b]) swap(a, b);
  for (long long i = 0; i < 20; i++)
    if ((dep[a] - dep[b]) & (1 << i)) {
      a = dp[i][a];
    }
  if (a == b) return a;
  for (long long i = 19; i >= 0; i--) {
    if (dp[i][a] != dp[i][b]) {
      a = dp[i][a], b = dp[i][b];
    }
  }
  return dp[0][a];
}
void dfs1(long long r, long long p, long long d = 0) {
  ob[r] = fg;
  fg++;
  dep[r] = d;
  dp[0][r] = p;
  for (auto v : G[r])
    if (v != p) dfs1(v, r, d + 1);
}
vector<long long> c;
void ch(long long r, long long g, long long co) {
  while (1) {
    if (c[r] == 0) {
      c[r] = co;
      if (r == g) return;
      r = dp[0][r];
    } else if (c[r] == co) {
      return;
    } else {
      cout << 0 << endl;
      exit(0);
    }
  }
}
long long fp1[300030] = {};
long long fp2[300030] = {};
void dfs2(long long r, long long p, long long K) {
  vector<long long> ve1, ve2;
  long long wa = 0;
  vector<long long> w;
  w.push_back(1);
  for (auto v : G2[r])
    if (v != p) {
      dfs2(v, r, K);
      ve1.push_back(fp1[v]);
      ve2.push_back(fp2[v]);
      wa += fp2[v];
      wa %= mod;
      w.push_back(w.back() * ((fp1[v] + fp2[v]) % mod) % mod);
    }
  vector<long long> ww;
  ww.push_back(1);
  for (long long i = (long long)ve1.size() - 1; i >= 0; i--) {
    ww.push_back(ww.back() * ((ve1[i] + ve2[i]) % mod) % mod);
  }
  reverse(ww.begin(), ww.end());
  fp1[r] = 0;
  fp2[r] = 1;
  if (r > K) {
    for (long long i = 0; i < ve1.size(); i++) {
      fp2[r] *= (ve1[i] + ve2[i]) % mod;
      fp2[r] %= mod;
    }
    for (long long i = 0; i < ve1.size(); i++) {
      fp1[r] += ve1[i] * (w[i] * ww[i + 1] % mod) % mod;
      fp1[r] %= mod;
    }
  } else {
    fp1[r] = 1;
    for (long long i = 0; i < ve1.size(); i++) {
      fp1[r] *= (ve1[i] + ve2[i]) % mod;
      fp1[r] %= mod;
    }
    fp2[r] = 0;
  }
  return;
}
void solve() {
  long long n, k;
  cin >> n >> k;
  mod = 998244353;
  c.resize(n, 0);
  for (long long i = 0; i < n; i++) {
    cin >> c[i];
  }
  vector<vector<pair<long long, long long>>> ve(
      k + 10, vector<pair<long long, long long>>());
  ob.resize(n);
  for (long long i = 0; i < n - 1; i++) {
    long long y, yy;
    cin >> y >> yy;
    y--, yy--;
    G[y].push_back(yy);
    G[yy].push_back(y);
  }
  dfs1(0, 0);
  for (long long i = 1; i < 20; i++)
    for (long long j = 0; j < n; j++) dp[i][j] = dp[i - 1][dp[i - 1][j]];
  for (long long i = 0; i < n; i++)
    if (c[i]) {
      ve[c[i]].push_back(make_pair(ob[i], i));
    }
  for (long long i = 0; i < n; i++) c[i] = 0;
  for (long long i = 1; i <= k; i++) {
    sort(ve[i].begin(), ve[i].end());
    long long lc = lca(ve[i][0].second, ve[i].back().second);
    for (auto v : ve[i]) {
      ch(v.second, lc, i);
    }
  }
  set<pair<long long, long long>> se;
  vector<long long> ji(n, 0);
  for (long long i = 0; i < n; i++)
    if (c[i] == 0) {
      ji[i] = G[i].size(), se.insert(make_pair(G[i].size(), i));
    }
  vector<long long> dame(n, 0);
  long long qw = 0;
  while (1) {
    if (se.size() == 0) break;
    auto it = se.begin();
    if ((*it).first > 1) break;
    long long g = (*it).second;
    ji[g] = 0;
    dame[g] = 1;
    se.erase(it);
    long long t = -1;
    for (auto v : G[g])
      if (dame[v] == 0) {
        t = v;
      }
    if (c[t] == 0) {
      se.erase((pair<long long, long long>)make_pair(ji[t], t));
      ji[t]--;
      se.insert(make_pair(ji[t], t));
    }
  }
  vector<long long> id(n + 1);
  fg = k + 1;
  for (long long i = 0; i < n; i++)
    if (c[i] == 0 && dame[i] == 0) {
      id[i] = fg;
      fg++;
    }
  for (long long i = 0; i < n; i++)
    if (dame[i] == 0)
      for (auto v : G[i])
        if (dame[v] == 0) {
          if (c[i] == c[v] && c[i] > 0) continue;
          long long x = i, y = v;
          if (c[x] == 0)
            x = id[x];
          else
            x = c[x];
          if (c[y] == 0)
            y = id[y];
          else
            y = c[y];
          G2[x].push_back(y);
        }
  dfs2(1, 1, k);
  cout << fp1[1] << endl;
}
signed main() {
  mod = 1000000007;
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n = 1;
  for (long long i = 0; i < n; i++) solve();
  return 0;
}