let rec upper_bound l r p =
  if r <= 1 + l
  then l
  else let m = (l + r) / 2 in
       if p m 
       then upper_bound m r p
       else upper_bound l m p

let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  let bits = Array.init (n + 1) (fun _ -> Array.make 31 0) in
  Array.iteri (fun i a ->
    for j = 0 to 30 do
      if a land (1 lsl j) > 0 then
        bits.(i + 1).(j) <- 1
    done) as_;
  for j = 0 to 30 do
    for i = 1 to n do
      bits.(i).(j) <- bits.(i).(j) + bits.(i - 1).(j)
    done
  done;
  (Array.init n (fun i ->
    upper_bound i n (fun j ->
      Array.init 31 (fun k -> bits.(j + 1).(k) - bits.(i).(k) <= 1)
      |> Array.fold_left ( && ) true) - i + 1))
  |> Array.fold_left ( + ) 0
  |> Printf.printf "%d\n"

let n = 4;;
let as_ = [| 2; 4; 5; 6 |];;

