#include <bits/stdc++.h>
using namespace std;
const int Maxn = 200005;
void ansv_left(vector<int>& v, vector<int>& res) {
  stack<pair<int, int> > stk;
  stk.push(make_pair(INT_MIN, v.size()));
  for (int i = v.size() - 1; i >= 0; i--) {
    while (stk.top().first >= v[i]) {
      res[stk.top().second] = i;
      stk.pop();
    }
    stk.push(make_pair(v[i], i));
  }
  while (stk.top().second < v.size()) {
    res[stk.top().second] = -1;
    stk.pop();
  }
}
void ansv_right(vector<int>& v, vector<int>& res) {
  stack<pair<int, int> > stk;
  stk.push(make_pair(INT_MIN, -1));
  for (int i = 0; i < v.size(); i++) {
    while (stk.top().first > v[i]) {
      res[stk.top().second] = i;
      stk.pop();
    }
    stk.push(make_pair(v[i], i));
  }
  while (stk.top().second > -1) {
    res[stk.top().second] = v.size();
    stk.pop();
  }
}
void anbv_left(vector<int>& v, vector<int>& res) {
  stack<pair<int, int> > stk;
  stk.push(make_pair(INT_MAX, v.size()));
  for (int i = v.size() - 1; i >= 0; i--) {
    while (stk.top().first <= v[i]) {
      res[stk.top().second] = i;
      stk.pop();
    }
    stk.push(make_pair(v[i], i));
  }
  while (stk.top().second < v.size()) {
    res[stk.top().second] = -1;
    stk.pop();
  }
}
void anbv_right(vector<int>& v, vector<int>& res) {
  stack<pair<int, int> > stk;
  stk.push(make_pair(INT_MAX, -1));
  for (int i = 0; i < v.size(); i++) {
    while (stk.top().first < v[i]) {
      res[stk.top().second] = i;
      stk.pop();
    }
    stk.push(make_pair(v[i], i));
  }
  while (stk.top().second > -1) {
    res[stk.top().second] = v.size();
    stk.pop();
  }
}
int main() {
  int n;
  scanf("%d", &n);
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &v[i]);
  }
  vector<int> ansvr(n), ansvl(n), anbvr(n), anbvl(n);
  ansv_left(v, ansvl);
  ansv_right(v, ansvr);
  anbv_left(v, anbvl);
  anbv_right(v, anbvr);
  long long ans = 0LL;
  for (int i = 0; i < n; i++) {
    ans -= (ansvr[i] - i) * 1LL * (i - ansvl[i]) * v[i];
    ans += (anbvr[i] - i) * 1LL * (i - anbvl[i]) * v[i];
  }
  printf("%lld\n", ans);
  return 0;
}
