#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int N = 1000006;
pair<int, long long> S[N];
long long A[N], n, i, s, t, x;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> A[i];
    while (s && A[i] > A[S[s].first]) s--;
    t += x = A[i] * (i - S[s].first) + S[s].second;
    S[++s] = make_pair(i, x);
  }
  for (s = 0, i = 1; i <= n; i++) {
    while (s && A[i] < A[S[s].first]) s--;
    t -= x = A[i] * (i - S[s].first) + S[s].second;
    S[++s] = make_pair(i, x);
  }
  cout << t;
  return 0;
}
