#include <bits/stdc++.h>
using namespace std;
vector<long long> kek;
long long n;
signed main() {
  cin >> n;
  long long arr[n + 1];
  for (long long i = 0; i != n; ++i) scanf("%lld", &arr[i]);
  arr[n] = n + 1;
  long long ans = 0;
  kek.push_back(arr[0]);
  for (long long i = 1; i != n + 1; ++i) {
    if (arr[i] == arr[i - 1]) continue;
    kek.push_back(arr[i]);
  }
  long long m = kek.size();
  for (long long i = 1; i != m; ++i) {
    if (kek[i] > kek[i - 1]) {
      ans += kek[i - 1] * (kek[i] - kek[i - 1]);
    } else {
      ans += (n - kek[i - 1] + 1) * (kek[i - 1] - kek[i]);
    }
  }
  cout << ans << endl;
}
