#include <bits/stdc++.h>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int extgcd(int a, int b, int &x, int &y) { int g = a; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; }

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a)  begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

const int MAX_N = 100005;
int N;
int P[MAX_N];

class Node {
public:
    Node(LL v = unitValue) : value(v) {

    }

    Node operator + (const Node& monoid) const {
        return Node(max(this->getValue(), monoid.getValue()));
    }

    static Node unit() {
        return Node(unitValue);
    }

    void setValue(LL l) {
        this->value = l;
    }

    LL getValue() const {
        return this->value;
    }

private:
    LL value;
    static const LL unitValue = -INF_LL;
};

template <class T>
class SegmentTree {
public:
    SegmentTree(int size) {
        init(size);
    }

    void init(int size) {
        vec.clear();
        k = 1;
        while(k < size) {
            k *= 2;
        }
        for(int i = 0; i < 2*k; i++) {
            vec.push_back(T::unit());
        }
    }

    void update(int i, T x) { //iをxに変更する
        i += k-1;
        vec[i] = x;
        while(i > 0) {
            i = (i-1)/2;
            vec[i] = vec[i*2+1] + vec[i*2+2];
        }
    }

    T find(int l, int r) {
        return _find(l, r, 0, 0, k);
    }

private:
    vector<T> vec;
    int k;

    T _find(int l, int r, int k, int x, int y) {
        if (r <= x || y <= l) { //重なってない場合
            return T::unit();
        } else if (l <= x && y <= r) { //完全にかぶってる場合
            return vec[k];
        } else { //中途半端にかぶってる場合
            int mid = (x+y)/2;
            T lNode = _find(l, r, 2*k+1, x, mid);
            T rNode = _find(l, r, 2*k+2, mid, y);
            return lNode + rNode;
        }
    }
};

SegmentTree<Node> sg(MAX_N);

int solve()
{
  int ret = 0;
  REP(n,N) {
    int l, r, rr;

    int i = -1, j = n;
    while(j-i>1) {
      int mid = (i+j)/2;
      if(sg.find(mid, n).getValue() < P[n]) {
        j = mid;
      } else {
        i = mid;
      }
    }
    l = i;

    if(P[n+1] > P[n]) {
      r = n+1;
    } else {
      i = n+1, j = N;
      while(j-i>1) {
        int mid = (i+j)/2;
        if(sg.find(n+1, mid+1).getValue() < P[n]) {
          i = mid;
        } else {
          j = mid;
        }
        if(n == 5) {
          // cerr << i << " " << j << endl;
        }
      }
      r = min(N, j);
      if(n == 5) {
        // cerr << i << " " << j << endl;
      }
    }

    if(P[r+1] > P[n]) {
      rr = r+1;
    } else {
      i = r+1, j = N+1;
      while(j-i>1) {
        int mid = (i+j)/2;
        if(sg.find(r+1, mid+1).getValue() < P[n]) {
          i = mid;
        } else {
          j = mid;
        }
      }
      rr = min(N, j);
    }

    ret += (n-l) * (rr-r) * P[n];
    // cerr << n << " " << P[n] << " " << l << " " << r << " " << rr << endl;
    // cerr << (n-l) * (rr-r) * P[n] << endl;

  }
  return ret;
}

signed main()
{
  cin >> N;
  REP(n,N) {
    cin >> P[n];
    sg.update(n,Node(P[n]));
  }

  int ans = solve();

  reverse(P, P+N);
  REP(n,N) {
    sg.update(n, Node(P[n]));
  }

  ans += solve();

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 3184