#include <bits/stdc++.h>
using namespace std;
int n, x;
long long smn, smx = 0;
set<pair<int, int> > mset;
pair<int, int> g;
void summin(pair<int, int> a[]) {
  mset.insert(pair<int, int>(n - 1, 0));
  for (int i = 0; i < n; ++i) {
    g = *mset.lower_bound(pair<int, int>(a[i].second, 0));
    mset.erase(g);
    smn += ((long long)a[i].first * (g.first - a[i].second + 1) *
            (a[i].second - g.second + 1));
    if (g.first != a[i].second)
      mset.insert(pair<int, int>(g.first, a[i].second + 1));
    if (g.second != a[i].second)
      mset.insert(pair<int, int>(a[i].second - 1, g.second));
  }
}
void summax(pair<int, int> a[]) {
  mset.insert(pair<int, int>(n - 1, 0));
  for (int i = n - 1; i > -1; --i) {
    g = *mset.lower_bound(pair<int, int>(a[i].second, 0));
    mset.erase(g);
    smx += ((long long)a[i].first * (g.first - a[i].second + 1) *
            (a[i].second - g.second + 1));
    if (g.first != a[i].second)
      mset.insert(pair<int, int>(g.first, a[i].second + 1));
    if (g.second != a[i].second)
      mset.insert(pair<int, int>(a[i].second - 1, g.second));
  }
}
int main() {
  scanf("%d", &n);
  pair<int, int> arr[n];
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x);
    arr[i].first = x;
    arr[i].second = i;
  }
  sort(arr, arr + n);
  summin(arr);
  summax(arr);
  printf("%lld", smx - smn);
}
