package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	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
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}
	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	pp := make([]int, n)

	for i := 0; i < n; i++ {
		pp[getNextInt(scanner)-1] = i
	}

	seg := Segment{}

	seg.init(n)

	var ans int64
	for i := n; i > 0; i-- {
		seg.add(pp[i-1], 1)
		l := seg.nextLeft(pp[i-1])
		ll := seg.nextLeft(l)
		r := seg.nextRight(pp[i-1])
		rr := seg.nextRight(r)
		ans += int64(pp[i-1]-l) * int64(rr-r) * int64(i)
		ans += int64(r-pp[i-1]) * int64(l-ll) * int64(i)
	}

	fmt.Fprintln(writer, ans)
	writer.Flush()
}

// Segment ...
type Segment struct {
	n, h, i int
	unit    []int
	bucket  [][]int
}

func (seg *Segment) init(n int) {
	seg.n = n
	seg.unit = make([]int, 1)
	seg.unit[0] = 1
	seg.bucket = make([][]int, 1)
	seg.bucket[0] = make([]int, n)

	chunk := 8
	i := 0
	for n > chunk+1 {
		n = (n-1)/chunk + 1
		seg.bucket = append(seg.bucket, make([]int, n))
		seg.unit = append(seg.unit, seg.unit[i]*chunk)
		i++
	}
	seg.h = len(seg.unit)
}

func (seg *Segment) add(index, value int) {
	for i := 0; i < seg.h; i++ {
		seg.bucket[i][index/seg.unit[i]] += value
	}
}

func (seg *Segment) minimize(index int) {
	raw := seg.bucket[0]
	for h := 1; h < seg.h; h++ {
		if seg.bucket[h][index/seg.unit[h]] > raw[index] {
			seg.bucket[h][index/seg.unit[h]] = raw[index]
		}
	}
}

func (seg *Segment) min(l, r int) int {
	if seg.n < r {
		r = seg.n
	}
	mn := math.MaxInt32
	for index := l; index < r; index += seg.unit[seg.i] {
		for seg.i = 0; seg.i < seg.h-1 && index%seg.unit[seg.i+1] == 0; seg.i++ {
			if index+seg.unit[seg.i+1] >= r {
				break
			}
		}
		if mn > seg.bucket[seg.i][index/seg.unit[seg.i]] {
			mn = seg.bucket[seg.i][index/seg.unit[seg.i]]
		}
	}

	return mn
}

func (seg *Segment) nextLeft(index int) int {
	raw := seg.bucket[0]
	for index = index - 1; index >= 0; index -= seg.unit[seg.i] {
		if raw[index] > 0 {
			return index
		}
		for seg.i = 0; seg.i < seg.h-1 && (index+1)%seg.unit[seg.i+1] == 0; seg.i++ {
			if seg.bucket[seg.i+1][index/seg.unit[seg.i+1]] != 0 {
				break
			}
		}
	}

	return -1
}

func (seg *Segment) nextRight(index int) int {
	raw := seg.bucket[0]
	for index = index + 1; index < seg.n; index += seg.unit[seg.i] {
		if raw[index] > 0 {
			return index
		}
		for seg.i = 0; seg.i < seg.h-1 && index%seg.unit[seg.i+1] == 0; seg.i++ {
			if seg.bucket[seg.i+1][index/seg.unit[seg.i+1]] != 0 {
				break
			}
		}
	}

	return seg.n
}


Measured memory_bytes: 3456