#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let n = read::<usize>();
    let p = read_vec::<i64>();

    let mut pairs = vec![];
    for i in 0..n {
        pairs.push((p[i], i));
    }
    pairs.sort();
    pairs.reverse();

    let poses_order = pairs.iter().map(|&x| x.1).collect::<Vec<_>>();
    let mut next_r = vec![n as i64; n];
    let mut next_l = vec![-1; n];
    let mut next_next_r = vec![n as i64; n];
    let mut next_next_l = vec![-1; n];

    let mut s = BTreeSet::new();
    for pos in poses_order {
        let mut after = s.range((Excluded(pos), Unbounded));
        if let Some(&x) = after.next() {
            next_r[pos] = x as i64;
            if let Some(&x) = after.next() {
                next_next_r[pos] = x as i64;
            }
        }
        let mut before = s.range((Unbounded, Excluded(pos))).rev();
        if let Some(&x) = before.next() {
            next_l[pos] = x as i64;
            if let Some(&x) = before.next() {
                next_next_l[pos] = x as i64;
            }
        }
        s.insert(pos);
    }
    debug!(next_r);
    debug!(next_l);

    let mut ans = 0;
    for i in 0..n {
        let temp;
        if next_l[i] == -1 && next_r[i] == n as i64 {
            temp = 0;
        } else if next_r[i] == n as i64 {
            temp = p[i] * (n - i) as i64 * (next_l[i] - next_next_l[i]);
        } else if next_l[i] == -1 {
            temp = p[i] * (i + 1) as i64 * (next_next_r[i] - next_r[i]);
        } else {
            let temp1 = p[i] * (i as i64 - next_l[i]) * (next_next_r[i] - next_r[i]);
            let temp2 = p[i] * (next_r[i] - i as i64) * (next_l[i] - next_next_l[i]);
            // debug!(temp1, temp2);
            temp = temp1 + temp2;
        }
        // ans += p[i] *
        //debug!(temp);
        ans += temp;
    }
    println!("{}", ans);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

use std::cmp::Ordering;

pub trait BinarySearch<T> {
    fn lower_bound(&self, x: &T) -> usize;
    fn upper_bound(&self, x: &T) -> usize;
}

impl<T: Ord> BinarySearch<T> for [T] {
    fn lower_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();

        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less => {
                    low = mid + 1;
                }
                Ordering::Equal | Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }

    fn upper_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();

        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less | Ordering::Equal => {
                    low = mid + 1;
                }
                Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
}


Measured memory_bytes: 10984