#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long longinf = 1LL << 60;
const long long mod = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  long long a[n];
  long long ans = 0;
  long long b[n];
  long long left = -1;
  long long right = n;
  for (int i = (int)0; i < (int)n; ++i) {
    cin >> a[i];
    a[i]--;
    b[a[i]] = i;
  }
  set<long long> se;
  se.insert(-1);
  se.insert(n);
  for (int i = (int)0; i < (int)n; ++i) {
    if (i > 0) {
      auto x = se.upper_bound(b[i]);
      right = *x;
      x--;
      left = *x;
    }
    se.insert(b[i]);
    ans +=
        (i + 1) * ((b[i] - left - 1) * (right - b[i] - 1) + right - left - 1);
  }
  cout << ans << endl;
  return 0;
}
