#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); int N; cin >> N; ll target = 1e9+7; vector A(N); rep(i,N) cin >> A[i]; if(*max_element(A.begin(), A.end()) >= 4) { cout << target << endl; } else { ll prod = 1; rep(i,N) { if(A[i] == 2) { prod *= 4; if(prod > target) { cout << target << endl; return 0; } } if(A[i] == 3) { prod *= 729; if(prod > target){ cout << target << endl; return 0; } } } cout << (target % prod == 0 ? -1 : target % prod) << endl; } }