Problem:
Score : 600 points

Problem Statement

Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x.

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

1 \leq L \leq R \leq 10^{18}

Input

Input is given from Standard Input in the following format:

L R

Output

Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7.

Sample Input 1

2 3

Sample Output 1

3

Three pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).

Sample Input 2

10 100

Sample Output 2

604

Sample Input 3

1 1000000000000000000

Sample Output 3

68038601

Be sure to compute the number modulo 10^9 + 7.

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

const MOD: usize = 1e+9 as usize + 7;

fn calc(
    depth: usize,
    upper_l: usize,
    lower_r: usize,
    rev: usize,
    l: usize,
    r: usize,
    memo: &mut Vec<Vec<Vec<Vec<usize>>>>,
) -> usize {
    if memo[upper_l][lower_r][rev][depth] != MOD {
        return memo[upper_l][lower_r][rev][depth];
    }
    let bit = 1 << depth;
    if bit > r || (bit * 2 > r && bit <= r && lower_r == 0) {
        return 0;
    }
    let mut ret = 0;
    if rev == 0 && (bit > l || (bit * 2 > l && bit <= l && upper_l == 1)) {
        ret = 1;
    }
    for i in 0..2 {
        let nlower_r = if (r & bit) > 0 {
            i == 0 || lower_r == 1
        } else {
            i == 0 && lower_r == 1
        };
        let nlower_r = if nlower_r { 1 } else { 0 };
        for j in 0..2 {
            if i == 0 && j == 1 {
                continue;
            }
            let nupper_l = if (l & bit) > 0 {
                j == 1 && upper_l == 1
            } else {
                j == 1 || upper_l == 1
            };
            let nupper_l = if nupper_l { 1 } else { 0 };
            let nrev = if i < j || (rev == 1 && i == j) { 1 } else { 0 };
            ret = (ret + calc(depth + 1, nupper_l, nlower_r, nrev, l, r, memo)) % MOD;
        }
    }
    memo[upper_l][lower_r][rev][depth] = ret;
    // println!("{} {} {} {} {}", depth, upper_l, lower_r, rev, ret);
    return ret;
}

fn main() {
    let l = read::<usize>();
    let r = read::<usize>();
    let mut memo = vec![vec![vec![vec![MOD; 70]; 2]; 2]; 2];
    let ans = calc(0, 1, 1, 0, l, r, &mut memo);
    println!("{}", ans);
}