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.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.stream.IntStream;

public class fLarge {
/*
19 4
1 0 2 0 0 0 0 3 0 4 0 0 1 3 4 0 0 0 0
9 2
2 3
2 11
11 1
11 18
18 17
18 13
13 4
17 12
17 16
17 19
19 8
8 14
11 5
5 10
10 15
10 6
6 7

--> 12


5 2
1 0 0 0 2
1 2
2 3
3 4
4 5

 */
	static class Solver {

		int N, K, col[], croot[], fixed[];
		ArrayList<Integer> adj[], colors[];
		
		@SuppressWarnings("unchecked")
		void solve(FastScanner s, PrintWriter out) {
			N = s.nextInt(); K = s.nextInt(); col = s.nextIntArray(N);
			Arrays.setAll(adj = new ArrayList[N], x -> new ArrayList<>());
			Arrays.setAll(colors = new ArrayList[K], x -> new ArrayList<>());
			for (int i = 0; i < N; i++) if(--col[i] != -1) colors[col[i]].add(i); 
			
			for(int u, v, i = 1; i < N; i++) {
				u = s.nextInt() - 1; v = s.nextInt() - 1;
				adj[u].add(v); adj[v].add(u);
			}
			
			dep = new int[N]; nxt = new int[N]; par = new int[N]; gc = new int[N]; sz = new int[N]; in = new int[N]; oot = new int[N];
			par[0] = -1; rdfs(0, 0); hldfs(0); // dab dab dab
			
			croot = new int[K]; Arrays.fill(fixed = new int[N], -1);
			for (int i = 0; i < K; i++)
				fixed[croot[i] = colors[i].stream().reduce((x, y) -> lca(x, y)).get()] = i;
			
			for(int i = 0; i < K; i++)
				for(int x : colors[i])
					if(!bubble(x, i)) { out.println(0); return; }
			
			// trim the stupid leaves smh
			ArrayDeque<Integer> q = new ArrayDeque<>();
			int[] deg = IntStream.range(0, N).map(x -> adj[x].size()).toArray();
			for(int i = 0; i < N; i++) if(fixed[i] == -1 && deg[i] == 1) q.add(i);
			
			while(!q.isEmpty()) {
				int v = q.poll(), to = -1;
				fixed[v] = -2;
				for(int x : adj[v]) if(fixed[x] != -2) to = x;
				if(--deg[to] == 1 && fixed[to] == -1)
					q.add(to);
			}
			
			long ways = 1; memo = new long[2][N];
			for(long[] a : memo) Arrays.fill(a, -1);
			for(int i = 0; i < N; i++)
				if(fixed[i] == -1)
					ways = mul(ways, dp(i, 0, -1));
			
			out.println(ways);
		}
		
		long memo[][];
		// ways to assign the vertices of this tree to leaves
		// back: 1 if we can send this vertex to the group of its parent
		//       0 if we ARE the assigned group of our parent
		long dp(int v, int back, int par) {
			if(fixed[v] >= 0) return 1;
			if(memo[back][v] != -1) return memo[back][v];
			fixed[v] = -3;
			long P = 1, k;
			for(int c : adj[v]) if(c != par && fixed[c] != -2) {
				P = mul(P, dp(c, 1, v));
			}
			long tot = back * P;
			for(int c : adj[v]) if(c != par && fixed[c] != -2) {
				k = minv(dp(c, 1, v));
				k = mul(k, mul(P, dp(c, 0, v)));
				tot = add(tot, k);
			}
			return memo[back][v] = tot;									
		}
		
		boolean bubble(int v, int c) {
			if(fixed[v] != -1) return fixed[v] == c;
			fixed[v] = c; return bubble(par[v], c);
		}

		int lca(int u, int v) {
			if(nxt[u] == nxt[v]) return dep[u] < dep[v] ? u : v;
			if(dep[nxt[u]] > dep[nxt[v]]) return lca(par[nxt[u]], v);
			return lca(u, par[nxt[v]]);
		}

		int dep[], nxt[], par[], gc[], sz[], in[], oot[], IDX = 1;
		
		void rdfs(int v, int d) {
			dep[v] = d; sz[v] = 1; gc[v] = -1;
			for (int c : adj[v]) {
				if (c == par[v]) continue;
				par[c] = v; rdfs(c, d + 1);
				sz[v] += sz[c];
				gc[v] = (gc[v] == -1 || sz[gc[v]] < sz[c]) ? c : gc[v];
			}
		}
		
		/* nxt: chain parent
		 * dep: node depth
		 * par: parent of node
		 *  gc: largest child
		 *  sz: nodes in subtree
		 *  in: vtx preorder number
		 * out: subtree max preorder
		 */
		void hldfs(int v) {
			in[v] = IDX++;
			if(gc[v] != -1) { nxt[gc[v]] = nxt[v]; hldfs(gc[v]); }
			for (int c : adj[v])
				if (c != gc[v] && c != par[v])
					hldfs(nxt[c] = c);
			oot[v] = IDX - 1;
		}
		
		long MOD = 998_244_353;
		long mul(long a, long b) { return (a * b) % MOD; }
		long add(long a, long b) { return (a + b) % MOD; }
		long minv(long x) { return pow(x, MOD - 2); }
		long pow(long b, long e) {
			if(e == 0) return 1;
			long sq = pow(b, e >> 1); sq = mul(sq, sq);
			return (e & 1) == 0 ? sq : mul(sq, b);
		}

	}

	public static void main(String[] args) {
		FastScanner s = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		new Solver().solve(s, out);
		out.close();
		s.close();
	}

	static class FastScanner {
		private InputStream stream;
		private byte[] buf = new byte[1 << 22];
		private int curChar;
		private int numChars;

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

		public FastScanner(File f) throws FileNotFoundException {
			this(new FileInputStream(f));
		}

		public FastScanner(String s) {
			this.stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
		}

		void close() {
			try {
				stream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		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++];
		}

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

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public String next() {
			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 (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}

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

		public int[][] next2DIntArray(int N, int M) {
			int[][] ret = new int[N][];
			for (int i = 0; i < N; i++)
				ret[i] = this.nextIntArray(M);
			return ret;
		}

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

		public long[][] next2DLongArray(int N, int M) {
			long[][] ret = new long[N][];
			for (int i = 0; i < N; i++)
				ret[i] = nextLongArray(M);
			return ret;
		}

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

		public double[][] next2DDoubleArray(int N, int M) {
			double[][] ret = new double[N][];
			for (int i = 0; i < N; i++)
				ret[i] = this.nextDoubleArray(M);
			return ret;
		}

	}

}