N=int(input()) A=list(map(int, input().split())) mod=10**9+7 if min(A)==0: print(-1) D=[1,1,4,729] d=1 for i in A: if i>4: print(0) exit() else: d*=D[i] print(mod%d)