#include using namespace std; typedef long long ll; const ll mod = 1000000007; int main() { int N; cin >> N; ll A[N]; bool flag0 = false; bool flag4 = false; for (int i = 0; i < N; i++) { cin >> A[i]; if (A[i] == 0) flag0 = true; if (A[i] >= 4) flag4 = true; } if (flag0) { cout << "-1\n"; return 0; } if (flag4) { cout << mod << "\n"; return 0; } ll ans = 1; int cnt = 0; ll b[3] = {1, 4, 729}; for (int i = 0; i < N; i++) { ans *= b[A[i] - 1]; if (ans > mod) break; } cout << (mod % ans) << "\n"; return 0; }