N=int(input()) A=list(map(int,input().split())) if max(A)>=4: print(10**9+7) exit() T=[0,1,4,729] X=1 for a in A: X*=T[a] if (10**9+7)%X==0: print(-1) else: print((10**9+7)%X)