#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long llinf = 0x3f3f3f3f3f3f3f3f;
template <typename INint>
inline void IN(INint &first) {
  first = 0;
  int f = 1;
  char c;
  cin.get(c);
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    cin.get(c);
  }
  while (c >= '0' && c <= '9') {
    first = first * 10 + c - '0';
    cin.get(c);
  }
  first *= f;
}
template <typename INint>
inline void OUT(INint first) {
  if (first > 9) OUT(first / 10);
  cout.put(first % 10 + '0');
}
const int maxn = int(1e5 + 5);
vector<int> G[maxn];
int a[maxn];
long long ans[maxn] = {0};
int n;
int main() {
  int T(1), times(0);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  while (++times, T--) {
    cin >> n;
    for (int i = 1; i <= int(n); i++) {
      int a;
      cin >> a;
      G[a].push_back(i);
    }
    for (int i = 1; i <= int(n); i++) {
      ans[i] = ans[i - 1];
      map<int, int> ma;
      for (auto first : G[i]) {
        a[first] = i;
        if (a[first - 1]) ++ma[a[first - 1]];
        if (a[first + 1]) ++ma[a[first + 1]];
      }
      if (ma.empty()) {
        ans[i] += i * G[i].size();
        continue;
      }
      ma[i] = max(ma[i], 0);
      ma[0] = -1;
      long long cnt(0);
      for (map<int, int>::iterator it = (--ma.end()); it != ma.begin(); --it) {
        cnt += it->second;
        auto bef = it;
        --bef;
        ans[i] += (it->first - bef->first) * G[i].size();
        ans[i] -= (it->first - bef->first) * cnt;
      }
    }
    cout << accumulate(ans + 1, ans + 1 + n, 0ll) << endl;
  }
  return 0;
}
