#include<bits/stdc++.h>
using namespace std;
const int N = 1000010;
const double EPS = 1e-9;
typedef long long ll;
typedef long long int lli;
typedef long double ld;
typedef pair < int , int > pii;
typedef vector < pii > vii;
typedef set < int > si;
typedef map < int , int > mi;
typedef unordered_map < int , int > omi;
const int MOD = 236;
const int INF = 99999999;
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define forn(i,n) for(int i = 0; i < n; ++i)
#define foer(i,n) for(int i = 1; i <= n; ++i)
#define PI 3.1415926535897932384
template<class T> inline void normmod(T &x, T m) { x %= m; if (x < 0) x += m; }
#if INTPTR_MAX == INT32_MAX or !defined(__SIZEOF_INT128__)
inline ll mulmod(ll x, ll n, ll m){ ll r = 0; normmod(x, m); normmod(n, m); while (n) { if (n & 1) r += x; x += x; if (r >= m) r -= m; if (x >= m) x -= m; n /= 2; } return r; }
#else
using int128 = __int128;
inline ll mulmod(ll x, ll n, ll m){ return __int128(x) * n % m; }
#endif
inline ll powmod(ll x, ll n, ll m){ ll r = 1; normmod(x, m); while (n){ if (n & 1) r *= x; x *= x; r %= m; x %= m; n /= 2; }return r; }
inline ll powmulmod(ll x, ll n, ll m) { ll res = 1; normmod(x, m); while (n){ if (n & 1)res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; }
template<class T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; }
template<class T> bool isPrime(T x) { if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;
    for (T i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; }
bool millerRabin(long long n) {
    if (n <= 1000) return isPrime(n);
    long long s = n - 1; int t = 0; while (s % 2 == 0) s /= 2, ++t;
    for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!(a %= n)) return true;
        long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue;
        for (int i = 1; i < t; ++i) if ((f = mulmod(f, f, n)) == n - 1) goto nextp;
        return false; nextp:;
    } return true;
}
vector < int > g[N];
pair < int , int > a[N];
int sz[N],p[N],vis[N],n;
lli ans1,ans2;
void make_set(int x)
{
    p[x] = x;
    sz[x] = 1;
}
int get(int x)
{
    return x == p[x] ? x : p[x] = get(p[x]);
}
void solve(int x, int val, lli &ans)
{
    lli wt = 1;
    vis[x] = 1;
    for(auto u:g[x])
    {
        if(!vis[u]) continue;
        int y = get(u);
        wt+= (long long int)sz[x]*sz[y];
        sz[x] += sz[y];
        p[y] = x;
    }
    ans += wt*val;
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n;
    forn(i,n){cin >> a[i].first; a[i].second = i;}
    forn(i,n-1)
    {
        int u,v;
        cin >> u >> v;
        g[u-1].pb(v-1), g[v-1].pb(u-1);
    }
    sort(a,a+n);
    for(int i = 0; i < n; ++i) make_set(i);
    for(int i = 0; i < n; ++i)
    {
        solve(a[i].second,a[i].first,ans1);
    }
    for(int i = 0; i < n; ++i) {make_set(i); vis[i] = 0;}
    for(int i = n-1; i >= 0; --i)
    {
        solve(a[i].second,a[i].first,ans2);
    }
    lli anss = ans1 - ans2;
    cout << anss << endl;
    return 0;
}