#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
long long solve1(long long x, long long y) {
  if (abs(x - y) < 10) {
    long long ans = 0;
    for (long long i = x; i <= y; i++) {
      long long m1 = v[i];
      for (long long j = i; j <= y; j++) {
        m1 = max(m1, v[j]);
        ans += m1;
      }
    }
    return ans;
  }
  long long m = (x + y) / 2;
  long long ans = 0, m1 = v[m], m2 = v[m];
  vector<long long> v1, v2, s1, s2;
  s1.push_back(0);
  for (long long i = m; i <= y; i++) {
    m1 = max(m1, v[i]);
    m2 = min(m2, v[i]);
    v1.push_back(m1);
  }
  sort(v2.begin(), v2.end());
  m1 = v[m];
  for (long long i = 0; i < v1.size(); i++) {
    s1.push_back(s1[s1.size() - 1] + v1[i]);
  }
  for (long long i = m; i >= x; i--) {
    m1 = max(m1, v[i]);
    long long d1 = upper_bound(v1.begin(), v1.end(), m1) - v1.begin();
    ans += d1 * (m1) + s1[s1.size() - 1] - s1[d1];
  }
  long long a = solve1(x, m - 1) + solve1(m + 1, y);
  return ans + a;
}
long long solve2(long long x, long long y) {
  if (abs(x - y) < 10) {
    long long ans = 0;
    for (long long i = x; i <= y; i++) {
      long long m2 = v[i];
      for (long long j = i; j <= y; j++) {
        m2 = min(m2, v[j]);
        ans += m2;
      }
    }
    return ans;
  }
  long long m = (x + y) / 2;
  long long ans = 0, m1 = v[m], m2 = v[m];
  vector<long long> v2, s2;
  s2.push_back(0);
  for (long long i = m; i <= y; i++) {
    m2 = min(m2, v[i]);
    v2.push_back(m2);
  }
  sort(v2.begin(), v2.end());
  m2 = v[m];
  for (long long i = 0; i < v2.size(); i++) {
    s2.push_back(s2[s2.size() - 1] + v2[i]);
  }
  for (long long i = m; i >= x; i--) {
    m2 = min(m2, v[i]);
    long long d2 = upper_bound(v2.begin(), v2.end(), m2) - v2.begin();
    ans += s2[d2] + (v2.size() - d2) * m2;
  }
  long long a = solve2(x, m - 1) + solve2(m + 1, y);
  return ans + a;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    v.push_back(x);
  }
  long long ans1 = solve1(0, n - 1);
  long long ans2 = solve2(0, n - 1);
  cout << abs(ans1 - ans2);
}
