#include <bits/stdc++.h>
using namespace std;
typedef long long int ll; 
    

    // for debug
    /*
    void show_vector( vector<ll> A ){
            for(int i=0; i<A.size(); i++){
                cout<< A.at(i) << endl; 
            }
    }
    */
    
    bool compare( vector<ll> &A, vector<ll> &B ){
        bool bit=true; 
        for(int i=0; i < A.size(); i++){
            if(  B.at(i) > A.at(i)+1 ) bit = false; 
        }
        return bit; 
    }
    
            int main(){
                ll N;
                cin >> N;
                vector<ll> A(N); 
                vector<vector<ll>> c(N, vector<ll>(30,0));
                
                for(int i=0; i<N; i++){
                    cin >> A.at(i);
                    
                    bitset<30> bin(A.at(i));
                    for(int j=0; j<30; j++){
                        if(i==0){
                            if( bin.test(j) ) c.at(0).at(j) = 1;
                        }else{
                            c.at(i).at(j) = c.at(i-1).at(j);
                            if( bin.test(j) ) c.at(i).at(j) ++; 
                        }
                    }
                }
                
                ll count =0; 
                vector<ll> zero(30);
                for(int i=0; i<30; i++){
                    zero.at(i)=0; 
                }
                
                
                for(int l=0; l<N; l++){
                    
                    int left=l;
                    //int right = l+1;
                    int right = N-1; 
                    
                    
                    while( right - left > 1){
                        
                        int middle = (left + right)/2; 
                        
                        
                        
                        if(l==0){
                                if( compare( zero, c.at(middle)) ){
                                    left = middle;
                                }else{
                                    right = middle; 
                                }                            
                        }else{
                                if( compare(c.at(l-1), c.at(middle)) ){
                                    left = middle;
                                }else{
                                    right = middle; 
                                }                            
                        }
                    
                    }
                    
                    
                    if( l>0 && compare( c.at(l-1), c.at(N-1) ) ) left=N-1; 
                    if( l==0 && compare( zero, c.at(N-1) ) ) left=N-1; 
                    
                    //cout<< l << " " <<left <<" "<< right << endl; 
                    
                    count = count + left - l + 1;
                    
                }
                
                
             cout<< count << endl;   
            
                
                // For check
                /*
                for(int i=0; i<N; i++){
                    for(int j=20; j>=0; j--){
                        cout << c.at(i).at(j); 
                    }
                    cout<< endl; 
                }
                */
                
                
                //cout<< count << endl; 
                
                
            }

Measured memory_bytes: 56576