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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
	
		// xを固定すると
		//        x = 1, x = 2, x = 3, x = 4
		// y % x      0,     0,     0,     0
		// y % x             1,     1,     1
		// y % x                    2,     2
		// y % x                           3
		// 
		// なので
		// y % x == y ^ x == i なら yの候補は y == i ^ x に一意に定まり
		//        x = 1, x = 2, x = 3, x = 4
		// y        0^1,   0^2,   0^3,   0^4
		// y               1^2,   1^3,   1^4
		// y                      2^3,   2^4
		// y                             3^4
		// 
		// のなかで y % x == y ^ x であって L<=x<=y<=R となっているものを数えればよい
		//  y % x == y ^ x　は ∃nであって y - n * x == y ^ x が成り立てばよいが
		//    これは xの最上位ビットに着目すると n > 1 では常に成り立たない 
		//    n == 1 の時は y - x == y ^ x でありこれは y ⊇ x に置き換わる．
		// 
		// (i, j) in range(0,inf) * range(0,inf)を持ってきて
		//  x = i, y = j ^ i　と書けば，(i, j)の満たすべき条件は
		//     L <= i     (L <= x)
		//     i <= j ^ i (x <= y)
		//     j ^ i <= R (y <= R)
		//     j < i
		//     j ^ i ⊇ i (y ⊇ x)
		//  の5つであり，これらは全てdigit eventで判定できるので，桁dpをする．
		
		
		// dp[state]:
		//   stateは (L < x) << 3 | (x < y) << 2 | (y < R) << 1 | (j < i) << 0
		//   各ビット0は等号で保留している
		
		int M = 60;
		var dp = new mlong[1 << 4];  
		dp[0] = 1;
		
		for(int d = M; d >= 0; d--){
			var ndp = new mlong[1 << 4];
			for(int i=0;i<2;i++){
				for(int j=0;j<2;j++){
					for(int s=0;s<1<<4;s++){
						int x = i;
						int y = i ^ j;
						int l = (int)((L >> d) & 1);
						int r = (int)((R >> d) & 1);
						
						if(y < x) continue;
						
						int[] sbit = new int[4];
						for(int k=0;k<4;k++) sbit[k] = (s >> k) & 1;
						int ns = s;
						
						if(sbit[3] == 0 && l > x) continue;
						if(sbit[2] == 0 && x > y) continue;
						if(sbit[1] == 0 && y > r) continue;
						if(sbit[0] == 0 && j > i) continue;
						
						if(sbit[3] == 0 && l < x) ns |= 1 << 3;
						if(sbit[2] == 0 && x < y) ns |= 1 << 2;
						if(sbit[1] == 0 && y < r) ns |= 1 << 1;
						if(sbit[0] == 0 && j < i) ns |= 1 << 0;
						
						ndp[ns] += dp[s];
					}
				}
			}
			dp = ndp;
		}
		
		// OK部分だけを足す．L <= x, x <= y, y <= R, j < i
		mlong ans = 0;
		for(int s=0;s<1<<4;s++){
			if(((s >> 0) & 1) == 0) continue;
			ans += dp[s];
		}
		Console.WriteLine(ans);
		
		
	}
	
	long L,R;
	public Sol(){
		var d = rla();
		R = d[1]; L = d[0];
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}


struct mlong {
	
	const long mod = (long) 1e9 + 7;
	long V;
	
	public mlong(long _v = 0){
		V = _v;
	}
	
	public static mlong operator+(mlong a, mlong b){
		var v0 = a.V + b.V; if(v0 >= mod) v0 -= mod;
		return new mlong(v0);
	}
	public static mlong operator-(mlong a, mlong b){
		var v0 = mod + a.V - b.V; if(v0 >= mod) v0 -= mod;
		return new mlong(v0);
	}
	public static mlong operator-(mlong b){
		var v0 = mod - b.V; if(v0 >= mod) v0 -= mod;
		return new mlong(v0);
	}
	public static mlong operator*(mlong a, mlong b){
		var v0 = a.V * b.V; if(v0 >= mod) v0 %= mod;
		return new mlong(v0);
	}
	public static mlong operator/(mlong a, mlong b){
		var v0 = a.V * inv(b.V).V; if(v0 >= mod) v0 %= mod;
		return new mlong(v0);
	}
	
	
	public static mlong inv(long x){
		long a = 0, b = 0, c = 0;
		ExtGCD(x, mod, ref a, ref b, ref c);
		return (mlong)((a + mod) % mod);
	}
	
	public static void ExtGCD(long x, long y, ref long a, ref long b, ref long c){
		long r0 = x; long r1 = y;
		long a0 = 1; long a1 = 0;
		long b0 = 0; long b1 = 1;
		long q1, r2, a2, b2;
		while(r1 > 0){
			q1 = r0 / r1;
			r2 = r0 % r1;
			a2 = a0 - q1 * a1;
			b2 = b0 - q1 * b1;
			r0 = r1; r1 = r2;
			a0 = a1; a1 = a2;
			b0 = b1; b1 = b2;
		}
		c = r0;
		a = a0;
		b = b0;
	}
	
	public static mlong ModPow(mlong a, long k){
		if(k == 0) return (mlong) 1;
		if(a == 0) return (mlong) 0;
		mlong x = a;
		mlong ret = 1;
		while(k > 0){
			if(k % 2 == 1) ret *= x;
			x *= x;
			k >>= 1;
		}
		return ret;
	}
	
	
	public static bool operator == (mlong a, mlong b){
		 return a.Equals(b);
	}
	public static bool operator != (mlong a, mlong b){
		 return !(a == b);
	}
	
	public override bool Equals(System.Object obj){
		if(obj  ==  null) return false;
		mlong p  =  (mlong) obj;
		if((System.Object) p  ==  null) return false;
		return p.V  ==  V;
	}
	public override int GetHashCode(){
		return V.GetHashCode();
	}
	
	public static implicit operator mlong(long n){
		long v = n % mod; if(v < 0) v += mod;
		return new mlong(v);
	}
	public static implicit operator mlong(int n){
		long v = n % mod; if(v < 0) v += mod;
		return new mlong(v);
	}
	public static explicit operator long(mlong n){
		return n.V;
	}
	
	public override String ToString(){
		return V.ToString();
	}
	
	
}