{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE BangPatterns          #-}
{-# LANGUAGE CPP                   #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE MagicHash             #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE MultiWayIf            #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE TupleSections         #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE ViewPatterns          #-}

import           Control.Applicative
import           Control.Exception
import           Control.Monad
import           Control.Monad.Primitive
import           Control.Monad.ST
import           Control.Monad.State.Strict
import           Data.Bool
import qualified Data.ByteString             as B
import qualified Data.ByteString.Builder     as B
import qualified Data.ByteString.Char8       as C
import qualified Data.ByteString.Internal    as B
import qualified Data.ByteString.Unsafe      as B
import           Data.Char
import qualified Data.Foldable               as F
import           Data.Function
import qualified Data.IntMap.Strict          as IM
import qualified Data.IntSet                 as IS
import qualified Data.List                   as L
import qualified Data.Map.Strict             as M
import           Data.Monoid
import           Data.Ord
import           Data.Primitive.MutVar
import qualified Data.Set                    as S
import           Data.Tuple
import qualified Data.Vector                 as V
import qualified Data.Vector.Generic         as G
import qualified Data.Vector.Generic.Mutable as GM
import qualified Data.Vector.Unboxed         as U
import qualified Data.Vector.Unboxed.Mutable as UM
import           Debug.Trace
import           Foreign                     hiding (void)
import           GHC.Exts
import qualified System.IO                   as IO
import           Unsafe.Coerce

main :: IO ()
main = do
    n <- readLn :: IO Int
    xs <- U.unfoldrN n parseInt <$> C.getLine
    print $ solve n xs

solve :: Int -> U.Vector Int -> Int
solve n xs = runST $ do
    ft <- newFenwickTree (n + 1) :: ST s (FenwickTree s Int)
    res <- UM.replicate 1 (0 :: Int)
    addFT ft (rxs U.! n) 1
    rev n $ \i -> do
        let pos = rxs U.! i
        freqL <- sumToFT ft pos
        resL <- case freqL of
            0 -> return 0
            _ -> do
                maxL <- fmap pred . lowerBoundM 0 pos $ \j -> do
                    (< 1) <$> sumFromToFT ft j pos
                l <- lowerBoundM 0 maxL $ \j -> do
                    (< 2) <$> sumFromToFT ft j pos
                r <- upperBoundM maxL (n - 1) $ \j -> do
                    (< 1) <$> sumFromToFT ft pos (j + 1)
                -- traceShowM ("i",i,"l",l,"maxL",maxL,"pos",pos,"r",r)
                return $! (maxL - l + 1) * (r - pos + 1)
        freqR <- sumFromToFT ft pos (n + 1)
        resR <- case freqR of
            0 -> return 0
            _ -> do
                maxR <- fmap succ . upperBoundM pos n $ \j -> do
                    (< 1) <$> sumFromToFT ft pos (j + 1)
                l <- lowerBoundM 0 maxR $ \j -> do
                    (< 1) <$> sumFromToFT ft j pos
                r <- upperBoundM maxR (n - 1) $ \j -> do
                    (< 2) <$> sumFromToFT ft pos (j + 1)
                -- traceShowM ("i",i,"l",l,"pos",pos,"maxR",maxR,"r",r)
                return $! (pos - l + 1) * (r - maxR + 1)
        let resi = (resL + resR) * i
        UM.unsafeModify res (+resi) 0
        addFT ft pos 1
    UM.unsafeRead res 0
  where
    rxs = U.accumulate (flip const) (U.replicate (n + 1) 0)
        $ U.imap (flip (,)) xs


-------------------------------------------------------------------------------
rep, rev :: Monad m => Int -> (Int -> m ()) -> m ()
rep !n = U.forM_ $ U.generate n id
rev !n = U.forM_ $ U.iterateN n (subtract 1) (n - 1)
{-# INLINE rep #-}
{-# INLINE rev #-}

lowerBoundM :: (Integral i, Monad m) => i -> i -> (i -> m Bool) -> m i
lowerBoundM low high p = go low high
  where
    go !low !high
        | high <= low = return high
        | otherwise = do
            pmid <- p mid
            if pmid
            then go low mid
            else go (mid + 1) high
      where
        h = toInteger high
        l = toInteger low
        mid = fromIntegral $ l + div (h - l) 2

{-# INLINE lowerBoundM #-}

upperBoundM :: (Integral i, Monad m) => i -> i -> (i -> m Bool) -> m i
upperBoundM low high p = do
    phigh <- p high
    if phigh
    then return high
    else subtract 1 <$> lowerBoundM low high (fmap not.p)
{-# INLINE upperBoundM #-}


type Parser a = C.ByteString -> Maybe (a, C.ByteString)

parseInt :: Parser Int
parseInt = C.readInt . C.dropWhile isSpace

parseInt2 :: Parser (Int, Int)
parseInt2 = runStateT $
    (,) <$> StateT (C.readInt . C.dropWhile isSpace)
        <*> StateT (C.readInt . B.unsafeTail)

parseInt3 :: Parser (Int, Int, Int)
parseInt3 = runStateT $
    (,,) <$> StateT (C.readInt . C.dropWhile isSpace)
        <*> StateT (C.readInt . B.unsafeTail)
        <*> StateT (C.readInt . B.unsafeTail)

parseInt4 :: Parser (Int, Int, Int, Int)
parseInt4 = runStateT $
    (,,,) <$> StateT (C.readInt . C.dropWhile isSpace)
        <*> StateT (C.readInt . B.unsafeTail)
        <*> StateT (C.readInt . B.unsafeTail)
        <*> StateT (C.readInt . B.unsafeTail)

newtype FenwickTree s a = FenwickTree {getFenwickTree :: UM.MVector s a}

newFenwickTree :: (PrimMonad m, U.Unbox a, Num a) => Int -> m (FenwickTree (PrimState m) a)
newFenwickTree n = FenwickTree <$> UM.replicate (n + 1) 0

-- | sum [0..k)
sumToFT :: (PrimMonad m, U.Unbox a, Num a)
    => FenwickTree (PrimState m) a -> Int -> m a
sumToFT (FenwickTree ft) k = go 0 k
  where
    go !acc !i
        | i > 0 = do
            xi <- UM.unsafeRead ft i
            go (acc + xi) (i - (i .&. (-i)))
        | otherwise = return acc
{-# INLINE sumToFT #-}

-- | sum [l..r)
sumFromToFT :: (PrimMonad m, U.Unbox a, Num a)
    => FenwickTree (PrimState m) a -> Int -> Int -> m a
sumFromToFT ft l r = (-) <$> sumToFT ft r <*> sumToFT ft l
{-# INLINE sumFromToFT #-}

addFT :: (PrimMonad m, U.Unbox a, Num a)
    => FenwickTree (PrimState m) a -> Int -> a -> m ()
addFT (FenwickTree ft) k v = flip fix (k + 1) $ \loop !i -> do
    when (i < n) $ do
        UM.unsafeModify ft (+v) i
        loop $ i + (i .&. (-i))
  where
    n = UM.length ft
{-# INLINE addFT #-}
