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