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.text.*;
import java.util.*;
import java.math.*;
public class template {
	public static void main(String[] args) throws Exception {
		new template().run();
	}
	public void run() throws Exception {
		FastScanner f = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		int n = f.nextInt(), k= 2;
		Node[] arr = new Node[n];
		for(int i = 0; i < n; i++)
			arr[i] = new Node(i,f.nextInt());
		for(int i = 0; i < n-1; i++) {
			int a= f.nextInt()-1, b = f.nextInt()-1;
			arr[a].child.add(arr[b]);
			arr[b].child.add(arr[a]);
		}
		Node[] colors = new Node[k+1];
		arr[0].pre(null);
		for(Node no : arr) {
			if(no.c == 0) continue;
			Node a = colors[no.c];
			if(a == null) colors[no.c] = no;
			else colors[no.c] = LCA(a, no);
		}
		for(int i = 1; i <= k; i++)
			colors[i].endColor = i;
		hasAns = true;
		LinkedList<Node> ll = new LinkedList<>();
		for(Node no : arr)
			if(no.child.size() == 0) ll.add(no);
		while(hasAns && !ll.isEmpty()) {
			Node no = ll.removeFirst();
			no.dp();
			no = no.anc[0];
			if(no == null) continue;
			if(++no.childCnt == no.child.size()) ll.addLast(no);
		}
		if(hasAns)
			out.println(arr[0]._hasntEnd);
		else
			out.println(0);
		out.flush();
	}
	boolean hasAns;
	public Node LCA(Node a, Node b) {
		if(a.d < b.d) {
			Node temp = a;
			a = b;
			b = temp;
		}
		for(int i = 19; i >= 0; i--)
			if(a.anc[i] != null && a.anc[i].d >= b.d) 
				a = a.anc[i];
		if(a == b) return a;
		for(int i = 19; i >= 0; i--)
			if(a.anc[i] != null && b.anc[i] != null && a.anc[i] != b.anc[i]) {
				a = a.anc[i];
				b = b.anc[i];
			}
		return a.anc[0];
	} 
	class Node {
		Node[] anc;
		int i, c, d;
		int endColor, retColor, childCnt;
		long _didEnd, _hasntEnd;
		LinkedList<Node> child;
		public Node(int i, int c) {
			this.i = i;
			this.c = c;
			child = new LinkedList<Node>();
		}
		public void pre(Node n) {
			anc = new Node[20];
			d = (n == null ? 0 : n.d + 1);
			anc[0] = n;
			for(int i = 1; i < 20; i++)
				if(anc[i-1] != null)
					anc[i] = anc[i-1].anc[i-1];
				else
					break;
			child.remove(n);
			for(Node c : child)
				c.pre(this);
		}
		public String toString() {
			return ""+i;
		}
		public void dp() {
			if(!hasAns) return;
			if(child.size() == 0) {
				if(endColor != 0) _hasntEnd = 1;
				else _didEnd = 1;
				//System.out.println("Node " + i + " ended:" + _didEnd + " hasnt:" + _hasntEnd + " " + endColor);
				if(endColor != 0) retColor = 0;
				else retColor = c;
			} else {
				long totalprod = 1;
				int propColor = c;
				for(Node c : child) {
					int color = c.retColor;
					if(propColor != 0 && color != 0 && propColor != color) hasAns = false;
					else if(propColor == 0) propColor = color;
					totalprod = totalprod * (c._didEnd + c._hasntEnd) % MOD;
				}
				if(endColor != 0 && propColor != endColor) hasAns = false;
				if(propColor == 0) {
					_didEnd = totalprod;
					for(Node c : child) {
						_hasntEnd = (_hasntEnd + (totalprod * modInv(c._didEnd + c._hasntEnd)) % MOD * c._hasntEnd % MOD) % MOD;
					}
				} else _hasntEnd = totalprod;
				//System.out.println("Node " + i + " ended:" + _didEnd + " hasnt:" + _hasntEnd + " " + endColor + " " + propColor);
				if(propColor == endColor) retColor = 0;
				else retColor = propColor;
			}
			
		}
	}
	long x, y, MOD = 998_244_353;
	public long modInv(long val) {
		gcd(val, MOD);
		return (x % MOD + MOD) % MOD;
	}
	public void gcd(long a, long b) {
		if(a * b == 0) {
			x = 0;
			y = 1;
			return;
		}
		gcd(b%a,a);
		long nx = y - b/a * x;
		long ny = x;
		x = nx;
		y = ny;
	}
    static class FastScanner {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        public FastScanner() {
            reader = new BufferedReader(new InputStreamReader(System.in), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }
        public long nextLong() {
        	return Long.parseLong(next());
        }
        public double nextDouble() {
        	return Double.parseDouble(next());
        }
        public String nextLine() {
        	try {
        		return reader.readLine();
        	} catch(IOException e) {
        		throw new RuntimeException(e);
        	}
        }
    }
}