#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int h[MAXN], ent[MAXN], sai[MAXN];
stack<int> st, sti;
unsigned long long int resp, cur, cur2;
int n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &h[i]);
  st.push(-1);
  sti.push(0);
  for (int i = 1; i <= n + 1; i++) {
    if (i == n + 1) h[i] = 0;
    while (st.size() && st.top() > h[i]) {
      sai[sti.top()] = i - 1;
      st.pop();
      sti.pop();
    }
    ent[i] = sti.top() + 1;
    st.push(h[i]);
    sti.push(i);
  }
  for (int i = 1; i <= n; i++) {
    cur = h[i];
    cur *= (sai[i] - ent[i] + 1);
    cur2 = i - ent[i];
    cur2 *= sai[i] - i;
    cur2 *= h[i];
    resp += cur;
    resp += cur2;
  }
  printf("%llu\n", resp);
}
