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