MOD = 10 ** 9 + 7 N = int(input()) A = list(map(int, input().split())) ans = 1 for a in A: if a == 0: print(-1) exit() elif a == 2: ans *= 4 elif a == 3: ans *= 729 elif a >= 4: print(MOD) exit() if ans > MOD: print(MOD) exit() print(MOD % ans)