#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 9;
const long long mod = 1e9 + 7;
vector<bool> prime(MAX, 1);
vector<int> spf(MAX, 1), primes;
void sieve() {
  prime[0] = prime[1] = 0;
  spf[2] = 2;
  for (long long i = 4; i < MAX; i += 2) {
    spf[i] = 2;
    prime[i] = 0;
  }
  primes.push_back(2);
  for (long long i = 3; i < MAX; i += 2) {
    if (prime[i]) {
      primes.push_back(i);
      spf[i] = i;
      for (long long j = i * i; j < MAX; j += i) {
        prime[j] = 0;
        if (spf[j] == 1) {
          spf[j] = i;
        }
      }
    }
  }
}
template <typename T>
void ps(T x) {
  cout << x << " ";
}
template <typename T>
void ps(const vector<T> &x, int n) {
  for (int i = 0; i < n; i++) {
    cout << x[i];
    (i == n - 1) ? cout << '\n' : cout << " ";
  }
}
template <typename T>
void pl(T x) {
  cout << x << '\n';
}
template <typename T>
void pl(const vector<T> &x, int n) {
  for (int i = 0; i < n; i++) {
    cout << x[i] << "\n";
  }
}
template <typename T>
T power(T a, T b) {
  T res = 1;
  while (b) {
    if (b & 1) {
      res = res * a;
    }
    a = a * a;
    b = b >> 1;
  }
  return res;
}
template <typename T>
T power(T a, T b, T m) {
  T res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % m;
    }
    a = (a * a) % m;
    b = b >> 1;
  }
  return res % m;
}
void _IOE() {}
class Main {
 public:
  void _main() {
    long long n;
    cin >> n;
    vector<long long> v(n);
    for (long long i = 0; i < n; i++) cin >> v[i];
    stack<long long> s;
    vector<long long> l(n), r(n);
    for (long long i = 0; i < n; i++) {
      while (!s.empty() && v[s.top()] < v[i]) {
        s.pop();
      }
      if (s.empty())
        l[i] = -1;
      else
        l[i] = s.top();
      s.push(i);
    }
    while (s.empty() == 0) s.pop();
    for (long long i = n - 1; i >= 0; i--) {
      while (!s.empty() && v[s.top()] <= v[i]) {
        s.pop();
      }
      if (s.empty())
        r[i] = n;
      else
        r[i] = s.top();
      s.push(i);
    }
    long long ans = 0;
    for (long long i = 0; i < n; i++) {
      ans += (abs(i - l[i]) * abs(r[i] - i) * v[i]) - v[i];
    }
    while (s.empty() == 0) s.pop();
    for (long long i = 0; i < n; i++) {
      while (!s.empty() && v[s.top()] > v[i]) {
        s.pop();
      }
      if (s.empty())
        l[i] = -1;
      else
        l[i] = s.top();
      s.push(i);
    }
    while (s.empty() == 0) s.pop();
    for (long long i = n - 1; i >= 0; i--) {
      while (!s.empty() && v[s.top()] >= v[i]) {
        s.pop();
      }
      if (s.empty())
        r[i] = n;
      else
        r[i] = s.top();
      s.push(i);
    }
    for (long long i = 0; i < n; i++) {
      ans -= (abs(i - l[i]) * abs(r[i] - i) * v[i]) - v[i];
    }
    pl(ans);
  }
};
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL),
      cerr.tie(NULL);
  _IOE();
  long long test_cases = 1;
  Main obj;
  for (long long i = 1; i <= test_cases; i++) {
    obj._main();
  }
  return 0;
}

### Prompt

In Cpp, your task is to solve 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;
const int MAX = 2e5 + 9;
const long long mod = 1e9 + 7;
vector<bool> prime(MAX, 1);
vector<int> spf(MAX, 1), primes;
void sieve() {
  prime[0] = prime[1] = 0;
  spf[2] = 2;
  for (long long i = 4; i < MAX; i += 2) {
    spf[i] = 2;
    prime[i] = 0;
  }
  primes.push_back(2);
  for (long long i = 3; i < MAX; i += 2) {
    if (prime[i]) {
      primes.push_back(i);
      spf[i] = i;
      for (long long j = i * i; j < MAX; j += i) {
        prime[j] = 0;
        if (spf[j] == 1) {
          spf[j] = i;
        }
      }
    }
  }
}
template <typename T>
void ps(T x) {
  cout << x << " ";
}
template <typename T>
void ps(const vector<T> &x, int n) {
  for (int i = 0; i < n; i++) {
    cout << x[i];
    (i == n - 1) ? cout << '\n' : cout << " ";
  }
}
template <typename T>
void pl(T x) {
  cout << x << '\n';
}
template <typename T>
void pl(const vector<T> &x, int n) {
  for (int i = 0; i < n; i++) {
    cout << x[i] << "\n";
  }
}
template <typename T>
T power(T a, T b) {
  T res = 1;
  while (b) {
    if (b & 1) {
      res = res * a;
    }
    a = a * a;
    b = b >> 1;
  }
  return res;
}
template <typename T>
T power(T a, T b, T m) {
  T res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % m;
    }
    a = (a * a) % m;
    b = b >> 1;
  }
  return res % m;
}
void _IOE() {}
class Main {
 public:
  void _main() {
    long long n;
    cin >> n;
    vector<long long> v(n);
    for (long long i = 0; i < n; i++) cin >> v[i];
    stack<long long> s;
    vector<long long> l(n), r(n);
    for (long long i = 0; i < n; i++) {
      while (!s.empty() && v[s.top()] < v[i]) {
        s.pop();
      }
      if (s.empty())
        l[i] = -1;
      else
        l[i] = s.top();
      s.push(i);
    }
    while (s.empty() == 0) s.pop();
    for (long long i = n - 1; i >= 0; i--) {
      while (!s.empty() && v[s.top()] <= v[i]) {
        s.pop();
      }
      if (s.empty())
        r[i] = n;
      else
        r[i] = s.top();
      s.push(i);
    }
    long long ans = 0;
    for (long long i = 0; i < n; i++) {
      ans += (abs(i - l[i]) * abs(r[i] - i) * v[i]) - v[i];
    }
    while (s.empty() == 0) s.pop();
    for (long long i = 0; i < n; i++) {
      while (!s.empty() && v[s.top()] > v[i]) {
        s.pop();
      }
      if (s.empty())
        l[i] = -1;
      else
        l[i] = s.top();
      s.push(i);
    }
    while (s.empty() == 0) s.pop();
    for (long long i = n - 1; i >= 0; i--) {
      while (!s.empty() && v[s.top()] >= v[i]) {
        s.pop();
      }
      if (s.empty())
        r[i] = n;
      else
        r[i] = s.top();
      s.push(i);
    }
    for (long long i = 0; i < n; i++) {
      ans -= (abs(i - l[i]) * abs(r[i] - i) * v[i]) - v[i];
    }
    pl(ans);
  }
};
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL),
      cerr.tie(NULL);
  _IOE();
  long long test_cases = 1;
  Main obj;
  for (long long i = 1; i <= test_cases; i++) {
    obj._main();
  }
  return 0;
}

```