#include<bits/stdc++.h>
using namespace std;

long long n, l,val[200005],u,v,ans,cont,upd[200005],h[200005];
map<long long,vector<int> > opz;
map<int,bool> us;
vector<vector<int>> adj;

int timer;
vector<int> tin, tout;
vector<vector<int>> up;

int prof;
void dfs(int v, int p)
{
	
    tin[v] = ++timer;
    up[v][0] = p;
    for (int i = 1; i <= l; ++i)
        up[v][i] = up[up[v][i-1]][i-1];

	
    for (int u : adj[v]) {
        if (u != p)
            dfs(u, v);
    }
    h[v]=prof;
	prof++;
    tout[v] = ++timer;
}

bool is_ancestor(int u, int v)
{
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}

int lca(int u, int v)
{
    if (is_ancestor(u, v))
        return u;
    if (is_ancestor(v, u))
        return v;
    for (int i = l; i >= 0; --i) {
        if (!is_ancestor(up[u][i], v))
            u = up[u][i];
    }
    return up[u][0];
}

void preprocess(int root) {
    tin.resize(n+1);
    tout.resize(n+1);
    timer = 0;
    l = ceil(log2(n+1));
    up.assign(n+1, vector<int>(l + 1));
    dfs(root, root);
}

bool comp(int x,int y){
	return h[x]<h[y];
}

void calc(int nodo,int last,int num){
	if(upd[nodo]==1)return;
	if(upd[nodo]==-1)num--;
	if(num==0)ans++;
	
	for(auto i : adj[nodo]){
		if(i!=last){
			calc(i,nodo,num);
		}
	}	
}

int main(){

	//ios::sync_with_stdio(false);
	//cin.tie(0);

	cin>>n;
	
	vector<int> tmp;
	for(int i=0;i<=n;i++)adj.push_back(tmp);
	
	for(int i=1;i<=n;i++){
		cin>>val[i];
		opz[val[i]].push_back(i);
	}
	
	for(int i=0;i<n-1;i++){
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	
	preprocess(1);
	
	for(auto i : opz){
		
		tmp=i.second;
		sort(tmp.begin(),tmp.end(),comp);
		
		if(tmp.size()==1)continue;
		
		us.clear();
		int lc=tmp[0];
		us[lc]=true;
		
		for(int j=1;j<tmp.size();j++){
			if(lc==tmp[j]){
				cout<<0<<"\n";
				return 0;
			}
			lc=lca(lc,tmp[j]);
			us[tmp[j]]=true;
			//cout<<tmp[j]<<" ecco\n";
			if(us[lc]==true && j+1<tmp.size()){
				cout<<0<<"\n";
				return 0;
			}
		}
		
		//cout<<i.first<<" vale "<<lc<<"\n";
		
		if(us[lc]==false){
			for(auto j : tmp)upd[j]=1;
		}else{
			cont++;
						
			int lc2=-1;
			
			for(auto j : tmp){
				if(j!=lc){
					if(lc2==-1)lc2=j;
					else lc2=lca(lc2,j);
					upd[j]=1;
				}
			}
			
			for(auto j : adj[lc]){
				if(lca(lc,j)==lc && lca(lc2,j)==j){
					if(upd[j]!=1)
					upd[j]=-1;
					break;
				}
			}
		}
	}
	
	calc(1,1,cont);
	cout<<ans<<"\n";	

	return 0;
}

