#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n;
int a[N], L[N], R[N];
long long Max() {
  long long S = 0;
  stack<int> ST;
  for (int i = 1; i <= n; ++i) {
    while (ST.size() && a[i] > a[ST.top()]) ST.pop();
    L[i] = ST.empty() ? 1 : ST.top() + 1;
    ST.push(i);
  }
  while (ST.size()) ST.pop();
  for (int i = n; i >= 1; --i) {
    while (ST.size() && a[i] >= a[ST.top()]) ST.pop();
    R[i] = ST.empty() ? n : ST.top() - 1;
    ST.push(i);
  }
  for (int i = 1; i <= n; ++i)
    S += (long long)a[i] * (i - L[i] + 1) * (R[i] - i + 1);
  return S;
}
long long Min() {
  long long S = 0;
  stack<int> ST;
  for (int i = 1; i <= n; ++i) {
    while (ST.size() && a[i] < a[ST.top()]) ST.pop();
    L[i] = ST.empty() ? 1 : ST.top() + 1;
    ST.push(i);
  }
  while (ST.size()) ST.pop();
  for (int i = n; i >= 1; --i) {
    while (ST.size() && a[i] <= a[ST.top()]) ST.pop();
    R[i] = ST.empty() ? n : ST.top() - 1;
    ST.push(i);
  }
  for (int i = 1; i <= n; ++i)
    S += (long long)a[i] * (i - L[i] + 1) * (R[i] - i + 1);
  return S;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  printf("%I64d", Max() - Min());
  return 0;
}
