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.IOException;
import java.io.InputStream;
import java.util.*;
public class sol {
    static long mod=(long)Math.pow(10,9)+7;
    static StringBuilder sb = new StringBuilder();
    static class Pair { 
        int a; 
        int b;  
        Pair(int x, int y) { 
            a = x; 
            b = y; 
        } 
    }
    static Pair color[];
    static ArrayList<Integer> tree[];
    static boolean vis[];
    static int ar[];
    static void dfs(int x) {
        vis[x] = true;
        color[x]=new Pair(ar[x]==1 ? 1 : 0, ar[x]==2 ? 1 : 0);
        for(Integer i : tree[x]) {
            if(!vis[i]) {
                dfs(i);
                color[x].a+=color[i].a;
                color[x].b+=color[i].b;
            }
        }
    }
    public static void main(String args[])throws Exception {
        FastReader in = new FastReader(System.in); 
        int t=1,i,j;
        while(t-->0) { 
            int n = in.nextInt(),r=0,b=0;
            color = new Pair[n];
            ar = new int[n];
            tree = new ArrayList[n];
            vis = new boolean[n];
            for(i=0;i<n;i++) {
                tree[i] = new ArrayList<>();
                ar[i] = in.nextInt();
                if(ar[i] == 1)
                    r++;
                if(ar[i] == 2)
                    b++;
            }
            for(i=1;i<n;i++) {
                int x = in.nextInt()-1,y = in.nextInt()-1;
                tree[x].add(y);
                tree[y].add(x);
            }
            dfs(0);
            int ans=0;
            for(i=1;i<n;i++) {
                int r1=r-color[i].a;
                int b1=b-color[i].b;
                if((r1>0 && b1>0) || (color[i].a>0 && color[i].b>0))
                    continue;
                ans++;
            }
            System.out.println(ans);
        }
    }
    static long power(long a,long b,long p) {
        long res=1;
        a = a % p;  
        while (b > 0) { 
            if((b & 1)==1) 
                res = (res * a) % p;
            b = b >> 1;  
            a = (a * a) % p;  
        } 
        return res; 
    }
    static void prt(int a[]) {
        for(int i=0;i<=a.length;i++)
            System.out.print(a[i]+" ");
        System.out.println();
    }
}
class FastReader {
    byte[] buf = new byte[2048];
    int index, total;
    InputStream in;
    FastReader(InputStream is) {
        in = is;
    }
    int scan() throws IOException {
        if (index >= total) {
            index = 0;
            total = in.read(buf);
            if (total <= 0) {
                return -1;
            }
        }
        return buf[index++];
    }
    String next() throws IOException {
        int c;
        for (c = scan(); c <= 32; c = scan()) ;
        StringBuilder sb = new StringBuilder();
        for (; c > 32; c = scan()) {
            sb.append((char) c);
        }
        return sb.toString();
    }
    String nextLine() throws IOException {
        int c;
        for (c = scan(); c <= 32; c = scan()) ;
        StringBuilder sb = new StringBuilder();
        for (; c != 10 && c != 13; c = scan()) {
            sb.append((char) c);
        }
        return sb.toString();
    }
    char nextChar() throws IOException {
        int c;
        for (c = scan(); c <= 32; c = scan()) ;
        return (char) c;
    }
    int nextInt() throws IOException {
        int c, val = 0;
        for (c = scan(); c <= 32; c = scan()) ;
        boolean neg = c == '-';
        if (c == '-' || c == '+') {
            c = scan();
        }
        for (; c >= '0' && c <= '9'; c = scan()) {
            val = (val << 3) + (val << 1) + (c & 15);
        }
        return neg ? -val : val;
    }
    long nextLong() throws IOException {
        int c;
        long val = 0;
        for (c = scan(); c <= 32; c = scan()) ;
        boolean neg = c == '-';
        if (c == '-' || c == '+') {
            c = scan();
        }
        for (; c >= '0' && c <= '9'; c = scan()) {
            val = (val << 3) + (val << 1) + (c & 15);
        }
        return neg ? -val : val;
    }
}
/*static void merge(int arr[], int l, int m, int r) {
    int n1 = m - l + 1; 
    int n2 = r - m; 
    int L[] = new int [n1]; 
    int R[] = new int [n2]; 
    for (int i=0; i<n1; ++i) 
        L[i] = arr[l + i]; 
    for (int j=0; j<n2; ++j) 
        R[j] = arr[m + 1+ j]; 
    int i = 0, j = 0; 
    int k = l; 
    while (i < n1 && j < n2) { 
        if (L[i] <= R[j]) { 
            arr[k] = L[i]; 
            i++; 
        } 
        else { 
            arr[k] = R[j]; 
            j++; 
        } 
        k++; 
    } 
    while (i < n1) { 
        arr[k] = L[i]; 
        i++; 
        k++; 
    } 
    while (j < n2) { 
        arr[k] = R[j]; 
        j++; 
        k++; 
    } 
}  
static void sort(int arr[], int l, int r) { 
    if (l < r) {
        int m = (l+r)/2; 
        sort(arr, l, m); 
        sort(arr , m+1, r); 
        merge(arr, l, m, r); 
    } 
} */
/*static int gcd(int a, int b) { 
    if (b == 0) 
        return a; 
    return gcd(b, a % b);  
}*/