n, *a = map(int, open(0).read().split()) mx = max(a) mn = min(a) mod = 10 ** 9 + 7 if mx > 3: print(mod) elif mn == 0: print(-1) else: b = [0, 1, 16, 729] ans = 1 for c in a: ans *= b[c] if ans > mod: print(mod) print(mod % ans)