#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T abs(T t) {
  return t < 0 ? -t : t;
}
const long long modn = 1000000007;
inline long long mod(long long x) { return x % modn; }
const int MAXN = 1123456;
int n, m, k;
long long s[MAXN];
long long mnL[MAXN], mnR[MAXN], mxL[MAXN], mxR[MAXN];
stack<int> q;
int main() {
  scanf("%d", &n);
  for (int a = 1; a <= n; a++) {
    scanf("%lld", s + a);
  }
  q.push(0);
  s[0] = LLONG_MAX;
  s[n + 1] = LLONG_MAX;
  for (int a = 1; a <= n + 1; a++) {
    while (!q.empty() && s[q.top()] < s[a]) {
      mxR[q.top()] = a;
      q.pop();
    }
    mxL[a] = q.top();
    q.push(a);
  }
  while (!q.empty()) q.pop();
  s[0] = LLONG_MIN;
  s[n + 1] = LLONG_MIN;
  q.push(0);
  for (int a = 1; a <= n + 1; a++) {
    while (!q.empty() && s[q.top()] > s[a]) {
      mnR[q.top()] = a;
      q.pop();
    }
    mnL[a] = q.top();
    q.push(a);
  }
  while (!q.empty()) q.pop();
  long long res = 0;
  for (long long a = 1; a <= n; a++) {
    res += s[a] * (a - mxL[a]) * (mxR[a] - a);
    res -= s[a] * (a - mnL[a]) * (mnR[a] - a);
  }
  printf("%lld\n", res);
}
