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;
const int maxn = 3e5 + 5, lg = 20, mod = 998244353;
int seg[4 * maxn], par[maxn][lg], h[maxn], st[maxn], ft[maxn], a[maxn],
    T = -1, sz[maxn], zs[maxn], n;
vector<int> vec[maxn], adj[maxn];
long long dp[maxn], pd[maxn];
bool cmp(int v, int u) { return st[v] < st[u]; }
long long P(long long A, long long B) {
  if (!B) return 1;
  long long Y = P(A, B / 2);
  Y *= Y;
  Y %= mod;
  if (B % 2) return Y * A % mod;
  return Y;
}
void addcheck(int id, int L, int R, int l, int r, int val) {
  if (R <= l || r <= L) return;
  if (L >= l && R <= r) {
    seg[id] = max(seg[id], val);
    return;
  }
  int mid = (L + R) / 2;
  addcheck(2 * id + 0, L, mid, l, r, val);
  addcheck(2 * id + 1, mid, R, l, r, val);
}
int getcheck(int id, int L, int R, int idx) {
  if (idx < L || idx >= R) return -1;
  if (R == L + 1) return seg[id];
  int mid = (L + R) / 2;
  return max(seg[id], max(getcheck(2 * id + 0, L, mid, idx),
                          getcheck(2 * id + 1, mid, R, idx)));
}
void add(int id, int L, int R, int idx, int val) {
  if (idx < L || idx >= R) return;
  if (R == L + 1) {
    seg[id] += val;
    return;
  }
  int mid = (L + R) / 2;
  add(2 * id + 0, L, mid, idx, val);
  add(2 * id + 1, mid, R, idx, val);
  seg[id] = seg[2 * id] + seg[2 * id + 1];
}
int get(int id, int L, int R, int l, int r) {
  if (R <= l || r <= L) return 0;
  if (L >= l && R <= r) return seg[id];
  int mid = (L + R) / 2;
  return get(2 * id, L, mid, l, r) + get(2 * id + 1, mid, R, l, r);
}
int get_par(int v, int d) {
  for (int i = 0; i < lg; i++) {
    if (d & (1 << i)) v = par[v][i];
  }
  return v;
}
int lca(int v, int u) {
  if (h[v] > h[u]) swap(v, u);
  u = get_par(u, h[u] - h[v]);
  if (v == u) return v;
  for (int i = lg - 1; i >= 0; i--) {
    if (par[v][i] != par[u][i]) {
      v = par[v][i];
      u = par[u][i];
    }
  }
  return par[v][0];
}
void dfs(int v, int p = -1) {
  par[v][0] = p;
  fill(par[v] + 1, par[v] + lg, -1);
  for (int i = 1; i < lg; i++) {
    if (par[v][i - 1] == -1) break;
    par[v][i] = par[par[v][i - 1]][i - 1];
  }
  st[v] = ++T;
  sz[v] = (bool)(~a[v]);
  for (auto u : adj[v]) {
    if (u != p) {
      h[u] = h[v] + 1;
      dfs(u, v);
      sz[v] += sz[u];
    }
  }
  ft[v] = T;
}
void getzs(int v, int p = -1) {
  for (auto u : adj[v]) {
    if (u != p) {
      zs[u] = zs[v] + sz[v] - sz[u];
      getzs(u, v);
    }
  }
}
void getdp(int v, int p = -1) {
  long long Pi = 1;
  for (auto u : adj[v]) {
    if (u != p) {
      getdp(u, v);
      Pi *= dp[u] + pd[u];
      Pi %= mod;
    }
  }
  if (!sz[v]) {
    dp[v] = 0;
  } else {
    if (get(1, 0, n, st[v], ft[v] + 1)) {
      dp[v] = Pi;
    } else {
      for (auto u : adj[v]) {
        if (u != p) {
          if (sz[u]) {
            dp[v] += dp[u] * Pi % mod * P(dp[u] + pd[u], mod - 2) % mod;
            dp[v] %= mod;
          }
        }
      }
    }
  }
  if (!zs[v]) {
    pd[v] = 0;
  } else {
    if (get(1, 0, n, st[v], ft[v] + 1)) {
      pd[v] = 0;
    } else {
      pd[v] = Pi;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int k;
  cin >> n >> k;
  fill(seg, seg + 4 * n, -1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    if (~a[i]) vec[a[i]].push_back(i);
  }
  for (int i = 0; i < n - 1; i++) {
    int v, u;
    cin >> v >> u;
    v--;
    u--;
    adj[v].push_back(u);
    adj[u].push_back(v);
  }
  dfs(0);
  for (int i = 0; i < k; i++) {
    sort(vec[i].begin(), vec[i].end(), cmp);
    for (int j = 0; j < vec[i].size() - 1; j++) {
      int x = lca(vec[i][j], vec[i][j + 1]);
      if (getcheck(1, 0, n, st[vec[i][j]]) >= h[x] ||
          getcheck(1, 0, n, st[vec[i][j + 1]]) >= h[x]) {
        cout << 0;
        return 0;
      }
    }
    for (auto v : vec[i]) {
      addcheck(1, 0, n, st[v], ft[v] + 1, h[v]);
    }
  }
  fill(seg, seg + 4 * n, -1);
  for (int i = k - 1; i >= 0; i--) {
    for (int j = 0; j < vec[i].size() - 1; j++) {
      int x = lca(vec[i][j], vec[i][j + 1]);
      if (getcheck(1, 0, n, st[vec[i][j]]) >= h[x] ||
          getcheck(1, 0, n, st[vec[i][j + 1]]) >= h[x]) {
        cout << 0;
        return 0;
      }
    }
    for (auto v : vec[i]) {
      addcheck(1, 0, n, st[v], ft[v] + 1, h[v]);
    }
  }
  fill(seg, seg + 4 * n, 0);
  for (int i = 0; i < k; i++) {
    if (vec[i].size() == 1) {
      add(1, 0, n, st[vec[i][0]], +1);
      if (~par[vec[i][0]][0]) add(1, 0, n, st[par[vec[i][0]][0]], -1);
      continue;
    }
    for (int j = 0; j < vec[i].size() - 1; j++) {
      int x = lca(vec[i][j], vec[i][j + 1]);
      add(1, 0, n, st[vec[i][j]], +1);
      add(1, 0, n, st[vec[i][j + 1]], +1);
      if (~par[x][0]) add(1, 0, n, st[par[x][0]], -2);
    }
  }
  getzs(0);
  getdp(0);
  cout << dp[0];
}