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.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
namespace io {
class input_reader {
  template <typename T>
  inline void readIntegerValue(T &f) {
    bool positive = true;
    f = 0;
    char c;
    if (!in.get(c))
      throw std::runtime_error("No more inputs while reading number");
    while (c < '0' || c > '9') {
      if (c == '-') positive = false;
      if (!in.get(c))
        throw std::runtime_error("No more inputs while reading number");
    }
    while (c >= '0' && c <= '9') {
      f = (f << 3) + (f << 1) + (c & 15);
      if (!in.get(c)) break;
    }
    if (!positive) f *= -1;
  }

 public:
  input_reader(std::istream &in) : in(in) {}
  inline void read(int &f) { readIntegerValue(f); }
  inline void read(short &f) { readIntegerValue(f); }
  inline void read(long int &f) { readIntegerValue(f); }
  inline void read(long long int &f) { readIntegerValue(f); }
  inline void read(unsigned int &f) { readIntegerValue(f); }
  inline void read(unsigned short &f) { readIntegerValue(f); }
  inline void read(unsigned long int &f) { readIntegerValue(f); }
  inline void read(unsigned long long int &f) { readIntegerValue(f); }
  template <typename T>
  inline void read(std::vector<T> &f) {
    for (auto &i : f) read(i);
  }
  template <typename T, int N>
  inline void read(std::array<T, N> &res) {
    for (auto &i : res) read(i);
  }
  template <typename T1, typename T2>
  inline void read(std::pair<T1, T2> &p) {
    in.read(p.first, p.second);
  }
  template <typename T>
  inline void read(T &f) {
    in >> f;
  }
  template <typename T1, typename T2>
  inline void read(T1 &t1, T2 &t2) {
    read(t1), read(t2);
  }
  template <typename T1, typename T2, typename T3>
  inline void read(T1 &t1, T2 &t2, T3 &t3) {
    read(t1, t2), read(t3);
  }
  template <typename T1, typename T2, typename T3, typename T4>
  inline void read(T1 &t1, T2 &t2, T3 &t3, T4 &t4) {
    read(t1, t2, t3), read(t4);
  }
  template <typename T1, typename T2, typename T3, typename T4, typename T5>
  inline void read(T1 &t1, T2 &t2, T3 &t3, T4 &t4, T5 &t5) {
    read(t1, t2, t3, t4), read(t5);
  }
  template <typename T>
  inline void readArray(int n, T *res) {
    while (n--) read(*(res++));
  }
  template <typename T>
  input_reader &operator>>(T &t) {
    return read(t), *this;
  }
  std::istream &in;
};
}  // namespace io
namespace io {
class output_writer {
  std::ostream &out;
  template <typename T>
  inline void printIntegerHelper(T x) {
    if (x >= 10) printIntegerHelper(x / 10);
    out.put(x % 10 + 48);
  }
  template <typename T>
  inline void printInteger(T x) {
    if (x < 0) out.put('-'), x = -x;
    printIntegerHelper(x);
  }
  template <typename T>
  inline void printArray(T &ar) {
    bool first = true;
    for (auto &t : ar) print((first) ? first = false, "" : " ", t);
  }

 public:
  output_writer(std::ostream &out) : out(out) {}
  inline void flush() { out.flush(); }
  inline void print() {}
  inline void print(char x) { out.put(x); }
  inline void print(short x) { printInteger(x); }
  inline void print(int x) { printInteger(x); }
  inline void print(long int x) { printInteger(x); }
  inline void print(long long int x) { printInteger(x); }
  inline void print(unsigned short x) { printInteger(x); }
  inline void print(unsigned int x) { printInteger(x); }
  inline void print(unsigned long int x) { printInteger(x); }
  inline void print(unsigned long long int x) { printInteger(x); }
  template <typename T1>
  inline void print(const T1 &t1) {
    out << t1;
  }
  template <typename T1>
  inline void print(const std::vector<T1> &t1) {
    printArray(t1);
  }
  template <typename T1, int N>
  inline void print(const std::array<T1, N> &t1) {
    printArray(t1);
  }
  template <typename T1, int N, int M>
  inline void print(const std::array<std::array<T1, M>, N> &t1) {
    for (auto &t : t1) println(t);
  }
  template <typename T1>
  inline void print(const std::vector<std::vector<T1>> &t1) {
    for (auto &t : t1) println(t);
  }
  template <typename T, typename... Args>
  inline void print(const T &t, Args... args) {
    print(t), print(args...);
  }
  template <typename... Args>
  inline void println(Args... args) {
    print(args...), print('\n');
  }
  template <typename T>
  output_writer &operator<<(const T &t) {
    return print(t), *this;
  }
};
}  // namespace io
using namespace std;
class F2TreeCuttingHardVersion {
  class Solver {
    io::input_reader in;
    io::output_writer out;

   public:
    Solver(std::istream &in, std::ostream &out) : in(in), out(out) {}
    const long long int MOD = 998244353;
    vector<int> a, parent, depth;
    vector<vector<int>> e;
    int n, k;
    vector<bool> visited;
    void dfs(const int i, const int p) {
      parent[i] = p;
      for (int j : e[i]) {
        if (j == p) continue;
        depth[j] = depth[i] + 1;
        dfs(j, i);
      }
    }
    bool group_vertices(const vector<int> &v) {
      auto comp = [&](int i, int j) { return depth[i] < depth[j]; };
      priority_queue<int, vector<int>, decltype(comp)> pq(comp);
      for (int i : v) pq.push(i);
      while (!pq.empty()) {
        int i = pq.top();
        pq.pop();
        if (!pq.empty()) {
          int j = parent[i];
          if (a[j] != a[i]) {
            if (a[j] == 0) {
              a[j] = a[i];
              pq.push(j);
            } else {
              return false;
            }
          }
        }
      }
      return true;
    }
    void solve() {
      in >> n >> k;
      visited.resize(n);
      a.resize(n);
      e.resize(n);
      vector<vector<long long int>> res(n, vector<long long int>(2));
      in >> a;
      long long int total = 1;
      for (int i = 0; i < n - 1; i++) {
        int u, v;
        in.read(u, v);
        u--, v--;
        e[u].push_back(v);
        e[v].push_back(u);
      }
      parent.resize(n);
      depth.resize(n);
      int startP = 0;
      for (int i = 0; i < n; i++)
        if (a[i]) startP = i;
      dfs(startP, -1);
      vector<vector<int>> gr_col(k + 1);
      for (int i = 0; i < n; i++) gr_col[a[i]].push_back(i);
      for (int i = (1); i <= (k); ++i)
        if (!group_vertices(gr_col[i])) {
          out << 0;
          return;
        }
      vector<int> v(n);
      for (int i = 0; i < n; i++) v[i] = i;
      sort((v).begin(), (v).end(),
           [&](int i, int j) { return depth[i] > depth[j]; });
      int last = v.back();
      v.pop_back();
      for (int i = 0; i < n; i++) {
        if (a[i]) {
          res[i][0] = 0, res[i][1] = 1;
        } else {
          res[i][1] = 0, res[i][0] = 1;
        }
      }
      for (int i : v) {
        int j = parent[i];
        if (!a[j]) {
          long long int c0 =
              (res[j][0] * res[i][0] % MOD + res[j][0] * res[i][1] % MOD) % MOD;
          long long int c1 = (res[j][0] * res[i][1] % MOD +
                              res[j][1] * (res[i][0] + res[i][1]) % MOD) %
                             MOD;
          res[j] = {c0, c1};
        } else {
          res[j][1] = (res[j][1] * (res[i][1] + res[i][0])) % MOD;
        }
      }
      out << res[last][1];
    }
  };

 public:
  void solve(std::istream &in, std::ostream &out) { Solver(in, out).solve(); }
};
int main() {
  std::ios_base::sync_with_stdio(false), cin.tie(0);
  F2TreeCuttingHardVersion solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}
