import std.algorithm;
import std.array;
import std.conv;
import std.container;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

immutable int bits = 20;

long solve (int n)
{
	readln;
	auto a = readln.splitter.map !(to !(int)).array;
	RedBlackTree !(int) [bits] t;
	foreach (i; 0..bits)
	{
		t[i] = redBlackTree !(int) ();
		t[i].insert (-1);
		t[i].insert (n);
		foreach (int j, ref c; a)
		{
			if (c & (1 << i))
			{
				t[i].insert (j);
			}
		}
	}

	auto b = new int [n];
	foreach (int j, ref c; a)
	{
		int lim = n;
		foreach (i; 0..bits)
		{
			if (c & (1 << i))
			{
				lim = min (lim, t[i].upperBound (j).front);
			}
		}
		b[j] = lim;
	}

	foreach_reverse (j; 1..n)
	{
		b[j - 1] = min (b[j - 1], b[j]);
	}

	long res = 0;
	foreach (j; 0..n)
	{
		res += b[j] - j;
	}
	return res;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		writeln (solve (n));
	}
}
