MX = 10**9+7 n = int(input()) *a, = map(int, input().split()) if min(a) == 0: print(-1) elif max(a) > 3: print(MX) else: v = [1, 4, 729] ans = 1 for ai in a: ans *= v[ai-1] if ans > MX: print(MX) break else: print(MX % ans)