import algorithm, bitops, deques, heapqueue, math, macros, sets, sequtils, strformat, strutils, sugar, tables proc input*(): string {.inline.} = stdin.readLine proc inputs*(): seq[string] {.inline.} = stdin.readLine.split proc inputInt*(): int {.inline.} = stdin.readLine.parseInt proc inputInts*(): seq[int] {.inline.} = stdin.readLine.split.map(parseInt) proc chmax*[T: SomeNumber](n: var T, m: T) {.inline.} = n = max(n, m) proc chmin*[T: SomeNumber](n: var T, m: T) {.inline.} = n = min(n, m) proc `%=`*[T: SomeInteger](n: var T, m: T) {.inline.} = n = floorMod(n, m) when isMainModule: var N = inputInt() A = inputInts() var ones, twos: int for ai in A: if ai == 1: ones.inc elif ai == 2: twos.inc var ans = 0 for i, ai in A: if ai == 1: ones.dec ans += 3*twos + 2*(N - twos - (i + 1)) elif ai == 2: twos.dec ans += 3*ones + N - ones - (i + 1) else: ans += 2*ones + N - ones - (i + 1) echo ans