#include <bits/stdc++.h>
using namespace std;
template <typename X>
void MA(X &a, X b) {
  a = max(a, b);
}
template <typename X>
void MI(X &a, X b) {
  a = min(a, b);
}
long long DEBUG = 0, MULTI = 0, GCJ = 0;
struct solver_t;
solver_t *solver;
struct solver_t {
  vector<long long> A;
  vector<vector<long long> > EDG;
  struct lcu_t {
    long long N;
    vector<long long> CNT, PAR;
    lcu_t(long long N) : N(N) {
      PAR.resize(N);
      CNT.resize(N);
      for (int64_t i = (0); i < (N); ++i) PAR[i] = i;
      for (int64_t i = (0); i < (N); ++i) CNT[i] = 1;
    }
    long long parent(long long i) {
      if (PAR[i] == i)
        return i;
      else
        return PAR[i] = parent(PAR[i]);
    }
    long long get_cnt(int64_t x) { return x * (x - 1) / 2; }
    int64_t join(long long a, long long b) {
      long long pa = parent(a), pb = parent(b);
      if (pa == pb) return 0;
      if (!(CNT[pb] >= CNT[pa])) swap(pa, pb);
      int64_t diff = -get_cnt(CNT[pa]) - get_cnt(CNT[pb]);
      PAR[pa] = pb;
      CNT[pb] += CNT[pa];
      diff += get_cnt(CNT[pb]);
      return diff;
    }
  };
  void solve() {
    long long N;
    cin >> N;
    A.resize(N + 1);
    for (int64_t i = (1); i <= (N); ++i) cin >> A[i];
    EDG.resize(N + 1);
    for (int64_t i = (0); i < (N - 1); ++i) {
      long long X, Y;
      cin >> X >> Y;
      EDG[X].push_back(Y);
      EDG[Y].push_back(X);
    }
    vector<array<int64_t, 2> > order;
    for (int64_t i = (1); i <= (N); ++i) order.push_back({A[i], i});
    sort((order).begin(), (order).end());
    int64_t res[2] = {};
    for (int64_t mode = (0); mode < (2); ++mode) {
      lcu_t lcu(N + 1);
      int64_t prev_cnt = 0;
      int64_t &sum = res[mode];
      vector<long long> vis(N + 1);
      for (long long i = 0; i < (long long)order.size();) {
        int64_t diff = 0;
        long long j;
        {
          if (DEBUG)
            cerr << 82 << " "
                 << "\"\""
                 << " "
                 << "" << endl;
        };
        for (j = i; j < (long long)order.size(); j++) {
          if (order[i][0] != order[j][0]) break;
          long long u = order[j][1];
          vis[u] = 1;
          {
            if (DEBUG)
              cerr << 87 << " "
                   << "\"vis \" << u"
                   << " "
                   << "vis " << u << endl;
          };
          for (long long v : EDG[u])
            if (vis[v]) {
              diff += lcu.join(u, v);
              {
                if (DEBUG)
                  cerr << 91 << " "
                       << "u << \" \" << v << \" \" << diff"
                       << " " << u << " " << v << " " << diff << endl;
              };
            }
        }
        {
          if (DEBUG)
            cerr << 94 << " "
                 << "order[i][0] << \" \" << diff"
                 << " " << order[i][0] << " " << diff << endl;
        };
        sum += order[i][0] * diff;
        prev_cnt += diff;
        i = j;
      }
      reverse((order).begin(), (order).end());
      {
        if (DEBUG)
          cerr << 100 << " "
               << "sum"
               << " " << sum << endl;
      };
      {
        if (DEBUG)
          cerr << 101 << " "
               << "\"!!\""
               << " "
               << "!!" << endl;
      };
    }
    cout << abs(res[0] - res[1]) << "\n";
  }
  void gen() {}
  void brute() {}
};
int main(int argc, char **argv) {
  for (int64_t i = (1); i <= (argc - 1); ++i)
    for (int j = 0; argv[i][j]; j++)
      if (argv[i][j] == '.') freopen(argv[i], "r", stdin);
  for (int64_t i = (1); i <= (argc - 1); ++i)
    if (argv[i] == string("q")) DEBUG = 1 << 30;
  for (int64_t i = (1); i <= (argc - 1); ++i)
    if (argv[i] == string("gen")) {
      (solver = new solver_t())->gen();
      exit(0);
    }
  for (int64_t i = (1); i <= (argc - 1); ++i)
    if (argv[i] == string("brute")) {
      (solver = new solver_t())->brute();
      exit(0);
    }
  ios::sync_with_stdio(false), cin.tie(0);
  cout.setf(ios::fixed), cout.precision(10);
  int t;
  if (MULTI || GCJ)
    cin >> t;
  else
    t = 1;
  for (int64_t i = (1); i <= (t); ++i) {
    if (DEBUG) cerr << 130 << " " << i << endl;
    if (GCJ) cout << "Case #" << i << ": ";
    solver = new solver_t();
    solver->solve();
  }
  return 0;
}
