#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
long long solve1(long long x, long long y) {
  if (abs(x - y) < 100) {
    long long ans = 0;
    for (long long i = x; i <= y; i++) {
      long long m1 = v[i];
      for (long long j = i; j <= y; j++) {
        m1 = max(m1, v[j]);
        ans += m1;
      }
    }
    return ans;
  }
  long long m = (x + y) / 2;
  long long ans = 0, m1 = v[m], m2 = v[m];
  vector<long long> v1, v2, s1, s2;
  s1.push_back(0);
  for (long long i = m; i <= y; i++) {
    m1 = max(m1, v[i]);
    m2 = min(m2, v[i]);
    v1.push_back(m1);
  }
  sort(v2.begin(), v2.end());
  m1 = v[m];
  for (long long i = 0; i < v1.size(); i++) {
    s1.push_back(s1[s1.size() - 1] + v1[i]);
  }
  for (long long i = m; i >= x; i--) {
    m1 = max(m1, v[i]);
    long long d1 = upper_bound(v1.begin(), v1.end(), m1) - v1.begin();
    ans += d1 * (m1) + s1[s1.size() - 1] - s1[d1];
  }
  long long a = solve1(x, m - 1) + solve1(m + 1, y);
  return ans + a;
}
long long solve2(long long x, long long y) {
  if (abs(x - y) < 100) {
    long long ans = 0;
    for (long long i = x; i <= y; i++) {
      long long m2 = v[i];
      for (long long j = i; j <= y; j++) {
        m2 = min(m2, v[j]);
        ans += m2;
      }
    }
    return ans;
  }
  long long m = (x + y) / 2;
  long long ans = 0, m1 = v[m], m2 = v[m];
  vector<long long> v2, s2;
  s2.push_back(0);
  for (long long i = m; i <= y; i++) {
    m2 = min(m2, v[i]);
    v2.push_back(m2);
  }
  sort(v2.begin(), v2.end());
  m2 = v[m];
  for (long long i = 0; i < v2.size(); i++) {
    s2.push_back(s2[s2.size() - 1] + v2[i]);
  }
  for (long long i = m; i >= x; i--) {
    m2 = min(m2, v[i]);
    long long d2 = upper_bound(v2.begin(), v2.end(), m2) - v2.begin();
    ans += s2[d2] + (v2.size() - d2) * m2;
  }
  long long a = solve2(x, m - 1) + solve2(m + 1, y);
  return ans + a;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    v.push_back(x);
  }
  long long ans1 = solve1(0, n - 1);
  long long ans2 = solve2(0, n - 1);
  cout << abs(ans1 - ans2);
}

### Prompt

Your task is to create a Cpp solution to the following problem:

You are given an array a consisting of n elements. The imbalance value of some subsegment of this array is the difference between the maximum and minimum element from this segment. The imbalance value of the array is the sum of imbalance values of all subsegments of this array.

For example, the imbalance value of array [1, 4, 1] is 9, because there are 6 different subsegments of this array: 

  * [1] (from index 1 to index 1), imbalance value is 0; 
  * [1, 4] (from index 1 to index 2), imbalance value is 3; 
  * [1, 4, 1] (from index 1 to index 3), imbalance value is 3; 
  * [4] (from index 2 to index 2), imbalance value is 0; 
  * [4, 1] (from index 2 to index 3), imbalance value is 3; 
  * [1] (from index 3 to index 3), imbalance value is 0; 



You have to determine the imbalance value of the array a.

Input

The first line contains one integer n (1 ≤ n ≤ 106) — size of the array a.

The second line contains n integers a1, a2... an (1 ≤ ai ≤ 106) — elements of the array.

Output

Print one integer — the imbalance value of a.

Example

Input

3
1 4 1


Output

9

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
long long solve1(long long x, long long y) {
  if (abs(x - y) < 100) {
    long long ans = 0;
    for (long long i = x; i <= y; i++) {
      long long m1 = v[i];
      for (long long j = i; j <= y; j++) {
        m1 = max(m1, v[j]);
        ans += m1;
      }
    }
    return ans;
  }
  long long m = (x + y) / 2;
  long long ans = 0, m1 = v[m], m2 = v[m];
  vector<long long> v1, v2, s1, s2;
  s1.push_back(0);
  for (long long i = m; i <= y; i++) {
    m1 = max(m1, v[i]);
    m2 = min(m2, v[i]);
    v1.push_back(m1);
  }
  sort(v2.begin(), v2.end());
  m1 = v[m];
  for (long long i = 0; i < v1.size(); i++) {
    s1.push_back(s1[s1.size() - 1] + v1[i]);
  }
  for (long long i = m; i >= x; i--) {
    m1 = max(m1, v[i]);
    long long d1 = upper_bound(v1.begin(), v1.end(), m1) - v1.begin();
    ans += d1 * (m1) + s1[s1.size() - 1] - s1[d1];
  }
  long long a = solve1(x, m - 1) + solve1(m + 1, y);
  return ans + a;
}
long long solve2(long long x, long long y) {
  if (abs(x - y) < 100) {
    long long ans = 0;
    for (long long i = x; i <= y; i++) {
      long long m2 = v[i];
      for (long long j = i; j <= y; j++) {
        m2 = min(m2, v[j]);
        ans += m2;
      }
    }
    return ans;
  }
  long long m = (x + y) / 2;
  long long ans = 0, m1 = v[m], m2 = v[m];
  vector<long long> v2, s2;
  s2.push_back(0);
  for (long long i = m; i <= y; i++) {
    m2 = min(m2, v[i]);
    v2.push_back(m2);
  }
  sort(v2.begin(), v2.end());
  m2 = v[m];
  for (long long i = 0; i < v2.size(); i++) {
    s2.push_back(s2[s2.size() - 1] + v2[i]);
  }
  for (long long i = m; i >= x; i--) {
    m2 = min(m2, v[i]);
    long long d2 = upper_bound(v2.begin(), v2.end(), m2) - v2.begin();
    ans += s2[d2] + (v2.size() - d2) * m2;
  }
  long long a = solve2(x, m - 1) + solve2(m + 1, y);
  return ans + a;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    v.push_back(x);
  }
  long long ans1 = solve1(0, n - 1);
  long long ans2 = solve2(0, n - 1);
  cout << abs(ans1 - ans2);
}

```