{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE Strict #-}
{-# LANGUAGE StrictData #-}
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import Data.Array.Unboxed
import Data.Array.ST
import qualified Data.ByteString.Char8 as BS
import Control.Monad
import Data.List
import Data.Char
import qualified Data.Map.Strict as M

main :: IO ()
main = do
    getLine
    xs <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
    let solve :: Int -> [Int] -> [Int] -> Int
        solve count [] _ = count
        solve count _ [] = count
        solve count (x:xs) (y:ys) 
            | x == y = let (x1,x2) = span (== x) (x:xs); (y1,y2) = span (== y) (y:ys) in solve (count+(length x1)*(length y1)) x2 y2
            | x > y  = solve count xs (y:ys)
            | x < y  = solve count (x:xs) ys
    print $ solve 0 (sortBy (\j k -> compare k j) (zipWith (+) [0,1..] xs)) (sortBy (\j k -> compare k j) (zipWith (-) [0,1..] xs))