#include <bits/stdc++.h>
int main(void) {
  int n;
  scanf("%d", &n);
  std::list<std::pair<int, int>> minima, maxima;
  minima.push_back(std::make_pair(-1, 0));
  maxima.push_back(std::make_pair((1 << 30), 0));
  long long minima_delta = 0;
  long long maxima_delta = 0;
  long long grand_total = 0;
  for (int r = 1; r <= n; r++) {
    int a;
    scanf("%d", &a);
    if (true) {
      while (a <= minima.front().first) {
        auto second = (++minima.begin());
        minima_delta -= (long long)(minima.front().second - second->second) *
                        minima.front().first;
        minima.pop_front();
      }
      minima.push_front(std::make_pair(a, r));
      auto second = (++minima.begin());
      minima_delta += (long long)(minima.front().second - second->second) *
                      minima.front().first;
    }
    if (true) {
      while (a >= maxima.front().first) {
        auto second = (++maxima.begin());
        maxima_delta -= (long long)(maxima.front().second - second->second) *
                        maxima.front().first;
        maxima.pop_front();
      }
      maxima.push_front(std::make_pair(a, r));
      auto second = (++maxima.begin());
      maxima_delta += (long long)(maxima.front().second - second->second) *
                      maxima.front().first;
    }
    grand_total += maxima_delta - minima_delta;
  }
  printf("%lld\n", grand_total);
  return 0;
}
