package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}
func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0
	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 3
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}
	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	pp := make(permutations, n)
	for i := 0; i < n; i++ {
		pp[i].p = getNextInt(scanner)
		pp[i].i = i
	}
	sort.Sort(pp)
	dec := newDecomposition(n)
	var ans int64
	for i := 0; i < n; i++ {
		l := dec.kleft(0, pp[i].i, 1)
		ll := dec.kleft(0, pp[i].i, 2)
		r := dec.kright(pp[i].i, n, 1)
		rr := dec.kright(pp[i].i, n, 2)
		ans += int64(l-ll) * int64(r-pp[i].i) * int64(pp[i].p)
		ans += int64(rr-r) * int64(pp[i].i-l) * int64(pp[i].p)
		dec.add(pp[i].i, 1)
	}
	fmt.Fprintln(writer, ans)
}

type permutation struct {
	p, i int
}
type permutations []permutation

func (a permutations) Len() int           { return len(a) }
func (a permutations) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a permutations) Less(i, j int) bool { return a[i].p > a[j].p }

type decomposition struct {
	chunk  int
	bucket [][]int
}

func newDecomposition(n int) decomposition {
	bucket := make([][]int, 1)
	bucket[0] = make([]int, n)
	chunk := 8
	for i := 0; n > 1; i++ {
		n = (n-1)/chunk + 1
		bucket = append(bucket, make([]int, n))
	}
	return decomposition{
		chunk:  chunk,
		bucket: bucket,
	}
}
func (dec *decomposition) upleft(l int) int {
	return (l + dec.chunk - 1) / dec.chunk
}
func (dec *decomposition) upright(r int) int {
	return r / dec.chunk
}
func (dec *decomposition) add(index int, value int) {
	h := len(dec.bucket)
	for i := 0; i < h; i++ {
		dec.bucket[i][index] += value
		index /= dec.chunk
	}
}
func (dec *decomposition) rangesum(l, r int) int {
	return dec.rangesumh(l, r, 0)
}
func (dec *decomposition) rangesumh(l, r, h int) int {
	var sum int
	ll := dec.upleft(l)
	rr := dec.upright(r)
	for i := l; i < r && i < ll*dec.chunk; i++ {
		sum += dec.bucket[h][i]
	}
	if ll < rr {
		sum += dec.rangesumh(ll, rr, h+1)
	}
	for i := rr * dec.chunk; i < r && ll <= rr; i++ {
		sum += dec.bucket[h][i]
	}
	return sum
}
func (dec *decomposition) kright(l, r, k int) int {
	return dec.krighth(l, r, 0, &k)
}
func (dec *decomposition) krighth(l, r, h int, k *int) int {
	ll := dec.upleft(l)
	rr := dec.upright(r)
	for i := l; i < r && i < ll*dec.chunk; i++ {
		if *k > dec.bucket[h][i] {
			*k -= dec.bucket[h][i]
			continue
		}
		return i
	}
	if ll < rr {
		rr = dec.krighth(ll, rr, h+1, k)
	}
	for i := rr * dec.chunk; i < r && ll <= rr; i++ {
		if *k > dec.bucket[h][i] {
			*k -= dec.bucket[h][i]
			continue
		}
		return i
	}

	return r
}
func (dec *decomposition) kleft(l, r, k int) int {
	return dec.klefth(l, r, 0, &k)
}
func (dec *decomposition) klefth(l, r, h int, k *int) int {
	ll := dec.upleft(l)
	rr := dec.upright(r)
	for i := r - 1; i >= 0 && i >= rr*dec.chunk; i-- {
		if *k > dec.bucket[h][i] {
			*k -= dec.bucket[h][i]
			continue
		}
		return i
	}
	if ll < rr {
		ll = dec.klefth(ll, rr, h+1, k) + 1
	}
	for i := ll*dec.chunk - 1; i >= 0 && i >= l && ll <= rr; i-- {
		if *k > dec.bucket[h][i] {
			*k -= dec.bucket[h][i]
			continue
		}
		return i

	}
	return -1
}


Measured memory_bytes: 4352