#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) #define all(x) (x).begin(),(x).end() #define inf 1e18 #define mod 1000000007 using namespace std; typedef long long llint; typedef long long ll; typedef pair P; ll n; ll a[200005]; ll dp[105]; deque deq; ll M = 1e18; int main(void) { cin >> n; rep(i, 1, n){ cin >> a[i]; deq.push_back(a[i]); } ll add = 0; while(deq.size() && deq.front() == 1) deq.pop_front(), add++; while(deq.size() && deq.back() == 1) deq.pop_back(), add++; ll mul = 1; bool flag = false; for(auto x : deq){ if(x >= M / mul){ flag = true; break; } mul *= x; } if(flag){ ll ans = 1; for(auto x : deq) ans *= x, ans %= mod; ans += add, ans %= mod; cout << ans << endl; return 0; } for(auto x : deq) cout<< x << " "; cout << endl; vector vec, vec2; mul = 1; ll cnt = 0; for(auto x : deq){ if(x == 1){ if(cnt == 0){ vec.push_back(mul); cnt = 1; } else cnt++; } else{ if(cnt > 0){ vec2.push_back(cnt); mul = x; cnt = 0; } else mul *= x; } } vec.push_back(mul); vec2.push_back(cnt); //for(auto x : vec) cout<< x << " "; cout << endl; //for(auto x : vec2) cout<< x << " "; cout << endl; ll m = vec.size(); rep(i, 0, m) dp[i] = -inf; dp[0] = 0; rep(i, 0, m-1){ rep(j, i+1, m){ ll mul = 1; rep(k, i+1, j) mul *= vec[k-1]; chmax(dp[j], dp[i] + mul + vec2[j-1]); } } //rep(i, 0, m) cout << dp[i] << " "; cout<< endl; ll ans = (dp[m] + add) % mod; cout << ans << endl; return 0; }