Problem for Nazar
Contest: Codeforces Round 553 (Div. 2)
Difficulty rating: 1800
Tags: constructive algorithms, math

Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for his outstanding mathematical abilities. Today a math teacher gave him a very difficult task.

Consider two infinite sets of numbers. The first set consists of odd positive numbers ($$$1, 3, 5, 7, \ldots$$$), and the second set consists of even positive numbers ($$$2, 4, 6, 8, \ldots$$$). At the first stage, the teacher writes the first number on the endless blackboard from the first set, in the second stage — the first two numbers from the second set, on the third stage — the next four numbers from the first set, on the fourth — the next eight numbers from the second set and so on. In other words, at each stage, starting from the second, he writes out two times more numbers than at the previous one, and also changes the set from which these numbers are written out to another.

The ten first written numbers: $$$1, 2, 4, 3, 5, 7, 9, 6, 8, 10$$$. Let's number the numbers written, starting with one.

The task is to find the sum of numbers with numbers from $$$l$$$ to $$$r$$$ for given integers $$$l$$$ and $$$r$$$. The answer may be big, so you need to find the remainder of the division by $$$1000000007$$$ ($$$10^9+7$$$).

Nazar thought about this problem for a long time, but didn't come up with a solution. Help him solve this problem.

Input format:
The first line contains two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq 10^{18}$$$) — the range in which you need to find the sum.

Output format:
Print a single integer — the answer modulo $$$1000000007$$$ ($$$10^9+7$$$).

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example, the answer is the sum of the first three numbers written out ($$$1 + 2 + 4 = 7$$$).

In the second example, the numbers with numbers from $$$5$$$ to $$$14$$$: $$$5, 7, 9, 6, 8, 10, 12, 14, 16, 18$$$. Their sum is $$$105$$$.

Example input:
1 3

Example output:
7

Example input:
5 14

Example output:
105

Editorial:
At first let's simplify the problem. Let's denote as $$$f(x)$$$ function that returns sum of the elements that are on positions from $$$1$$$ to $$$x$$$ inclusive.
How to implement function $$$f(x)$$$?
To find the answer we can find the sum of even and sum of odd numbers and add them. Let's find how many there are even and odd numbers. Let's call them $$$cur_{even}$$$ and $$$cur_{odd}$$$. Iterate through the powers of two. If current power is even, then add minimum from $$$x$$$ and current power of two to $$$cur_{odd}$$$, otherwise add minimum from $$$x$$$ and current power of two to $$$cur_{even}$$$, after that reduce $$$x$$$ by current power of two. If $$$x$$$ becomes less than or equal to $$$0$$$ break.
Now our task is reduced to twos: for given number $$$n$$$ find the sum of first $$$n$$$ even numbers and for given number $$$m$$$ find the sum of first $$$m$$$ odd numbers. The answer for first task is $$$n \cdot (n + 1)$$$. The answer for second task is $$$m^2$$$.
Now the answer for the problem is $$$f(r)-f(l-1)$$$.
Don't forget about modulo :)
Complexity is $$$\mathcal{O}(\log{N})$$$.