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::collections::VecDeque;

const MOD: u64 = 1e9 as u64 + 7;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());

    let l: u64 = sc.read();
    let r: u64 = sc.read();

    let mut dp = vec![0; 1 << 3];
    dp[0] = 1;
    for i in (0..63).rev() {
        let r = (r >> i) & 1;
        let l = (l >> i) & 1;
        let mut next = vec![0; 1 << 3];
        for mask in 0..(1 << 3) {
            let started = mask & 1 == 1;
            let lower_than_r = (mask >> 1) & 1 == 1;
            let greater_than_l = (mask >> 2) & 1 == 1;

            for next_x in 0..2 {
                for next_y in 0..2 {
                    if next_x > next_y {
                        continue;
                    }
                    if !started && next_y == 1 && next_x == 0 {
                        continue;
                    }
                    if !lower_than_r && next_y > r {
                        continue;
                    }
                    if !greater_than_l && next_x < l {
                        continue;
                    }

                    let next_started = if started || next_y == 1 { 1 } else { 0 };
                    let next_lower_than_r = if lower_than_r || r > next_y { 1 } else { 0 };
                    let next_greater_than_l = if greater_than_l || next_x > l { 1 } else { 0 };
                    let next_mask =
                        next_started + (next_lower_than_r << 1) + (next_greater_than_l << 2);
                    next[next_mask] += dp[mask];
                    next[next_mask] %= MOD;
                }
            }
        }
        dp = next;
    }

    let mut ans = 0;
    for mask in 0..(1 << 3) {
        if mask & 1 == 1 {
            ans += dp[mask];
        }
    }

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

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .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()
    }
}