#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
  return !b ? a : gcd(b, a % b);
}
template <typename T>
inline void umin(T &a, T b) {
  a = a < b ? a : b;
}
template <typename T>
inline void umax(T &a, T b) {
  a = a > b ? a : b;
}
const int N = (int)1e6 + 5;
const int INF = (int)1e9 + 7;
const long long MOD = (long long)1e9 + 7;
int a[N], L[N], R[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  long long res = 0;
  for (int i = 1; i <= n; i++) {
    L[i] = i - 1;
    while (L[i] and a[L[i]] < a[i]) L[i] = L[L[i]];
  }
  for (int i = n; i >= 1; i--) {
    R[i] = i + 1;
    while (R[i] <= n and a[R[i]] <= a[i]) R[i] = R[R[i]];
  }
  for (int i = 1; i <= n; i++) {
    res += 1LL * a[i] * 1LL * (i - L[i]) * 1LL * (R[i] - i);
  }
  for (int i = 1; i <= n; i++) {
    L[i] = i - 1;
    while (L[i] and a[L[i]] > a[i]) L[i] = L[L[i]];
  }
  for (int i = n; i >= 1; i--) {
    R[i] = i + 1;
    while (R[i] <= n and a[R[i]] >= a[i]) R[i] = R[R[i]];
  }
  for (int i = 1; i <= n; i++) {
    res -= 1LL * a[i] * 1LL * (i - L[i]) * 1LL * (R[i] - i);
  }
  printf("%lld\n", res);
  return 0;
}
