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)

const MOD: usize = 1e9 as usize + 7;
fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let l: u64 = sc.read();
    let r: u64 = sc.read();
    let mut dp = vec![vec![vec![0; 2]; 2]; 2];
    dp[0][0][0] = 1;
    for i in (0..63).rev() {
        let l = (l >> i) & 1;
        let r = (r >> i) & 1;
        let mut next = vec![vec![vec![0; 2]; 2]; 2];
        for lower_r in 0..2 {
            for upper_l in 0..2 {
                for same_highest in 0..2 {
                    for (x, y) in vec![(0, 1), (0, 0), (1, 1)].into_iter() {
                        if lower_r == 0 && r < y {
                            continue;
                        }
                        if upper_l == 0 && l > x {
                            continue;
                        }
                        if same_highest == 0 && x != y {
                            continue;
                        }
                        let next_lower_r = if lower_r == 1 || r > y { 1 } else { 0 };
                        let next_upper_l = if upper_l == 1 || l < x { 1 } else { 0 };
                        let next_same_highest = if same_highest == 1 || x == 1 && y == 1 {
                            1
                        } else {
                            0
                        };
                        next[next_lower_r][next_upper_l][next_same_highest] +=
                            dp[lower_r][upper_l][same_highest];
                        next[next_lower_r][next_upper_l][next_same_highest] %= MOD;
                    }
                }
            }
        }
        dp = next;
    }

    let mut ans = 0;
    for i in 0..2 {
        for j in 0..2 {
            for k in 0..2 {
                ans += dp[i][j][k];
            }
        }
    }
    println!("{}", ans % MOD);
}

pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}