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