mod = 10**9+7 n = int(input()) A = list(map(int,input().split())) prod = [1,4,3**6] ans = 1 for a in A: if a == 0: print(-1) exit() if a >= 4: print(mod) exit() ans *= prod[a-1] print(mod%ans)