#include<bits/stdc++.h>
#define int long long
using namespace std;
auto read = [](){
int x;
scanf("%lld", &x);
return x;
};
const int N = 2005;
int n, a[N], d[N], f[N], ans;
vector<int> v[N];
void build(int x, int fa){
for(int i = 0; i < v[x].size(); i ++){
int y = v[x][i];
if(y == fa)
continue;
d[y] = d[x] + 1;
build(y, x);
}
return;
}
void dfs(int x){
f[x] ++;
for(int i = 0; i < v[x].size(); i ++){
int y = v[x][i];
if(d[y] < d[x] || a[y] <= a[x])
continue;
dfs(y);
f[x] += f[y];
}
for(int i = 0; i < v[x].size(); i ++){
int y = v[x][i];
if(d[y] < d[x] || a[y] <= a[x])
continue;
ans += f[y];
for(int j = i + 1; j < v[x].size(); j ++){
int z = v[x][j];
if(d[z] < d[x] || a[z] <= a[x])
continue;
ans += f[y] * f[z];
}
}
return;
}
signed main(){
freopen("touch.in", "r", stdin);
freopen("touch.out", "w", stdout);
n = read();
for(int i = 1; i <= n; i ++)
a[i] = read();
for(int i = 1, x, y; i < n; i ++){
x = read(), y = read();
v[x].push_back(y), v[y].push_back(x);
}
build(1, 0);
for(int i = 1; i <= n; i ++){
if(f[i] == 0)
dfs(i);
}
printf("%lld", ans);
return 0;
}