#include<bits/stdc++.h>
using namespace std;
long long x,y,n,zong=0;
bool vis[2005];
struct node{
long long w,f,lu;
vector<long long> ez;
}tree[2005];
vector<long long> g[2005];
void build(long long p){
vis[p]=1;
for(long long i=0;i<g[p].size();i++){
long long xin=g[p][i];
if(!vis[g[p][i]])
{
tree[xin].f=p;
tree[p].ez.push_back(xin);
build(g[p][i]);
}
}
}
void dfs1(long long p){
tree[p].lu=1;
for(long long i=0;i<tree[p].ez.size();i++){
dfs1(tree[p].ez[i]);
if(tree[tree[p].ez[i]].w>tree[p].w) tree[p].lu+=tree[tree[p].ez[i]].lu;
}
}
void suan(long long p){
zong+=tree[p].lu-1;
for(long long i=0;i<tree[p].ez.size();i++){
long long tmp1=tree[p].ez[i];
if(tree[tmp1].w>tree[p].w){
for(long long j=i+1;j<tree[p].ez.size();j++){
long long tmp2=tree[p].ez[j];
if(tree[tmp2].w>tree[p].w){
zong+=tree[tmp1].lu*tree[tmp2].lu;
}
}
}
}
}
int main(){
freopen("touch.in","r",stdin);
freopen("touch.out","w",stdout);
scanf("%lld",&n);
for(long long i=1;i<=n;i++){
scanf("%lld",&tree[i].w);
}
for(long long i=1;i<n;i++){
scanf("%lld%lld",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
vis[1]=1;
build(1);
dfs1(1);
for(long long i=1;i<=n;i++) suan(i);
//for(long long i=1;i<=n;i++) cout<<tree[i].lu<<' ';
printf("%lld",zong);
return 0;
}