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)

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<i64>();
    let big_int = 1000_000_000 + 7;
    let (l, r) = (v[0], v[1]);

    let digit_num = format!("{:b}", r).chars().collect::<Vec<_>>().len() + 1;

    let mut dp = vec![vec![vec![vec![0; 2]; 2]; 2]; digit_num + 1];
    dp[0][0][0][0] = 1;
    for i in 0..digit_num {
        let r_bit = (r >> (digit_num - 1 - i)) & 1;
        let l_bit = (l >> (digit_num - 1 - i)) & 1;
        for r_assured in 0..2 {
            for l_assured in 0..2 {
                for max_samed in 0..2 {
                    for x in 0..2 {
                        for y in 0..2 {
                            if x == 1 && y == 0 {
                                continue;
                            }
                            if max_samed == 0 && x != y {
                                continue;
                            }
                            let mut nm = max_samed;
                            if x == 1 && y == 1 {
                                nm = 1;
                            }
                            if x == 0 && l_assured == 0 && l_bit == 1 {
                                continue;
                            }
                            let mut nl = l_assured;
                            if l_bit == 0 && x == 1 {
                                nl = 1;
                            }
                            if r_assured == 0 && y == 1 && r_bit == 0 {
                                continue;
                            }
                            let mut nr = r_assured;
                            if r_bit == 1 && y == 0 {
                                nr = 1;
                            }

                            dp[i + 1][nl][nr][nm] += dp[i][l_assured][r_assured][max_samed];
                            dp[i + 1][nl][nr][nm] %= big_int;
                        }
                    }
                }
            }
        }
    }
    debug!(dp);
    let mut ans = 0i64;
    for l in 0..2 {
        for r in 0..2 {
            for m in 0..2 {
                ans += dp[digit_num][l][r][m];
                ans %= big_int;
            }
        }
    }
    println!("{}", ans);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}