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:
//package que_b;

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

public class utkarsh {

    InputStream is;
    PrintWriter out;
    
    long mod = (long)(1e9 + 7), inf = (long)(3e18);
    
    class pair {
        int F, S;
        pair(int f, int s) {
            F = f;  S = s;
        }
    }
    
    int col[], d[], ans, cnt[][];
    ArrayList <Integer> a[];
    
    void dfs(int s, int pt) {
        for(int x : a[s]) {
            if(x == pt) continue;
            d[x] = d[s] + 1;
            dfs(x, s);
            cnt[s][0] += cnt[x][0];
            cnt[s][1] += cnt[x][1];
        }
        if(col[s] == 1) cnt[s][0]++;
        if(col[s] == 2) cnt[s][1]++;
    }
    
    pair play(int s, int pt) {
        ArrayList <Integer> dp[] = new ArrayList[2];
        dp[0] = new ArrayList<>();
        dp[1] = new ArrayList<>();
        for(int x : a[s]) {
            if(x == pt) continue;
            pair p = play(x, s);
            if(p != null)   dp[p.S].add(p.F);
        }
        
        if(col[s] > 0) {
            dp[col[s]-1].clear();
            dp[col[s]-1].add(s);
        }
        
        if(dp[0].isEmpty() && dp[1].isEmpty()) {
            return null;
        } else if(dp[0].isEmpty()) {
            if(dp[1].size() == 1)   return new pair(dp[1].get(0), 1);
            return new pair(s, 1);
        } else if(dp[1].isEmpty()) {
            if(dp[0].size() == 1)   return new pair(dp[0].get(0), 0);
            return new pair(s, 0);
        } else {
            int u, v;
            if(dp[0].size() == 1 && dp[1].size() == 1) {
                u = dp[0].get(0);
                v = dp[1].get(0);
            } else if(dp[0].size() == 1) {
                u = dp[0].get(0);
                v = s;
            } else if(dp[1].size() == 1) {
                u = s;
                v = dp[1].get(0);
            } else {
                ans = 0;    return null;
            }
            
                int r = cnt[0][0] - cnt[s][0];
                int b = cnt[0][1] - cnt[s][1];
                if(r > 0 && b > 0) {
                    if(ans == -1)   ans = 0;
                } else if(r > 0) {
                    if(ans == -1)   ans = d[v] - d[s];
                } else if(b > 0) {
                    if(ans == -1)   ans = d[u] - d[s];
                } else {
                    if(ans == -1)   ans = d[v] + d[u] - d[s] - d[s];
                }
            return null;
        }
    }
    
    void solve() {
        //SZ = sieve(); //SZ = 1000001;
        int n = ni();
        col = na(n);
        cnt = new int[n][2];
        d = new int[n];
        a = new ArrayList[n];
        for(int i = 0; i < n; i++)  a[i] = new ArrayList<>();
        for(int i = 0; i < n-1; i++) {
            int u = ni() - 1, v = ni() - 1;
            a[u].add(v);    a[v].add(u);
        }
        ans = -1;
        dfs(0, -1);
        play(0, -1);
        
        out.println(ans);
    }
    
    //--------- Extra Template -----------
    
    int P[], S[], SZ, NP = 15485900;
    boolean isP[];
    
    int sieve() {
        int i, j, n = NP; 
        isP = new boolean[n]; 
        S = new int[n];
        for(i = 3; i < n; i += 2) {
            isP[i] = true;
            S[i-1] = 2;
            S[i] = i;
        }
        isP[2] = true;
        S[1] = 1;   //(UD)
        for(i = 3; i * i <= n; i += 2) {
            if( !isP[i] )   continue;
            for(j = i * i; j < n; j += i) {
                isP[j] = false;
                if(S[j] == j)   S[j] = i;
            }
        }

        P = new int[n];
        P[0] = 2;
        j = 1;
        for(i = 3; i < n; i += 2) {
            if ( isP[i] )   P[j++] = i;
        }
        P = Arrays.copyOf(P, j);
        return j;
    }

    long mp(long b, long e, long mod) {
        b %= mod;
        long r = 1;
        while(e > 0) {
            if((e & 1) == 1) {
                r *= b; r %= mod;
            }
            b *= b; b %= mod;
            e >>= 1;
        }
        return r;
    }

    //---------- I/O Template ----------
    
    public static void main(String[] args) { new utkarsh().run(); }
    void run() { 
        is = System.in; 
        out = new PrintWriter(System.out);
        solve();
        out.flush();
    }
    
    byte input[] = new byte[1024];
    int len = 0, ptr = 0;
    
    int readByte() { 
        if(ptr >= len) { ptr = 0; 
            try { len = is.read(input); } 
            catch(IOException e) { throw new InputMismatchException(); } 
            if(len <= 0) { return -1; } 
        } return input[ptr++];
    }
    boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
    int skip() { 
        int b = readByte(); 
        while(b != -1 && isSpaceChar(b)) { b = readByte(); } 
        return b;
    }
    
    char nc() { return (char)skip(); }
    String ns() { 
        int b = skip(); 
        StringBuilder sb = new StringBuilder(); 
        while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } 
        return sb.toString();
    }
    String nLine() { 
        int b = skip(); 
        StringBuilder sb = new StringBuilder(); 
        while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); } 
        return sb.toString();
    }
    int ni() { 
        int n = 0, b = readByte(); 
        boolean minus = false; 
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } 
        if(b == '-') { minus = true; b = readByte(); } 
        if(b == -1) { return -1; }  //no input 
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } 
        return minus ? -n : n;
    }
    long nl() { 
        long n = 0L;    int b = readByte(); 
        boolean minus = false; 
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } 
        if(b == '-') { minus = true; b = readByte(); } 
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } 
        return minus ? -n : n;
    }

    double nd() { return Double.parseDouble(ns()); }
    float nf() { return Float.parseFloat(ns()); }
    int[] na(int n) { 
        int a[] = new int[n]; 
        for(int i = 0; i < n; i++) { a[i] = ni(); } 
        return a;
    }
    char[] ns(int n) { 
        char c[] = new char[n]; 
        int i, b = skip(); 
        for(i = 0; i < n; i++) { 
            if(isSpaceChar(b)) { break; } 
            c[i] = (char)b; b = readByte(); 
        } return i == n ? c : Arrays.copyOf(c,i);
    }
}