#include <bits/stdc++.h>
const long long MOD = 1e9 + 7;
const long long MAXN = 1e6 + 1;
using namespace std;
long long readInt() {
  bool minus1 = false;
  long long result = 0;
  char ch;
  ch = getchar();
  while (true) {
    if (ch == '-') break;
    if (ch >= '0' && ch <= '9') break;
    ch = getchar();
  }
  if (ch == '-')
    minus1 = true;
  else
    result = ch - '0';
  while (true) {
    ch = getchar();
    if (ch < '0' || ch > '9') break;
    result = result * 10 + (ch - '0');
  }
  if (minus1)
    return -result;
  else
    return result;
}
int a[MAXN];
int r[MAXN];
int l[MAXN];
int main() {
  int n = readInt();
  for (int i = 1; i <= n; i++) {
    a[i] = readInt();
  }
  stack<int> s;
  for (int i = 1; i <= n; i++) {
    while (!s.empty() && a[s.top()] >= a[i]) {
      s.pop();
    }
    if (s.empty()) {
      l[i] = 0;
    } else {
      l[i] = s.top();
    }
    s.push(i);
  }
  stack<int> s1;
  for (int i = n; i >= 1; i--) {
    while (!s1.empty() && a[s1.top()] >= a[i]) {
      s1.pop();
    }
    if (s1.empty()) {
      r[i] = n + 1;
    } else {
      r[i] = s1.top();
    }
    s1.push(i);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int dist1 = abs(l[i] - i);
    int dist2 = abs(r[i] - i);
    ans += (1ll * dist1 * dist2) * a[i];
  }
  cout << ans;
  return 0;
}
