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:


/*
USER: caoash3
LANG: JAVA
TASK: 
*/

import java.io.*;
import java.util.*;


public class a {

	static final boolean stdin = true;
	static final String filename = "";
	static FastScanner br;
	static PrintWriter pw;

	public static void main(String[] args) throws IOException {

		if (stdin) {
			br = new FastScanner();
			pw = new PrintWriter(new OutputStreamWriter(System.out));
		}

		else {
			br = new FastScanner(filename + ".in");
			pw = new PrintWriter(new FileWriter(filename + ".out"));
		}

		X solver = new X();
		solver.solve(br, pw);
	}

	static class X {
		static int N;
		static int[] c, dsr, dsb, usr, usb, p;
		static boolean[] vis;
		static LinkedList<Integer>[] edges;
		public void solve(FastScanner br, PrintWriter pw) throws IOException {
			
			N = br.nextInt();
			
			c = new int[N];
			vis = new boolean[N];
			
			dsr = new int[N];
			usr = new int[N];
			dsb = new int[N];
			usb = new int[N];
			p = new int[N];
			for(int i = 0; i < N; i++) {
				c[i] = br.nextInt();
			}
		
			edges = new LinkedList[N];
			
			for(int i = 0; i < edges.length; i++) {
				edges[i] = new LinkedList<Integer>();
			}
			
			for(int i = 0; i < N-1; i++) {
				int x = br.nextInt();
				int y = br.nextInt();
				
				x--;
				y--;
				
				edges[x].add(y);
				edges[y].add(x);
			}
			
			dfs1(0,-1);
			dfs2(0,-1);
			
			Queue<Integer> bfs = new LinkedList<Integer>();
			
			boolean[] vis = new boolean[N];
			
			bfs.add(0);
			
			while(!bfs.isEmpty()) {
				int curr = bfs.poll();
				if(vis[curr]) {
					continue;
				}
				if(curr == 0) {
					usr[curr] = 0;
					usb[curr] = 0;
				}
				else {
					usr[curr] = (dsr[p[curr]] - dsr[curr]) + usr[p[curr]];
					usb[curr] = (dsb[p[curr]] - dsb[curr]) + usb[p[curr]];
				}
				
				
				for(Integer e : edges[curr]) {
					if(vis[e]) {
						continue;
					}
					
					bfs.add(e);
				}
				
				vis[curr] = true;
			}
			
			
			int ans = 0;
			
			for(int i = 0; i < N; i++) {
				if((usr[i] == 0 || usb[i] == 0) && (dsr[i] == 0 || dsb[i] == 0)) {
					ans++;
				}
			}
			
			pw.println(ans);
			
			pw.close();
		}
		
		public static class State{
			int s1;
			int s2;
			
			public State(int s1, int s2) {
				this.s1 = s1;
				this.s2 = s2;
			}
		}
		
		public static int dfs1(int u, int pV) {
			int sum = dsr[u]+ (c[u] == 1 ? 1 : 0);
			
//			pw.println("Node: " + u);
//			System.out.println(u + " " + pV);
			for(Integer e : edges[u]) {
				if(e == pV) continue;
//				pw.print("Edges: ");
//				pw.print(e + " ");
				sum += dfs1(e, u);
			}
			
			dsr[u] = sum;
			p[u] = pV;
			return dsr[u];
		}
		
		public static int dfs2(int u, int pV) {
			int sum = dsb[u] + (c[u] == 2 ? 1 : 0);
//			System.out.println(u + " " + pV);
			for(Integer e : edges[u]) {
				if(e == pV) continue;
				sum += dfs2(e, u);
			}
			
			dsb[u] = sum;
			return dsb[u];
		}


	}

	//fastscanner class
	public static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(String s) {
			try {
				br = new BufferedReader(new FileReader(s));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		public FastScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String nextToken() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(nextToken());
		}

		long nextLong() {
			return Long.parseLong(nextToken());
		}

		double nextDouble() {
			return Double.parseDouble(nextToken());
		}
	}

}