#include <bits/stdc++.h>
namespace IO {
const unsigned int bufsize = 1 << 16, outsize = 1 << 24;
static char ch[bufsize], *S = ch, *T = ch;
inline char getc() {
  return ((S == T) && (T = (S = ch) + fread(ch, 1, bufsize, stdin), S == T)
              ? 0
              : *S++);
}
static char Out[outsize], *nowp = Out;
inline void flush() {
  fwrite(Out, 1, nowp - Out, stdout);
  nowp = Out;
}
template <typename T>
void read(T &x) {
  char c = getc();
  x = 0;
  for (; !isdigit(c); c = getc())
    ;
  for (; isdigit(c); x = (x << 1) + (x << 3) + (c ^ '0'), c = getc())
    ;
}
template <typename T>
void write(T x, char c = '\n') {
  if (!x) *nowp++ = '0';
  static unsigned int stk[50], tp = 0;
  for (; x; x /= 10) stk[++tp] = x % 10;
  for (; tp; *nowp++ = stk[tp--] ^ '0')
    ;
  *nowp++ = c;
}
}  // namespace IO
int n, a[200005], stk[200005], pre[200005], tp;
long long ans;
int main() {
  using namespace IO;
  read(ans);
  for (register int i = (1); i <= (n); i = -(~(i))) {
    read(a[i]);
    while (tp && a[stk[tp]] > a[i]) --tp;
    pre[i] = stk[tp], stk[++tp] = i;
  }
  tp = 0;
  for (register int i = (n); i >= (1); i = ~(-(i))) {
    while (tp && a[stk[tp]] > a[i]) --tp;
    ans += (long long)(i - pre[i]) * ((tp ? stk[tp] : n + 1) - i) * a[i];
    stk[++tp] = i;
  }
  return write(ans), flush(), 0;
}
