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 long long int MOD = 1e9 + 7;
const long long int MOD2 = 998244353;
long double PI = 2 * acos(0.0);
template <typename T>
T gcd(T a, T b) {
  return (b == 0) ? a : gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
  return a * (b / gcd(a, b));
}
template <typename T>
T mod_exp(T b, T p, T m) {
  T x = 1;
  while (p) {
    if (p & 1) x = (x * b) % m;
    b = (b * b) % m;
    p = p >> 1;
  }
  return x;
}
template <typename T>
T invFermat(T a, T p) {
  return mod_exp(a, p - 2, p);
}
template <typename T>
T exp(T b, T p) {
  T x = 1;
  while (p) {
    if (p & 1) x = (x * b);
    b = (b * b);
    p = p >> 1;
  }
  return x;
}
string tostring(long long int number) {
  stringstream ss;
  ss << number;
  return ss.str();
}
long long int toint(const string& s) {
  stringstream ss;
  ss << s;
  long long int x;
  ss >> x;
  return x;
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
struct node {
  long long int blue;
  long long int red;
  long long int no_col;
};
vector<vector<long long int>> tree;
const long long int maxm = 3 * 1e5 + 5;
long long int n;
long long int a[maxm];
vector<node> vals;
vector<bool> vis(maxm, false);
void dfs(long long int src) {
  vis[src] = true;
  for (auto edge : tree[src]) {
    if (!vis[edge]) {
      dfs(edge);
      vals[src].no_col += vals[edge].no_col;
      vals[src].blue += vals[edge].blue;
      vals[src].red += vals[edge].red;
    }
  }
  if (a[src] == 1)
    vals[src].red += 1;
  else if (a[src] == 2)
    vals[src].blue += 1;
  else
    vals[src].no_col += 1;
}
void solve() {
  cin >> n;
  tree.resize(n);
  vals.resize(n);
  long long int tot_blue = 0, tot_red = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 1) tot_red++;
    if (a[i] == 2) tot_blue++;
    vals[i].red = 0;
    vals[i].blue = 0;
    vals[i].no_col = 0;
  }
  set<pair<long long int, long long int>> edges;
  for (long long int i = 0; i < n - 1; i++) {
    long long int x, y;
    cin >> x >> y;
    x--;
    y--;
    if (x > y) swap(x, y);
    edges.insert({x, y});
    tree[x].push_back(y);
    tree[y].push_back(x);
  }
  dfs(0);
  set<long long int> checked;
  long long int ans = 0;
  for (auto p : edges) {
    long long int op1, op2;
    op1 = vals[p.first].red + vals[p.first].blue + vals[p.first].no_col;
    op2 = vals[p.second].red + vals[p.second].blue + vals[p.second].no_col;
    if (op1 > op2) {
      if (vals[p.second].red == tot_red and vals[p.second].blue == 0)
        ans++;
      else if (vals[p.second].red == 0 and vals[p.second].blue == tot_blue)
        ans++;
    } else {
      if (vals[p.first].red == tot_red and vals[p.first].blue == 0)
        ans++;
      else if (vals[p.first].red == 0 and vals[p.first].blue == tot_blue)
        ans++;
    }
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}