Disclaimer: there are lots of untranslateable puns in the Russian version of the statement, so there is one more reason for you to learn Russian :)

Rick and Morty like to go to the ridge High Cry for crying loudly — there is an extraordinary echo. Recently they discovered an interesting acoustic characteristic of this ridge: if Rick and Morty begin crying simultaneously from different mountains, their cry would be heard between these mountains up to the height equal the bitwise OR of mountains they've climbed and all the mountains between them. 

Bitwise OR is a binary operation which is determined the following way. Consider representation of numbers x and y in binary numeric system (probably with leading zeroes) x = x_{k}... x_1x_0 and y = y_{k}... y_1y_0. Then z = x | y is defined following way: z = z_{k}... z_1z_0, where z_{i} = 1, if x_{i} = 1 or y_{i} = 1, and z_{i} = 0 otherwise. In the other words, digit of bitwise OR of two numbers equals zero if and only if digits at corresponding positions is both numbers equals zero. For example bitwise OR of numbers 10 = 1010_2 and 9 = 1001_2 equals 11 = 1011_2. In programming languages C/C++/Java/Python this operation is defined as «|», and in Pascal as «or».

Help Rick and Morty calculate the number of ways they can select two mountains in such a way that if they start crying from these mountains their cry will be heard above these mountains and all mountains between them. More formally you should find number of pairs l and r (1 ≤ l < r ≤ n) such that bitwise OR of heights of all mountains between l and r (inclusive) is larger than the height of any mountain at this interval.


-----Input-----

The first line contains integer n (1 ≤ n ≤ 200 000), the number of mountains in the ridge.

Second line contains n integers a_{i} (0 ≤ a_{i} ≤ 10^9), the heights of mountains in order they are located in the ridge.


-----Output-----

Print the only integer, the number of ways to choose two different mountains.


-----Examples-----
Input
5
3 2 1 6 5

Output
8

Input
4
3 3 3 3

Output
0



-----Note-----

In the first test case all the ways are pairs of mountains with the numbers (numbering from one):(1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)

In the second test case there are no such pairs because for any pair of mountains the height of cry from them is 3, and this height is equal to the height of any mountain.

Difficulty: HARD
Tags: Sorting

Combinatorics

Data structures
Skill types: Sorting

Data structures

Starter code:

Solutions:
n = int(input())
a = [int(i) for i in input().split()]
l = [i for i in range(len(a))]
r = [i for i in range(len(a))]
for i in range(len(a)):
	while l[i] >= 1 and a[i] | a[l[i] - 1] <= a[i]:
		l[i] = l[l[i] - 1]
for j in range(len(a)):
	i = len(a) - j - 1
	while r[i] < len(a) - 1 and a[i] | a[r[i] + 1] <= a[i] and (a[i] > a[r[i] + 1]):
		r[i] = r[r[i] + 1]
count = 0
for i in range(len(a)):
	x = r[i] - i + 1
	y = i - l[i] + 1
	count += x * y - 1
print(n * (n - 1) // 2 - count)


import os, io, sys
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = list(map(int, input().split()))
left = []
right = []
for i in range(31):
	left.append([-1] * n)
	right.append([n] * n)
for i in range(n):
	for j in range(31):
		if 1 << j & a[i]:
			left[j][i] = i
		elif i > 0:
			left[j][i] = left[j][i - 1]
for i in range(n - 1, -1, -1):
	for j in range(31):
		if 1 << j & a[i]:
			right[j][i] = i
		elif i < n - 1:
			right[j][i] = right[j][i + 1]
ans = n * (n - 1) // 2
ans += n
lastsame = [-1] * n
dic = {}
for i in range(n):
	if a[i] in dic:
		lastsame[i] = dic[a[i]]
		dic[a[i]] = i
	else:
		dic[a[i]] = i
for i in range(n):
	maxleft = -1
	minright = n
	for j in range(31):
		if not 1 << j & a[i]:
			maxleft = max(maxleft, left[j][i])
			minright = min(minright, right[j][i])
	if lastsame[i] > maxleft:
		maxleft = lastsame[i]
	ans -= (i - maxleft) * (minright - i)
print(ans)