import sys sys.setrecursionlimit(10 ** 6) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LC(): return list(input()) def IC(): return [int(c) for c in input()] def MI(): return map(int, sys.stdin.readline().split()) INF = float('inf') MOD = 10**9 + 7 def solve(): N = II() A = LI() One = 0 Two = 0 Three = 0 Other = 0 for a in A: if(a==1): One+=1 elif(a==2): Two+=1 elif(a==3): Three+=1 else: Other+=1 import math def combinations_count(n, r): return math.factorial(n) // (math.factorial(n - r) * math.factorial(r)) Thrid = One*Two Second = combinations_count(One, 2) + One*Three First = (N*(N-1))//2 - Thrid -Second print(3*Thrid+2*Second + First) return solve()