#include using namespace std; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template istream &operator>>(istream &is, vector &vec) { for (auto &v : vec) is >> v; return is; } const int MOD = 1000000007; bool is_prod_big(vector A) { long long tmp = 1; for (auto a : A) { tmp *= a; if (tmp >= 500000) return true; } return false; } int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int N; cin >> N; vector A(N); cin >> A; if (is_prod_big(A)) { int i = 0, j = N - 1; while (A[i] == 1) i++; while (A[j] == 1) j--; long long ret = 1; FOR(k, i, j + 1) ret = ret * A[k] % MOD; ret += i + (N - 1 - j); cout << ret % MOD << '\n'; return 0; } vector AA(N + 1, 1); REP(i, N) AA[i + 1] = AA[i] * A[i]; vector nonzero; REP(i, N) if (A[i] > 1) nonzero.push_back(i); vector dp(N + 1); REP(i, N) { chmax(dp[i + 1], dp[i] + A[i]); for (auto j : nonzero) { if (j < i) { chmax(dp[i + 1], dp[j] + AA[i + 1] / AA[j]); } } } cout << dp.back() << '\n'; }