import sys from collections import deque, Counter input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) n = ii() a = li() ans = 1 p = [0, 1, 4, 729] for i in range(n): if a[i] > 4: print(10 ** 9 + 7) exit() else: ans *= p[a[i]] if ans > 10 ** 9 + 7: print(10 ** 9 + 7) exit() if ans == 0: print(-1) else: print((10 ** 9 + 7) % ans)