#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=(a);i<(n);i++)
#define per(i,a,n) for (int i=(n)-1;i>=(a);i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) (int)x.size()
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
int getint() {
int x=0;
char ch;
do{ch=getchar();}while(ch <= ' ');
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x;
}
template<typename T1,typename T2>bool umin(T1 &x,const T2&y){if(x>y)return x=y,true;return false;}
template<typename T1,typename T2>bool umax(T1 &x,const T2&y){if(x<y)return x=y,true;return false;}
const int maxn=(int)3e5+10;
const int maxm=(int)8e3;
const int inf=(int)1e9+5;
const int mod=(int)1e9+9;
const ll llinf=(ll)1e18+5;
const ld pi=acos(-1.0);
int a[maxn];
vi g[maxn];
int num[maxn];
int cur;
int cnt[maxn];
map<pair<int,vi>,int>have;
void dfs(int v,int par=-1){
vi c;
rep(i,0,SZ(g[v])){
int to=g[v][i];
if(to==par)continue;
dfs(to,v);
c.pb(num[to]);
}
sort(all(c));
if(!have.count(mp(a[v],c))){
have[mp(a[v],c)]=++cur;
}
num[v]=have[mp(a[v],c)];
cnt[num[v]]++;
}
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(0);
int n;
cin>>n;
rep(i,0,n-1){
int x,y;
cin>>x>>y;
g[x].pb(y);
g[y].pb(x);
}
rep(i,1,n+1)cin>>a[i];
dfs(1,-1);
ll res=0;
rep(i,0,maxn){
ll x=cnt[i];
res+=1LL*x*(x-1)/2;
}
cout<<res<<endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiAgIAp1c2luZyBuYW1lc3BhY2Ugc3RkOwogICAKI2RlZmluZSByZXAoaSxhLG4pIGZvciAoaW50IGk9KGEpO2k8KG4pO2krKykKI2RlZmluZS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