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.util.*;
import java.lang.*;
import java.lang.reflect.Array;
import java.io.*;
import java.math.*;
import java.text.DecimalFormat;
public class Prac{     
    static class InputReader { 
        private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;
 	public InputReader(InputStream st) {
            this.stream = st;
	} 
	public int read() {
            if (snumChars == -1)
	 	throw new InputMismatchException();
            if (curChar >= snumChars) {
		curChar = 0;
                try {
                    snumChars = stream.read(buf);
		} 
                catch (IOException e) {
                    throw new InputMismatchException();
           	}
		if (snumChars <= 0)
                    return -1;
            }
            return buf[curChar++];
	}
        public int ni() {
            int c = read();
            while (isSpaceChar(c)) {
               	c = read();
            }
            int sgn = 1;
            if (c == '-') {
		sgn = -1;
		c = read();
            }
            int res = 0;
            do {
		res *= 10;
		res += c - '0';
		c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
	} 
	public long nl() {
            int c = read();
            while (isSpaceChar(c)) {
            c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
	} 
	public int[] nia(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
		a[i] = ni();
            }
            return a;
	} 
	public String rs() {
            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 String nextLine() {
            int c = read();
            while (isSpaceChar(c))
		c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        } 
	public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}
 
	private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
	} 
    }
    static PrintWriter w = new PrintWriter(System.out);
    
    static class Pair{
        int b,r;
        public Pair(int b,int r){
            this.r=r;
            this.b=b;
        }
    }
    
    static ArrayList<Integer> arr[];
    static int c[];
    static Pair ch[];
    static Pair dfs(int i,int p){
        int r=0,b=0;
        if(c[i]==1)r++;
        else if(c[i]==2) b++;
        Iterator itr=arr[i].iterator();
        while(itr.hasNext()){
            int a=(int)itr.next();
            if(a!=p){
                Pair pa=dfs(a,i);
                b+=pa.b;
                r+=pa.r;
            }
            
        }
        return ch[i]=new Pair(b,r);
    }
    public static void main(String[] args) throws IOException {   
        InputReader sc=new InputReader(System.in);
        int n=sc.ni();
        arr=new ArrayList[n+1];
        c=new int[n+1];
        ch=new Pair[n+1];
        for(int i=1;i<=n;i++){
            c[i]=sc.ni();
            arr[i]=new ArrayList<Integer>();
        }
        for(int i=0;i<n-1;i++){
            int u=sc.ni();
            int v=sc.ni();
            arr[u].add(v);
            arr[v].add(u);
        }
        dfs(1,0);
        //w.println();
//        Queue<Integer> q1=new LinkedList<Integer>();
//        q1.add(1);
//        
//        while(!q1.isEmpty()){
//            int c=q1.size();
//            while(c-->0){
//                int x=q1.poll();
//                Iterator itr=arr[x].iterator();
//                while(itr.hasNext()){
//                    int a=(int)itr.next();
//                    if(!v[a]){
//                        q1.add(a);
//                        ch[a].r+=(ch[x].r-ch[a].r);
//                        ch[a].b+=(ch[x].b-ch[a].b);
//                    }
//                }
//                v[x]=true;
//            }
//        }
//        Arrays.fill(v,false);
        boolean v[]=new boolean[n+1];
        Queue<Integer> q=new LinkedList<Integer>();
        q.add(1);
        int res=0;
        
        while(!q.isEmpty()){
            int co=q.size();
            while(co-->0){
                int x=q.poll();
                Iterator itr=arr[x].iterator();
                while(itr.hasNext()){
                    int a=(int)itr.next();
                    if(!v[a]){
                        q.add(a);
                        Pair p1=ch[a];
                        Pair p2=ch[x];
                        int r1=p2.r-p1.r;
                        int b1=p2.b-p1.b;
                        int r2=p1.r;
                        int b2=p1.b;
                        ch[a].r+=(ch[x].r-ch[a].r);
                        ch[a].b+=(ch[x].b-ch[a].b);
                        if(r1!=0&&b1!=0||r2!=0&&b2!=0){
                            continue;
                        }
                        
                        res++;
                    }
                }
                v[x]=true;
            }
        }
        w.println(res);
        w.close();
    }
}