#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
int MOD = 1000000007;
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
int main() {
  std::ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  std::vector<pair<int, int>> v;
  std::vector<int> A;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    v.push_back(make_pair(a, i));
    A.push_back(a);
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  UnionFind uf(n);
  long long ans = 0LL;
  for (int i = 0; i < n; i++) {
    int left = 0, right = 0;
    if (v[i].second - 1 >= 0 && A[v[i].second - 1] > A[v[i].second]) {
      left = uf.size(A[v[i].second - 1] - 1);
      uf.unionSet(A[v[i].second - 1] - 1, v[i].first - 1);
      ans += (left + 0LL) * (v[i].first + 0LL);
    }
    if (v[i].second + 1 < n && A[v[i].second + 1] > A[v[i].second]) {
      right = uf.size(A[v[i].second + 1] - 1);
      uf.unionSet(A[v[i].second + 1] - 1, v[i].first - 1);
      ans += (right + 0LL) * (v[i].first + 0LL);
    }
    ans += ((left + 0LL) * (right + 0LL) + 1LL) * (v[i].first + 0LL) + 0LL;
  }
  cout << ans << endl;
  return 0;
}
