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 JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Set;
import java.io.IOException;
import java.util.HashMap;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.function.Consumer;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Map;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author null
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Input in = new Input(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CF1118F1 solver = new CF1118F1();
        solver.solve(1, in, out);
        out.close();
    }

    static class CF1118F1 {
        public void solve(int testNumber, Input in, PrintWriter out) {
            try {
                int n = in.readInt();
                int[] c = new int[n];
                int[] K = new int[3];
                for (int i = 0; i < n; i++) {
                    c[i] = in.readInt();
                    K[c[i]]++;
                }

                Graph<Integer> tree = new Graph<>();
                for (int i = 1; i < n; i++) {
                    int v = in.readInt();
                    int u = in.readInt();
                    tree.addEdge(v - 1, u - 1, true);
                }

                AtomicInteger ans = new AtomicInteger(0);
                int[][] k = new int[3][n];
                DFS.dfs(tree, 0, v -> {
                    k[c[v]][v]++;
                    for (int u : tree.getEdges(v)) {
                        if (((k[1][u] == 0) && (K[2] - k[2][u] == 0)) || ((k[2][u] == 0) && (K[1] - k[1][u] == 0))) {
                            ans.incrementAndGet();
                        }
                        k[1][v] += k[1][u];
                        k[2][v] += k[2][u];
                    }
                });
                out.println(ans.get());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

    }

    static class Input {
        public final BufferedReader reader;
        private String line = "";
        private int pos = 0;

        public Input(InputStream inputStream) {
            reader = new BufferedReader(new InputStreamReader(inputStream));
        }

        private boolean isSpace(char ch) {
            return ch <= 32;
        }

        public String readWord() throws IOException {
            skip();
            int start = pos;
            while (pos < line.length() && !isSpace(line.charAt(pos))) {
                pos++;
            }
            return line.substring(start, pos);
        }

        public int readInt() throws IOException {
            return Integer.parseInt(readWord());
        }

        private void skip() throws IOException {
            while (true) {
                if (pos >= line.length()) {
                    line = reader.readLine();
                    pos = 0;
                }
                while (pos < line.length() && isSpace(line.charAt(pos))) {
                    pos++;
                }
                if (pos < line.length()) {
                    return;
                }
            }
        }

    }

    static class DFS {
        public static <V> void dfs(Graph<V> graph, V start, Consumer<V> vertexPostProcessor) {
            dfsInternal(graph, start, vertexPostProcessor, new HashSet<>());
        }

        private static <V> void dfsInternal(Graph<V> graph, V current, Consumer<V> vertexPostProcessor, Set<V> visited) {
            visited.add(current);
            for (V next : graph.getEdges(current)) {
                if (!visited.contains(next)) {
                    dfsInternal(graph, next, vertexPostProcessor, visited);
                }
            }
            vertexPostProcessor.accept(current);
        }

    }

    static class Graph<V> {
        private final Map<V, Set<V>> edges = new HashMap<>();

        public void addEdge(V from, V to, boolean undirected) {
            edges.computeIfAbsent(from, x -> new HashSet<>()).add(to);
            if (undirected) {
                edges.computeIfAbsent(to, x -> new HashSet<>()).add(from);
            }
        }

        public Set<V> getEdges(V from) {
            return edges.getOrDefault(from, Collections.emptySet());
        }

    }
}