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.
//package year2019.month01.cf.round540div3;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.LinkedList;

public class round540div3F1 {

    boolean ONLINE_JUDGE = (System.getProperty("ONLINE_JUDGE") != null);

    public round540div3F1() throws IOException {
        InputReader in;
        if (ONLINE_JUDGE) {
            in = new InputReader(System.in);
        } else {
            in = new InputReader(new FileInputStream("/Users/karj/cp/input.txt"));
        }
        PrintWriter out = new PrintWriter(System.out);

        int T = 1;
        for (int caseNo = 1; caseNo <= T; caseNo++) {
            solve(caseNo, in, out);
        }

        out.close();
    }


    int a[], u[], v[], h[];
    LinkedList<Integer> tree[];
    int cr[], cb[];
    int totalr = 0, totalb = 0;
    boolean vis[];
    private void solve(int caseNo, InputReader in, PrintWriter out) throws IOException {
        int n = in.nextInt();
        a = in.nextIntArray(n);
        debug(a);
        u = new int[n];
        v = new int[n];
        h = new int[n];
        cr = new int[n];
        cb = new int[n];
        tree = new LinkedList[n];
        vis = new boolean[n];
        for (int i = 0; i < n; i++) {
            tree[i] = new LinkedList<>();
        }
        for (int i = 0; i < n - 1; i++) {
            u[i] = in.nextInt() - 1;
            v[i] = in.nextInt() - 1;
            tree[u[i]].addLast(v[i]);
            tree[v[i]].addLast(u[i]);
        }
        dfs(0);
        debug(cr);
        debug(cb);
        debug(h);
        totalb = cb[0];
        totalr = cr[0];
        int nice = 0;
        for (int i = 0; i < n - 1; i++) {
            int uu = u[i], vv = v[i];
            if (h[u[i]] > h[v[i]]) {
                int tt = uu;
                uu = vv;
                vv = tt;
            }
            int crv = totalr - cr[uu];
            int cbv = totalb - cb[uu];
            debug(uu, vv, cr[uu], cb[uu], crv, cbv);
            if ((cr[uu] > 0 && cb[uu] > 0) ||
                    (crv > 0 && cbv > 0)) {

            } else {
                nice++;
            }
        }
        out.println(nice);
    }

    private void dfs(int uu) {
        vis[uu] = true;
        if (tree[uu].size() == 1 && vis[tree[uu].peekFirst()]) {
            if (a[uu] == 0) {

            } else if (a[uu] == 1) {
                cr[uu] = 1;
            } else if (a[uu] == 2) {
                cb[uu] = 1;
            }
            h[uu] = 1;
        }
        for (int vv : tree[uu]) {
            if (!vis[vv]) {
                dfs(vv);
                cr[uu] += cr[vv];
                cb[uu] += cb[vv];
                h[uu] = Math.max(h[uu], h[vv] + 1);
            }
        }
        if (a[uu] == 1) {
            cr[uu]++;
        } else if (a[uu] == 2) {
            cb[uu]++;
        }
    }

    static class InputReader {

        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public interface SpaceCharFilter {

            public boolean isSpaceChar(int ch);
        }

        public String next() {
            return nextString();
        }

        public String nextString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public Long nextLong() {
            return Long.parseLong(nextString());
        }

        public Double nextDouble() {
            return Double.parseDouble(nextString());
        }

        public char nextCharacter() {
            return nextString().charAt(0);
        }

        public int[] nextIntArray(int N) {
            int A[] = new int[N];
            for (int i = 0; i < N; i++) {
                A[i] = nextInt();
            }
            return A;
        }

        public long[] nextLongArray(int N) {
            long A[] = new long[N];
            for (int i = 0; i < N; i++) {
                A[i] = nextLong();
            }
            return A;
        }

        public double[] nextDoubleArray(int N) {
            double A[] = new double[N];
            for (int i = 0; i < N; i++) {
                A[i] = nextDouble();
            }
            return A;
        }
    }

    private void debug(Object... o) {
        if (ONLINE_JUDGE) {
            return;
        }
        System.out.println(Arrays.deepToString(o));
    }

    public static void main(String args[]) throws IOException {
        new round540div3F1();
    }
}
