#include <bits/stdc++.h>
using namespace std;
int n, tab[1000100], Rs, Ls;
pair<int, int> segMax[8000800];
pair<int, int> segMin[8000800];
pair<int, int> build_seg_max(int l, int r, int now) {
  if (r <= l) {
    segMax[now] = make_pair(tab[l], l);
    return segMax[now];
  }
  int mid = l + (r - l) / 2;
  pair<int, int> kiri = build_seg_max(l, mid, 2 * now);
  pair<int, int> kanan = build_seg_max(mid + 1, r, 2 * now + 1);
  if (kiri.first > kanan.first)
    segMax[now] = kiri;
  else
    segMax[now] = kanan;
  return segMax[now];
}
pair<int, int> build_seg_min(int l, int r, int now) {
  if (r <= l) {
    segMin[now] = make_pair(tab[l], l);
    return segMin[now];
  }
  int mid = l + (r - l) / 2;
  pair<int, int> kiri = build_seg_min(l, mid, 2 * now);
  pair<int, int> kanan = build_seg_min(mid + 1, r, 2 * now + 1);
  if (kiri.first < kanan.first)
    segMin[now] = kiri;
  else
    segMin[now] = kanan;
  return segMin[now];
}
pair<int, int> find_max(int Ln, int Rn, int now) {
  if (Rs < Ln) return make_pair(-1e9, -1);
  if (Rn < Ls) return make_pair(-1e9, -1);
  if (Ls <= Ln && Rn <= Rs) return segMax[now];
  int mid = Ln + (Rn - Ln) / 2;
  pair<int, int> kiri = find_max(Ln, mid, 2 * now);
  pair<int, int> kanan = find_max(mid + 1, Rn, 2 * now + 1);
  if (kiri.first > kanan.first)
    return kiri;
  else
    return kanan;
}
pair<int, int> find_min(int Ln, int Rn, int now) {
  if (Rs < Ln) return make_pair(1e9, -1);
  if (Rn < Ls) return make_pair(1e9, -1);
  if (Ls <= Ln && Rn <= Rs) return segMin[now];
  int mid = Ln + (Rn - Ln) / 2;
  pair<int, int> kiri = find_min(Ln, mid, 2 * now);
  pair<int, int> kanan = find_min(mid + 1, Rn, 2 * now + 1);
  if (kiri.first < kanan.first)
    return kiri;
  else
    return kanan;
}
pair<int, int> find_max1(int Ln, int Rn, int ll, int rr, int now) {
  Ls = ll;
  Rs = rr;
  return find_max(Ln, Rn, now);
}
pair<int, int> find_min1(int Ln, int Rn, int ll, int rr, int now) {
  Ls = ll;
  Rs = rr;
  return find_min(Ln, Rn, now);
}
long long rek_max(int left, int right) {
  if (right < left) return 0;
  pair<int, int> maxx = find_max1(0, n - 1, left, right, 1);
  long long ans = 0;
  ans = (long long)maxx.first * (maxx.second - left + 1) *
        (right - maxx.second + 1);
  ans += rek_max(left, maxx.second - 1);
  ans += rek_max(maxx.second + 1, right);
  return ans;
}
long long rek_min(int left, int right) {
  if (right < left) return 0;
  pair<int, int> minn = find_min1(0, n - 1, left, right, 1);
  long long ans = 0;
  ans = (long long)minn.first * (minn.second - left + 1) *
        (right - minn.second + 1);
  ans += rek_min(left, minn.second - 1);
  ans += rek_min(minn.second + 1, right);
  return ans;
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tab[i];
  }
  pair<int, int> maxi = build_seg_max(0, n - 1, 1);
  pair<int, int> mini = build_seg_min(0, n - 1, 1);
  long long ans = rek_max(0, n - 1);
  ans -= rek_min(0, n - 1);
  cout << ans << endl;
  return 0;
}

### Prompt

Your challenge is to write 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;
int n, tab[1000100], Rs, Ls;
pair<int, int> segMax[8000800];
pair<int, int> segMin[8000800];
pair<int, int> build_seg_max(int l, int r, int now) {
  if (r <= l) {
    segMax[now] = make_pair(tab[l], l);
    return segMax[now];
  }
  int mid = l + (r - l) / 2;
  pair<int, int> kiri = build_seg_max(l, mid, 2 * now);
  pair<int, int> kanan = build_seg_max(mid + 1, r, 2 * now + 1);
  if (kiri.first > kanan.first)
    segMax[now] = kiri;
  else
    segMax[now] = kanan;
  return segMax[now];
}
pair<int, int> build_seg_min(int l, int r, int now) {
  if (r <= l) {
    segMin[now] = make_pair(tab[l], l);
    return segMin[now];
  }
  int mid = l + (r - l) / 2;
  pair<int, int> kiri = build_seg_min(l, mid, 2 * now);
  pair<int, int> kanan = build_seg_min(mid + 1, r, 2 * now + 1);
  if (kiri.first < kanan.first)
    segMin[now] = kiri;
  else
    segMin[now] = kanan;
  return segMin[now];
}
pair<int, int> find_max(int Ln, int Rn, int now) {
  if (Rs < Ln) return make_pair(-1e9, -1);
  if (Rn < Ls) return make_pair(-1e9, -1);
  if (Ls <= Ln && Rn <= Rs) return segMax[now];
  int mid = Ln + (Rn - Ln) / 2;
  pair<int, int> kiri = find_max(Ln, mid, 2 * now);
  pair<int, int> kanan = find_max(mid + 1, Rn, 2 * now + 1);
  if (kiri.first > kanan.first)
    return kiri;
  else
    return kanan;
}
pair<int, int> find_min(int Ln, int Rn, int now) {
  if (Rs < Ln) return make_pair(1e9, -1);
  if (Rn < Ls) return make_pair(1e9, -1);
  if (Ls <= Ln && Rn <= Rs) return segMin[now];
  int mid = Ln + (Rn - Ln) / 2;
  pair<int, int> kiri = find_min(Ln, mid, 2 * now);
  pair<int, int> kanan = find_min(mid + 1, Rn, 2 * now + 1);
  if (kiri.first < kanan.first)
    return kiri;
  else
    return kanan;
}
pair<int, int> find_max1(int Ln, int Rn, int ll, int rr, int now) {
  Ls = ll;
  Rs = rr;
  return find_max(Ln, Rn, now);
}
pair<int, int> find_min1(int Ln, int Rn, int ll, int rr, int now) {
  Ls = ll;
  Rs = rr;
  return find_min(Ln, Rn, now);
}
long long rek_max(int left, int right) {
  if (right < left) return 0;
  pair<int, int> maxx = find_max1(0, n - 1, left, right, 1);
  long long ans = 0;
  ans = (long long)maxx.first * (maxx.second - left + 1) *
        (right - maxx.second + 1);
  ans += rek_max(left, maxx.second - 1);
  ans += rek_max(maxx.second + 1, right);
  return ans;
}
long long rek_min(int left, int right) {
  if (right < left) return 0;
  pair<int, int> minn = find_min1(0, n - 1, left, right, 1);
  long long ans = 0;
  ans = (long long)minn.first * (minn.second - left + 1) *
        (right - minn.second + 1);
  ans += rek_min(left, minn.second - 1);
  ans += rek_min(minn.second + 1, right);
  return ans;
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tab[i];
  }
  pair<int, int> maxi = build_seg_max(0, n - 1, 1);
  pair<int, int> mini = build_seg_min(0, n - 1, 1);
  long long ans = rek_max(0, n - 1);
  ans -= rek_min(0, n - 1);
  cout << ans << endl;
  return 0;
}

```