def main(): from sys import stdin import math readline=stdin.readline n = int(readline()) A = list(map(int, readline().split())) mod = 10**9 + 7 ans = 1 if 0 in A: print(-1) exit() for a in A: if a >= 4: print(mod) exit() elif a == 1: continue elif a == 2: ans *= 4 elif a == 3: ans *= 729 if ans > mod: print(mod) exit() print(mod % ans) main()