N=int(input()) A=list(map(int,input().split())) from collections import Counter one,two=0,0 other=0 l=[[0]*3 for i in range(N+1)] for i in range(N): l[i+1][0]=l[i][0] l[i+1][2]=l[i][2] l[i+1][1]=l[i][1] if A[i]==1: l[i+1][0]+=1 elif A[i]==2: l[i+1][1]+=1 else: l[i+1][2]+=1 ans=0 for i,j in enumerate(A): tmp_l=l[i] if j==1: ans+=tmp_l[0]*2+tmp_l[1]*3+tmp_l[2]*2 elif j==2: ans+=tmp_l[0]*3+tmp_l[1]+tmp_l[2] else: ans+=tmp_l[0]*2+tmp_l[1]+tmp_l[2] print(ans)