#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
using vb = vector<bool>;
const ll oo = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-9;
template <class T>
ll qp(T& a) {
  cin >> a;
}
template <class... A>
void in(A&... a) {
  auto b = {qp(a)...};
}
vl v, pos;
set<pll> ints;
vb schon;
ll ges = 0;
ll get(int nex) {
  if (schon[nex]) return 0;
  int p = pos[nex];
  auto it = upper_bound(begin(ints), end(ints), make_pair(ll(p + 1), ll(0)));
  it--;
  ll a = p - it->first + 1;
  ll b = it->second - p + 1;
  ll r = a * b;
  if (p - 1 > it->first) {
    ints.insert(make_pair(it->first, p - 1));
  } else if (p - 1 == it->first) {
    ges += v[p - 1];
    schon[v[p - 1] - 1] = true;
  }
  if (p + 1 < it->second) {
    ints.insert(make_pair(p + 1, it->second));
  } else if (p + 1 == it->second) {
    ges += v[p + 1];
    schon[v[p + 1] - 1] = true;
  }
  ints.erase(it);
  return r;
}
int main() {
  int n;
  in(n);
  ;
  v = vl(n);
  pos = vl(n);
  schon = vb(n);
  for (int i = (0); i < (n); i++) {
    cin >> v[i];
    pos[v[i] - 1] = i;
  }
  ints.insert(make_pair(0, n - 1));
  ll res = 0;
  for (int i = (0); i < (n); i++) {
    ll a = i + 1;
    ll b = get(i);
    res += a * b;
  }
  cout << res + ges << endl;
}
