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: Wrong Answer)

#![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 (l, r) = (v[0], v[1]);
    let r_string = format!("{:b}", r).chars().collect::<Vec<_>>();
    let l_string = format!("{:b}", l).chars().collect::<Vec<_>>();
    let mut l_string2 = vec![];
    for _ in 0..r_string.len() - l_string.len() {
        l_string2.push('0');
    }
    for ch in l_string {
        l_string2.push(ch);
    }
    assert!(r_string.len() == l_string2.len());
    let big_int = 1000_000_000 + 7;

    let mut dp = vec![vec![vec![vec![0i64; 2]; 2]; 2]; r_string.len() + 1];

    dp[0][0][0][0] = 1; // pos, less than r, larger than l, same

    for (i, &c) in r_string.iter().enumerate() {
        for j in 0..2 {
            let lim_r = if j == 1 { 1 } else { c.to_digit(10).unwrap() };
            for k in 0..2 {
                let lim_l = if k == 1 {
                    0
                } else {
                    l_string2[i].to_digit(10).unwrap()
                };
                for l in lim_l..lim_r + 1 {
                    let idx1 = if j == 1 || l < lim_r { 1 } else { 0 };
                    let idx2 = if k == 1 || l > lim_l { 1 } else { 0 };

                    dp[i + 1][idx1][idx2][0] += dp[i][j][k][0];
                    dp[i + 1][idx1][idx2][0] %= big_int;
                    if l == 1 && idx2 == 1 {
                        dp[i + 1][idx1][k][1] += dp[i][j][k][0];
                    }
                    dp[i + 1][idx1][idx2][1] += dp[i][j][k][1];
                    dp[i + 1][idx1][idx2][1] %= big_int;
                }
            }
        }
        debug!(i);
        debug!(dp[i + 1][0][0][1]);
        debug!(dp[i + 1][0][0][0]);
        debug!(dp[i + 1][1][0][1]);
        debug!(dp[i + 1][1][0][0]);
        debug!(dp[i + 1][0][1][1]);
        debug!(dp[i + 1][0][1][0]);
        debug!(dp[i + 1][1][1][1]);
        debug!(dp[i + 1][1][1][0]);
    }
    debug!(dp);

    let mut ans = 0;
    for j in 0..2 {
        for k in 0..2 {
            ans += dp[r_string.len()][j][k][0] + dp[r_string.len()][j][k][1];
            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()
}