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 (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());

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

    let mut dp = [0; 1 << 3];
    dp[0] = 1;
    for i in (0..61).rev() {
        let bit = 1 << i;
        let mut next = [0; 1 << 3];
        next[0] = 1;
        for mask in 0..(1 << 3) {
            if dp[mask] == 0 {
                continue;
            }
            let is_left_over = mask & 1 != 0;
            let is_right_below = (mask >> 1) & 1 != 0;
            let is_started = (mask >> 2) & 1 != 0;

            if is_started {
                for left in 0..2 {
                    for right in 0..2 {
                        if left > right {
                            continue;
                        }
                        if !is_left_over && (l >> i) & 1 > left {
                            continue;
                        }
                        if !is_right_below && (r >> i) & 1 < right {
                            continue;
                        }

                        let left_over = if is_left_over || (l >> i) & 1 < left {
                            1
                        } else {
                            0
                        };
                        let right_below = if is_right_below || (r >> i) & 1 > right {
                            2
                        } else {
                            0
                        };
                        let started = 4;
                        let next_mask = left_over + right_below + started;
                        next[next_mask] += dp[mask];
                        next[next_mask] %= MOD;
                    }
                }
            } else {
                assert!(!is_left_over);
                assert!(!is_right_below);
                if bit + bit - 1 < l || bit > r {
                    continue;
                }
                let next_left_over = if bit > l { 1 } else { 0 };
                let next_right_below = if bit < ((r >> i) << i) { 2 } else { 0 };
                let next_started = 4;
                let next_mask = next_left_over + next_right_below + next_started;
                next[next_mask] = 1;
            }
        }
        dp = next;
    }
    println!("{}", (4..8).map(|i| dp[i]).sum::<usize>() % 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) -> IO<R, W> {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: std::ops::Deref<Target = str>>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.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()
    }
}