#include <bits/stdc++.h>
using namespace std;
long long solve(long long n, long long a[]) {
  stack<long long> second;
  long long left[n], right[n], ans[n];
  for (long long i = 0; i < n; i++) {
    if (second.empty()) {
      left[i] = 0;
      second.push(i);
    } else {
      while (!second.empty() && a[second.top()] <= a[i]) second.pop();
      if (second.empty()) {
        left[i] = 0;
        second.push(i);
      } else {
        left[i] = second.top() + 1;
        second.push(i);
      }
    }
  }
  while (!second.empty()) second.pop();
  for (long long i = n - 1; i >= 0; i--) {
    if (second.empty()) {
      right[i] = n - 1;
      second.push(i);
    } else {
      while (!second.empty() && a[second.top()] < a[i]) second.pop();
      if (second.empty()) {
        right[i] = n - 1;
        second.push(i);
      } else {
        right[i] = second.top() - 1;
        second.push(i);
      }
    }
  }
  for (long long i = 0; i < n; i++) {
    long long x = i - left[i] + 1;
    long long y = right[i] - i + 1;
    ans[i] = x * y;
  }
  long long answer = 0;
  for (long long i = 0; i < n; i++) {
    answer += (ans[i] * abs(a[i]));
  }
  return answer;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long n, i, s1, s2;
  cin >> n;
  long long a[n];
  for (i = 0; i < n; i++) cin >> a[i];
  s1 = solve(n, a);
  for (i = 0; i < n; i++) a[i] = -a[i];
  s2 = solve(n, a);
  cout << s1 - s2 << endl;
  return 0;
}
