#include <bits/stdc++.h>
using namespace std;
bool check(long long n, long long pos) { return n & (1LL << pos); }
long long Set(long long n, long long pos) { return n = n | (1LL << pos); }
const long long N = 1000005;
long long arr[N];
pair<long long, long long> t1[N * 4];
struct segtree {
  long long n;
  void treeinit(long long node, long long b, long long e) {
    if (b == e) {
      t1[node] = {arr[b], b};
      return;
    }
    long long left = node * 2;
    long long right = (node * 2) + 1;
    long long mid = (b + e) / 2;
    treeinit(left, b, mid);
    treeinit(right, mid + 1, e);
    if (t1[left].first > t1[right].first)
      t1[node] = t1[left];
    else
      t1[node] = t1[right];
  }
  void init(long long _n) {
    n = _n;
    treeinit(1, 1, n);
  }
  void treeinit2(long long node, long long b, long long e) {
    if (b == e) {
      t1[node] = {arr[b], b};
      return;
    }
    long long left = node * 2;
    long long right = (node * 2) + 1;
    long long mid = (b + e) / 2;
    treeinit2(left, b, mid);
    treeinit2(right, mid + 1, e);
    if (t1[left].first < t1[right].first)
      t1[node] = t1[left];
    else
      t1[node] = t1[right];
  }
  void init2(long long _n) {
    n = _n;
    treeinit2(1, 1, n);
  }
  pair<long long, long long> treequery1(long long node, long long b,
                                        long long e, long long i, long long j) {
    if (i > e || j < b) {
      return {-LLONG_MAX, -1};
    }
    if (b >= i && e <= j) {
      return t1[node];
    }
    long long left = node * 2;
    long long right = (node * 2) + 1;
    long long mid = (b + e) / 2;
    pair<long long, long long> p1 = treequery1(left, b, mid, i, j);
    pair<long long, long long> p2 = treequery1(right, mid + 1, e, i, j);
    if (p1.first < p2.first) {
      return p2;
    }
    return p1;
  }
  pair<long long, long long> querymx(long long x, long long y) {
    return treequery1(1, 1, n, x, y);
  }
  pair<long long, long long> treequery2(long long node, long long b,
                                        long long e, long long i, long long j) {
    if (i > e || j < b) {
      return {LLONG_MAX, -1};
    }
    if (b >= i && e <= j) {
      return t1[node];
    }
    long long left = node * 2;
    long long right = (node * 2) + 1;
    long long mid = (b + e) / 2;
    pair<long long, long long> p1 = treequery2(left, b, mid, i, j);
    pair<long long, long long> p2 = treequery2(right, mid + 1, e, i, j);
    if (p1.first > p2.first) {
      return p2;
    }
    return p1;
  }
  pair<long long, long long> querymn(long long x, long long y) {
    return treequery2(1, 1, n, x, y);
  }
} seg;
long long solvemx(long long l, long long r) {
  if (l > r) return 0;
  if (l == r) {
    return arr[l];
  }
  long long ret = 0;
  pair<long long, long long> ask = seg.querymx(l, r);
  long long lft = (ask.second - l + 1);
  long long rgt = (r - ask.second + 1);
  ret += (lft * rgt * ask.first);
  ret += solvemx(l, ask.second - 1) + solvemx(ask.second + 1, r);
  return ret;
}
long long solvemn(long long l, long long r) {
  if (l > r) return 0;
  if (l == r) {
    return arr[l];
  }
  long long ret = 0;
  pair<long long, long long> ask = seg.querymn(l, r);
  long long lft = (ask.second - l + 1);
  long long rgt = (r - ask.second + 1);
  ret += (lft * rgt * ask.first);
  ret += solvemn(l, ask.second - 1) + solvemn(ask.second + 1, r);
  return ret;
}
int main() {
  long long i, j, k, l, m, n, o, r, q;
  long long testcase;
  long long input, flag, tag, ans;
  while (cin >> n) {
    for (long long i = 1; i <= n; i++) cin >> arr[i];
    seg.init(n);
    ans = solvemx(1, n);
    seg.init2(n);
    ans -= solvemn(1, n);
    cout << ans << "\n";
  }
}

### 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;
bool check(long long n, long long pos) { return n & (1LL << pos); }
long long Set(long long n, long long pos) { return n = n | (1LL << pos); }
const long long N = 1000005;
long long arr[N];
pair<long long, long long> t1[N * 4];
struct segtree {
  long long n;
  void treeinit(long long node, long long b, long long e) {
    if (b == e) {
      t1[node] = {arr[b], b};
      return;
    }
    long long left = node * 2;
    long long right = (node * 2) + 1;
    long long mid = (b + e) / 2;
    treeinit(left, b, mid);
    treeinit(right, mid + 1, e);
    if (t1[left].first > t1[right].first)
      t1[node] = t1[left];
    else
      t1[node] = t1[right];
  }
  void init(long long _n) {
    n = _n;
    treeinit(1, 1, n);
  }
  void treeinit2(long long node, long long b, long long e) {
    if (b == e) {
      t1[node] = {arr[b], b};
      return;
    }
    long long left = node * 2;
    long long right = (node * 2) + 1;
    long long mid = (b + e) / 2;
    treeinit2(left, b, mid);
    treeinit2(right, mid + 1, e);
    if (t1[left].first < t1[right].first)
      t1[node] = t1[left];
    else
      t1[node] = t1[right];
  }
  void init2(long long _n) {
    n = _n;
    treeinit2(1, 1, n);
  }
  pair<long long, long long> treequery1(long long node, long long b,
                                        long long e, long long i, long long j) {
    if (i > e || j < b) {
      return {-LLONG_MAX, -1};
    }
    if (b >= i && e <= j) {
      return t1[node];
    }
    long long left = node * 2;
    long long right = (node * 2) + 1;
    long long mid = (b + e) / 2;
    pair<long long, long long> p1 = treequery1(left, b, mid, i, j);
    pair<long long, long long> p2 = treequery1(right, mid + 1, e, i, j);
    if (p1.first < p2.first) {
      return p2;
    }
    return p1;
  }
  pair<long long, long long> querymx(long long x, long long y) {
    return treequery1(1, 1, n, x, y);
  }
  pair<long long, long long> treequery2(long long node, long long b,
                                        long long e, long long i, long long j) {
    if (i > e || j < b) {
      return {LLONG_MAX, -1};
    }
    if (b >= i && e <= j) {
      return t1[node];
    }
    long long left = node * 2;
    long long right = (node * 2) + 1;
    long long mid = (b + e) / 2;
    pair<long long, long long> p1 = treequery2(left, b, mid, i, j);
    pair<long long, long long> p2 = treequery2(right, mid + 1, e, i, j);
    if (p1.first > p2.first) {
      return p2;
    }
    return p1;
  }
  pair<long long, long long> querymn(long long x, long long y) {
    return treequery2(1, 1, n, x, y);
  }
} seg;
long long solvemx(long long l, long long r) {
  if (l > r) return 0;
  if (l == r) {
    return arr[l];
  }
  long long ret = 0;
  pair<long long, long long> ask = seg.querymx(l, r);
  long long lft = (ask.second - l + 1);
  long long rgt = (r - ask.second + 1);
  ret += (lft * rgt * ask.first);
  ret += solvemx(l, ask.second - 1) + solvemx(ask.second + 1, r);
  return ret;
}
long long solvemn(long long l, long long r) {
  if (l > r) return 0;
  if (l == r) {
    return arr[l];
  }
  long long ret = 0;
  pair<long long, long long> ask = seg.querymn(l, r);
  long long lft = (ask.second - l + 1);
  long long rgt = (r - ask.second + 1);
  ret += (lft * rgt * ask.first);
  ret += solvemn(l, ask.second - 1) + solvemn(ask.second + 1, r);
  return ret;
}
int main() {
  long long i, j, k, l, m, n, o, r, q;
  long long testcase;
  long long input, flag, tag, ans;
  while (cin >> n) {
    for (long long i = 1; i <= n; i++) cin >> arr[i];
    seg.init(n);
    ans = solvemx(1, n);
    seg.init2(n);
    ans -= solvemn(1, n);
    cout << ans << "\n";
  }
}

```