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.*;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        FastScanner in = new FastScanner();
        Task solver = new Task();
        solver.solve(in);
        in.close();
    }

    static class Task {
        ArrayList<Integer>[] g;
        int color[];
        int w[][];

        void dfs(int v, int p) {
            w[v][color[v]]++;
            for (int i = 0; i < g[v].size(); i++) {
                int to = g[v].get(i);
                if (to == p)
                    continue;
                dfs(to, v);
                for (int t = 0; t < 3; t++)
                    w[v][t] += w[to][t];
            }
        }

        int ans = 0;
        void calc(int v, int p) {
            for (int i = 0; i < g[v].size(); i++) {
                int to = g[v].get(i);
                if (to == p)
                    continue;
                boolean ft1 = (w[to][1] > 0);
                boolean ft2 = (w[to][2] > 0);
                boolean fv1 = (w[0][1] - w[to][1] > 0);
                boolean fv2 = (w[0][2] - w[to][2] > 0);

                if (!(ft1 & ft2) && !(fv1 & fv2))
                    ans++;

                calc(to, v);
            }
        }

        public void solve(FastScanner in) {
            int n = in.nextInt();
            g = new ArrayList[n];
            color = new int[n];
            w = new int[n][3];
            for (int i = 0; i < n; i++)
                g[i] = new ArrayList<Integer>();
            for (int i = 0; i < n; i++)
                color[i] = in.nextInt();

            int a, b;
            for (int i = 0; i < n - 1; i++) {
                a = in.nextInt();
                b = in.nextInt();
                a--; b--;
                g[a].add(b);
                g[b].add(a);
            }

            dfs(0, 0);
            calc(0, 0);

            System.out.print(ans);
        }
    }




    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    public static class FastScanner {
        final private int BUFFER_SIZE = 4096;
        private InputStreamReader in;
        private char[] buffer;
        private int bufferPointer, bytesRead;

        public FastScanner() {
            in = new InputStreamReader(System.in, StandardCharsets.UTF_8);
            buffer = new char[BUFFER_SIZE];
            bufferPointer = 0;
            bytesRead = 0;
        }

        public FastScanner(String name) {
            try {
                in = new InputStreamReader(new FileInputStream(name), StandardCharsets.UTF_8);
            } catch (Exception e) {
                e.printStackTrace();
            }
            buffer = new char[BUFFER_SIZE];
            bufferPointer = 0;
            bytesRead = 0;
        }

        private void fillBuffer() {
            bufferPointer = 0;
            try {
                while ((bytesRead = in.read(buffer)) == 0) {}
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (bytesRead == -1)
                buffer[0] = 0;
        }

        private char next() {
            if (bufferPointer >= bytesRead) {
                fillBuffer();
            }
            return buffer[bufferPointer++];
        }

        public boolean hasNext() {
            if (bufferPointer >= bytesRead) {
                fillBuffer();
                return (bytesRead != -1);
            }
            return true;
        }

        public String nextLine() {
            try {
                StringBuilder now = new StringBuilder();
                char c;
                while (true) {
                    c = next();
                    if (c == 0 || c == '\n') {
                        break;
                    }
                    now.append(c);
                }
                return now.toString();
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }
        }

        private int convert(StringBuilder s) {
            try {
                return Integer.parseInt(s.toString());
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
                return 0;
            }
        }

        public ArrayList<Integer> nextLineAsIntegers() {
            try {
                String inp = nextLine();
                StringBuilder now = new StringBuilder();
                ArrayList<Integer> res = new ArrayList<>();

                for (int i = 0; i < inp.length(); i++) {
                    if (!(Character.isDigit(inp.charAt(i)) || inp.charAt(i) == '-')) {
                        if (now.length() > 0) {
                            res.add(convert(now));
                            now.delete(0, now.length());
                        }
                    } else {
                        now.append(inp.charAt(i));
                    }
                }
                if (now.length() > 0) {
                    res.add(convert(now));
                }
                return res;
            } catch (Exception e) {
                e.printStackTrace();
                return new ArrayList<>();
            }
        }

        public ArrayList<String> nextLineAsWords() {
            ArrayList<String> res = new ArrayList<>();

            try {
                StringBuffer now = new StringBuffer();
                String inp = nextLine();

                for (int i = 0; i < inp.length(); i++) {
                    if (!(Character.isLetter(inp.charAt(i)) || Character.getType(inp.charAt(i)) == Character.DASH_PUNCTUATION || inp.charAt(i) == '\'')) {
                        if (now.length() == 0) {
                            continue;
                        }
                        String cur = now.toString().toLowerCase();
                        res.add(cur);
                        now.delete(0, now.length());
                    } else {
                        now.append(inp.charAt(i));
                    }
                }
                if (now.length() > 0) {
                    String cur = now.toString().toLowerCase();
                    res.add(cur);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return res;
        }

        public String nextString() {
            while(true) {
                char c = next();
                if (!Character.isWhitespace(c)) {
                    bufferPointer--;
                    break;
                }
            }
            try {
                StringBuilder now = new StringBuilder();
                char c;
                while (true) {
                    c = next();
                    if (c == 0 || Character.isWhitespace(c)) {
                        break;
                    }
                    now.append(c);
                }
                return now.toString();
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }
        }

        public int nextInt() {
            try {
                int sign = 1, res = 0;
                char c;
                while (true) {
                    c = next();
                    if (!Character.isWhitespace(c)) {
                        break;
                    }
                }
                if (c == '-') {
                    sign = -1;
                    c = next();
                }
                while (Character.isDigit(c)) {
                    res = res * 10 + (int) (c - '0');
                    c = next();
                }
                bufferPointer--;
                if (sign == 1) {
                    return res;
                } else {
                    return -res;
                }
            } catch(Exception e) {
                e.printStackTrace();
                return 0;
            }
        }

        public long nextLong() {
            try {
                long sign = 1l, res = 0l;
                char c;
                while (true) {
                    c = next();
                    if (!Character.isWhitespace(c)) {
                        break;
                    }
                }
                if (c == '-') {
                    sign = -1l;
                    c = next();
                }
                while (Character.isDigit(c)) {
                    res = res * 10l + (long) (c - '0');
                    c = next();
                }
                bufferPointer--;
                if (sign == 1l) {
                    return res;
                } else {
                    return -res;
                }
            } catch(Exception e) {
                e.printStackTrace();
                return 0l;
            }
        }

        public double nextDouble() {
            double res = 0.0, div = 1.0;
            char c;
            while (true) {
                c = next();
                if (!Character.isWhitespace(c)) {
                    break;
                }
            }
            boolean sign = (c == '-');
            if (sign)
                c = next();

            do {
                res = res * 10.0 + (c - '0');
            }
            while ((c = next()) >= '0' && c <= '9');

            if (c == '.') {
                while ((c = next()) >= '0' && c <= '9') {
                    res += (c - '0') / (div *= 10.0);
                }
            }

            if (sign)
                return -res;
            return res;
        }

        public void close() {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}