#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
long long n, a[N], dp[N], aux[N], tot = 0, ans = 0;
vector<int> v[N];
int main() {
  scanf("%lld", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    v[a[i]].push_back(i);
  }
  for (int i = n; i > 0; i--) {
    aux[i] = v[i].size();
    tot += (long long)v[i].size() * (n - i + 1LL);
    for (int j = 0; j < v[i].size(); j++) {
      int x = v[i][j];
      if (x && a[x - 1] > i) {
        long long y = a[x - 1];
        tot -= aux[y] * (n - y + 1LL);
        aux[y]--;
        tot += aux[y] * (n - y + 1LL);
      }
      if (x + 1 != n && a[x + 1] >= i) {
        long long y = a[x + 1];
        tot -= aux[y] * (n - y + 1LL);
        aux[y]--;
        tot += aux[y] * (n - y + 1LL);
      }
    }
    ans += tot;
  }
  printf("%lld\n", ans);
  return 0;
}
