n=int(input()) arr=list(map(int,input().split())) if arr.count(0)!=0: print(-1) elif arr.count(1)+arr.count(2)+arr.count(3)!=n: print(10**9+7) else: mod=10**9+7 ans=1 for val in arr: if val==1: ans*=1 elif val==2: ans*=4 ans%=mod elif val==3: ans*=729 ans%=mod print(ans)