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 N = 3e5 + 15;
const int M = 19;
vector<int> adj[N];
bool vis[N];
int a[N], val[N];
int commonAnc[N], jump[N][M], v[N][M], depth[N];
void dfs(int node, int p) {
  v[node][0] = val[node];
  jump[node][0] = p;
  for (int i = 1; i < M; ++i) {
    v[node][i] = v[node][i - 1] + v[jump[node][i - 1]][i - 1];
    jump[node][i] = jump[jump[node][i - 1]][i - 1];
  }
  for (int i = 0; i < adj[node].size(); ++i) {
    int ch = adj[node][i];
    if (ch == p) {
      continue;
    }
    depth[ch] = depth[node] + 1;
    dfs(ch, node);
  }
}
pair<int, int> getLca(int x, int y) {
  assert(x > 0 && y > 0);
  if (depth[x] < depth[y]) {
    swap(x, y);
  }
  int diff = depth[x] - depth[y];
  assert(diff >= 0);
  int rr = 0;
  for (int i = 0; i < M; ++i) {
    if (diff & (1 << i)) {
      rr += v[x][i];
      x = jump[x][i];
    }
  }
  if (x != y) {
    for (int i = M - 1; i >= 0; --i) {
      if (jump[x][i] != jump[y][i]) {
        rr += v[x][i] + v[y][i];
        x = jump[x][i];
        y = jump[y][i];
      }
    }
    rr += v[x][0];
    x = jump[x][0];
  }
  rr += v[x][0];
  return make_pair(rr, x);
}
int dsu[N];
int f(int x) {
  if (x == dsu[x]) {
    return x;
  }
  return dsu[x] = f(dsu[x]);
}
void merge(int a, int b) {
  a = f(a), b = f(b);
  if (a != b) {
    dsu[a] = b;
  }
}
int compColor[N], dist[N];
int bfsColor[N];
int dp[N][2];
const int mod = 998244353;
int add(int x, int y) {
  int ret = (x + y) % mod;
  if (ret < 0) {
    ret += mod;
  }
  return ret;
}
int mul(int x, int y) { return (long long)x * y % mod; }
int myPow(int x, int c) {
  int ret = 1;
  while (c > 0) {
    if (c & 1) {
      ret = mul(ret, x);
    }
    c /= 2;
    x = mul(x, x);
  }
  return ret;
}
int dpDfs(int node, int par) {
  if (a[node] != 0) {
    return 1;
  }
  vis[node] = 1;
  int ret = 0;
  vector<int> chList;
  map<int, int> G;
  int cur = 1;
  for (int i = 0; i < adj[node].size(); ++i) {
    int ch = adj[node][i];
    if (ch == par) {
      continue;
    }
    if (dpDfs(ch, node)) {
      ret = 1;
      chList.push_back(ch);
      int v = add(dp[ch][0], dp[ch][1]);
      if (v == 0) {
        cout << ch << " " << dp[ch][0] << " " << dp[ch][1] << endl;
      }
      G[v]++;
      if (v > 0) {
        cur = mul(cur, v);
      }
    }
  }
  dp[node][0] = cur;
  if (G[0] != 0) {
    dp[node][0] = 0;
  }
  for (int i = 0; i < chList.size(); ++i) {
    int ch = chList[i];
    int ccur = cur;
    int v = add(dp[ch][0], dp[ch][1]);
    --G[v];
    if (G[0] != 0) {
      ccur = 0;
    }
    if (v > 0) {
      ccur = mul(ccur, myPow(v, mod - 2));
    }
    ++G[v];
    ccur = mul(ccur, dp[ch][1]);
    dp[node][1] = add(dp[node][1], ccur);
  }
  return ret;
}
void solve() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%d ", &a[i]);
  }
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    scanf("%d %d", &x, &y);
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  memset(commonAnc, -1, sizeof(commonAnc));
  dfs(1, 0);
  for (int i = 1; i <= n; ++i) {
    if (a[i] != 0) {
      int color = a[i];
      if (commonAnc[color] == -1) {
        commonAnc[color] = i;
      }
      commonAnc[color] = getLca(commonAnc[color], i).second;
    }
  }
  for (int i = 1; i <= k; ++i) {
    assert(commonAnc[i] > 0);
    val[commonAnc[i]] += 1;
  }
  dfs(1, 0);
  queue<pair<int, int> > q;
  for (int i = 1; i <= n; ++i) {
    if (a[i] != 0) {
      int color = a[i];
      pair<int, int> tmp = getLca(commonAnc[color], i);
      assert(tmp.first >= 1);
      if (tmp.first > 1) {
        printf("0\n");
        return;
      }
      q.push(make_pair(i, commonAnc[color]));
    }
  }
  for (int i = 1; i <= n; ++i) {
    dsu[i] = i;
  }
  while (!q.empty()) {
    pair<int, int> tmp = q.front();
    int node = tmp.first, target = tmp.second;
    q.pop();
    if (node == target) {
      continue;
    }
    int par = jump[node][0];
    merge(par, node);
    if (!vis[par]) {
      vis[par] = true;
      q.push(make_pair(par, target));
    }
  }
  for (int i = 1; i <= k; ++i) {
    int node = f(commonAnc[i]);
    compColor[node] = i;
  }
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; ++i) {
    int id = f(i);
    if (compColor[id] != 0) {
      a[i] = compColor[id];
    }
    if (a[i] != 0) {
      dp[i][0] = 0;
      dp[i][1] = 1;
    } else {
      dp[i][0] = 1;
      dp[i][1] = 0;
    }
  }
  int ret = 1;
  for (int i = 1; i <= n; ++i) {
    if (!vis[i] && a[i] == 0) {
      dpDfs(i, 0);
      ret = mul(ret, dp[i][1]);
    }
  }
  cout << ret << endl;
}
int main() { solve(); }