#include using namespace std; __attribute__((constructor)) void fast_io() { ios::sync_with_stdio(false); cin.tie(nullptr); } long long binary_gcd(long long a, long long b) { if (a == 0) return b; if (b == 0) return a; int n = __builtin_ctzll(a); int m = __builtin_ctzll(b); a >>= n; b >>= m; while (a != b) { if (a < b) swap(a, b); int k = __builtin_ctzll(a - b); a = (a - b) >> k; } return a << min(n, m); } template struct swag { struct node { S value, sum; node(const S& value, const S& sum) : value(value), sum(sum) {} }; stack front_stack, back_stack; swag() {} bool empty() const { return front_stack.empty() && back_stack.empty();} size_t size() const { return front_stack.size() + back_stack.size();} S fold() const { assert(!empty()); if (front_stack.empty()) return back_stack.top().sum; else if (back_stack.empty()) return front_stack.top().sum; else return op(front_stack.top().sum, back_stack.top().sum); } void push(const S &x) { if (back_stack.empty()) back_stack.emplace(x, x); else { S s{op(back_stack.top().sum, x)}; back_stack.emplace(x, s); } } void pop() { assert(!empty()); if (front_stack.empty()) { front_stack.emplace(back_stack.top().value, back_stack.top().value); back_stack.pop(); while (!back_stack.empty()) { S s{op(back_stack.top().value, front_stack.top().sum)}; front_stack.emplace(back_stack.top().value, s); back_stack.pop(); } } front_stack.pop(); } }; int main() { int n; cin >> n; vector a(n); for (auto& ai : a) cin >> ai; swag q; long long ans = 0; for (int l = 0, r = 0; l < n; ++l) { while (r < n && (q.empty() || q.fold() != 1)) q.push(a[r++]); if (q.fold() == 1) ans += n - r + 1; q.pop(); } cout << ans << '\n'; }