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::prelude::*;

fn input<T: std::str::FromStr>() -> T {
    let stdin = std::io::stdin();
    let token: String = stdin
        .lock()
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}

#[allow(dead_code)]
fn with_stdout_lock<F: FnOnce(std::io::BufWriter<std::io::StdoutLock>)>(f: F) {
    let out = std::io::stdout();
    let w = std::io::BufWriter::new(out.lock());
    f(w);
}

#[allow(unused_macros)]
macro_rules! debug {
    ($var:expr) => {
        match &$var { v => eprintln!(concat!(stringify!($var), "={:?}"), v) }
    };
    ($var:expr, $($vars:expr),*) => {
        match &$var { v => eprint!(concat!(stringify!($var), "={:?} "), v) }
        debug!($($vars),*);
    };
}

fn main() {
    let l: i64 = input();
    let r: i64 = input();
    const MOD: i64 = 1000000007;

    let mut dp = vec![vec![vec![vec![0; 2]; 2]; 2]; 62];
    dp[61][0][0][0] = 1;
    for i in (0..61).rev() {
        let lb = l >> i & 1;
        let rb = r >> i & 1;
        for j in 0..2 {
            for k in 0..2 {
                for l in 0..2 {
                    for x in 0..2 {
                        for y in 0..2 {
                            if x == 1 && y == 0 {
                                continue;
                            }
                            if j == 0 && lb == 1 && x == 0 {
                                continue;
                            }
                            let nextj = if lb == 0 && x == 1 { 1 } else { j };
                            if k == 0 && rb == 0 && y == 1 {
                                continue;
                            }
                            let nextk = if rb == 1 && y == 0 { 1 } else { k };
                            if l == 0 && x != y {
                                continue;
                            }
                            let nextl = if x == 1 && y == 1 { 1 } else { l };
                            dp[i][nextj][nextk][nextl] += dp[i + 1][j][k][l];
                            dp[i][nextj][nextk][nextl] %= MOD;
                        }
                    }
                }
            }
        }
    }

    let mut ans = 0i64;
    for j in 0..2 {
        for k in 0..2 {
            for l in 0..2 {
                ans += dp[0][j][k][l];
                ans %= MOD;
            }
        }
    }

    println!("{}", ans);
}