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 (Python):
MOD = 10**9 + 7

L, R = [int(item) for item in input().split()]
L_blen = L.bit_length()
R_blen = R.bit_length()


dp = [[[0] * (R_blen+1) for _ in range(2)] for _ in range(2)]

for i in range(R_blen + 1):
    L_bit = L & (1 << R_blen - i)
    R_bit = R & (1 << R_blen - i)

    
    curbit_idx = R_blen - i + 1
    if curbit_idx <= R_blen and curbit_idx >= L_blen:
        dp[R_blen == curbit_idx][L_blen == curbit_idx][i] += 1

    
    if not R_bit and not L_bit:
        
        dp[0][0][i] += dp[0][0][i-1] 
        dp[0][0][i] += dp[0][1][i-1] 
        
        dp[1][1][i] += dp[1][1][i-1] 
        dp[1][0][i] += dp[1][0][i-1] 
        dp[0][0][i] += dp[0][0][i-1] 
        dp[0][1][i] += dp[0][1][i-1] 
        
        dp[0][1][i] += dp[0][1][i-1] 
        dp[0][0][i] += dp[0][0][i-1] 

    
    if R_bit and not L_bit:
        
        dp[0][0][i] += dp[0][0][i-1] 
        dp[1][0][i] += dp[1][0][i-1] 
        dp[0][0][i] += dp[0][1][i-1] 
        dp[1][0][i] += dp[1][1][i-1] 
        
        dp[0][0][i] += dp[0][0][i-1] 
        dp[0][0][i] += dp[1][0][i-1] 
        dp[0][1][i] += dp[0][1][i-1] 
        dp[0][1][i] += dp[1][1][i-1] 
        
        dp[0][0][i] += dp[0][0][i-1] 
        dp[1][0][i] += dp[1][0][i-1] 
        dp[0][1][i] += dp[0][1][i-1] 
        dp[1][1][i] += dp[1][1][i-1] 

    
    if not R_bit and L_bit:
        
        dp[0][0][i] += dp[0][0][i-1] 
        dp[0][1][i] += dp[0][1][i-1] 
        
        dp[0][0][i] += dp[0][0][i-1] 
        dp[1][0][i] += dp[1][0][i-1] 
        
        dp[0][0][i] += dp[0][0][i-1]

    
    if R_bit and L_bit:
        
        dp[0][0][i] += dp[0][0][i-1] 
        dp[1][0][i] += dp[1][0][i-1] 
        dp[0][1][i] += dp[0][1][i-1] 
        dp[1][1][i] += dp[1][1][i-1] 
        
        dp[0][0][i] += dp[1][0][i-1] 
        dp[0][0][i] += dp[0][0][i-1] 
        
        dp[1][0][i] += dp[1][0][i-1] 
        dp[0][0][i] += dp[0][0][i-1] 

    
    for i in range(2):
        for j in range(2):
            dp[i][j][i] %= MOD

ans = 0
for i in range(2):
    for j in range(2):
        ans += dp[i][j][-1]
        ans %= MOD
print(ans)

Solution (Python):
from functools import *
@lru_cache(maxsize=None)
def solve(L,R):
  if L > R: return 0
  if L == 1: return 1 + solve(2, R)
  return solve(L//2,(R-1)//2) + solve((L+1)//2,R//2) + solve((L+1)//2,(R-1)//2)
print(solve(*map(int,input().split()))%(10**9+7))

Solution (Python):
from functools import *
MOD=10**9+7
@lru_cache(maxsize=None)
def solve(L,R):
  if L > R: return 0
  if L == 1: return (1 + solve(2, R)) % MOD
  return (solve(L//2,(R-1)//2) + solve((L+1)//2,R//2) + solve((L+1)//2,(R-1)//2)) % MOD
  
print(solve(*map(int,input().split())))

Solution (Python):
L, R = map(int, input().split())
MOD = 10 ** 9 + 7
l = '{:060b}'.format(L)[::-1]
r = '{:060b}'.format(R)[::-1]

memo = [[[[-1 for l in range(2)] for k in range(2)] for j in range(2)] for i in range(60)]


def f(pos, flagX, flagY, flagZ):
    if pos == -1:
        return 1
    if memo[pos][flagX][flagY][flagZ] != -1:
        return memo[pos][flagX][flagY][flagZ]
    ret = 0
    
    if flagX or l[pos] == '0':
        ret += f(pos-1, flagX, 1 if r[pos]=='1' else flagY, flagZ)
    
    if (flagX or l[pos] == '0') and (flagY or r[pos] == '1') and flagZ:
        ret += f(pos-1, flagX, flagY, flagZ)
    
    if flagY or r[pos] == '1':
        ret += f(pos-1, 1 if l[pos] == '0' else flagX, flagY, 1)
    ret %= MOD
    memo[pos][flagX][flagY][flagZ] = ret
    return ret

ans = f(59, 0, 0, 0)
print(ans)

Solution (Python):

import bisect
import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

min_val,max_val=getints()

def get_key(pos, is_small, is_large, found):
    return ((pos * 2 + is_small) * 2 + is_large) * 2 + found

mod = 10**9 + 7
cache = [-1] * get_key(64,1,1,1)

def solve(pos, is_small, is_large, found):
    if pos < 0:
        return 1
    key = get_key(pos,is_small,is_large,found)
    res = cache[key]
    if res >= 0:
        return res

    res = 0
    for x in [0,1]:
        for y in [0,1]:
            if x == 1 and y == 0: continue
            if not is_small and x == 0 and (min_val >> pos & 1) == 1:
                continue
            if not is_large and y == 1 and (max_val >> pos & 1) == 0:
                continue
            if not found and x != y:
                continue
            new_is_small = True if x > (min_val >> pos & 1) else is_small
            new_is_large = True if y < (max_val >> pos & 1) else is_large
            new_found    = True if x == y and x == 1 else found
            res += solve(pos-1, new_is_small, new_is_large, new_found)

    res = res % mod
    cache[key] = res
    return res

print(solve(63, False, False, False))

Solution (Python):

L,R = map(int,input().split())


dp = []

for i in range(62):

    nowA = []

    for i in range(2):

        nowB = []

        for i in range(2):

            nowC = []

            for i in range(2):

                nowC.append(0)

            nowB.append(nowC)

        nowA.append(nowB)

    dp.append(nowA)

dp[0][0][0][0] = 1



for i in range(61):

    i += 1
    keta = 62-i

    lb = (L >> (keta-1)) % 2
    rb = (R >> (keta-1)) % 2


    for j in range(2):
        for k in range(2):
            for m in range(2):

                pre = dp[i-1][j][k][m]

                for x in range(2):
                    for y in range(2):

                        nj = j
                        nk = k
                        nm = m

                        if x == 1 and y == 0:
                            continue;

                        if x != y and m == 0:
                            continue;

                        if j == 0 and lb == 1 and x == 0:
                            continue;

                        if k == 0 and rb == 0 and y == 1:
                            continue;

                        if j == 0 and lb == 0 and x == 1:
                            nj = 1

                        if k == 0 and rb == 1 and y == 0:
                            nk = 1

                        if m == 0 and x == 1 and y == 1:
                            nm = 1


                        dp[i][nj][nk][nm] += dp[i-1][j][k][m]
                        dp[i][nj][nk][nm] %= 10 ** 9 +7

ans = 0

for j in range(2):
    for k in range(2):

        ans += dp[-1][j][k][1]

print (ans % (10 ** 9 + 7))

Solution (Python):
L, R = map(int, input().split())
mod = 10 ** 9 +7
l = '{:060b}'.format(L)
r = '{:060b}'.format(R)

memo = [[[[-1]*2 for i in range(2)] for j in range(2)] for k in range(60)]
def f(pos, flagL, flagR, flagM):
    if pos == 60:
        return 1
    if memo[pos][flagL][flagR][flagM] != -1:
        return memo[pos][flagL][flagR][flagM]
    ret = 0
    
    if flagL or l[pos] == '0':
        ret+=f(pos+1, flagL, 1 if r[pos]=='1' else flagR, flagM)
    
    if flagM and (flagL or l[pos]=='0') and (flagR or r[pos]=='1'):
        ret += f(pos+1, flagL, flagR, flagM)
    
    if flagR or r[pos]=='1':
        ret += f(pos+1, 1 if l[pos]=='0' else flagL, flagR, 1)
    memo[pos][flagL][flagR][flagM] = ret%mod
    return ret%mod
print(f(0, 0, 0, 0)%mod)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
from functools import lru_cache
mod=1000000007

@lru_cache()
def calc(l,r):
    if l>r:
        return 0
    if r==0:
        return 1
    b_l,b_r=l.bit_length(),r.bit_length()
    if b_l==b_r:
        return calc(l-(1<<(b_l-1)),r-(1<<(b_r-1)))
    if r==(1<<(b_r-1))-1 and l==0:
        return pow(3,b_r,mod)
    return (calc(l,r-(1<<(b_r-1)))+calc(l,(1<<(b_r-1))-1)+calc(0,r-(1<<b_r-1)))%mod

@lru_cache()
def solve(l,r):
    if l>r:
        return 0
    b_l,b_r=l.bit_length(),r.bit_length()
    if b_r>b_l:
        return (solve(l,(1<<b_r-1)-1)+calc(0,r-(1<<(b_r-1))))%mod
    a=1<<(b_l-1) if l else 0
    return calc(l-a,r-a)

l,r=map(int,input().split())
print(solve(l,r))

Solution (Python):
L, R = map(int, input().split())
MOD = 10 ** 9 + 7
l = '{:060b}'.format(L)[::-1]
r = '{:060b}'.format(R)[::-1]
 
memo = [[[[-1 for l in range(2)] for k in range(2)] for j in range(2)] for i in range(60)]
 
def f(pos, flagX, flagY, flagZ):
    if pos == -1:
        return 1
    if memo[pos][flagX][flagY][flagZ] != -1:
        return memo[pos][flagX][flagY][flagZ]
    ret = 0
    
    if flagX or l[pos] == '0':
        ret += f(pos - 1, flagX, 1 if r[pos] == '1' else flagY, flagZ)
    
    if (flagX or l[pos] == '0') and (flagY or r[pos] == '1') and flagZ:
        ret += f(pos - 1, flagX, flagY, flagZ)
    
    if flagY or r[pos] == '1':
        ret += f(pos - 1, 1 if l[pos] == '0' else flagX, flagY, 1)
    ret %= MOD
    memo[pos][flagX][flagY][flagZ] = ret
    return ret
 
ans = f(59, 0, 0, 0)
print(ans)

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 


l,r = [int(i) for i in readline().split()]

def make_dp(init, size):
    res = "[{}]*{}".format(init,size[-1])
    for i in reversed(size[:-1]):
        res = "[{} for _ in [0]*{}]".format(res,i)
    return eval(res)


MOD = 10**9 + 7

R = bin(r)[2:]
L = bin(l)[2:]
L = "0"*(len(R)-len(L)) + L

dp = make_dp(0,(len(R)+1,2,2,2))
dp[0][0][0][0] = 1
for i, (r,l) in enumerate(zip(R,L)): 
    ri = int(r)
    li = int(l)
    for is_less in range(2):
        for is_more in range(2):
            for is_nonzero in range(2):
                for dl in range(0 if is_more else li,2):
                    for dr in range(dl, 2 if is_less else ri+1): 
                        dp[i+1][is_nonzero or dr != 0][is_more or li < dl][is_less or dr < ri] += dp[i][is_nonzero][is_more][is_less]*(dr==dl or is_nonzero)
                        dp[i+1][is_nonzero or dr != 0][is_more or li < dl][is_less or dr < ri] %= MOD


ans = 0
for i in range(2):
    for j in range(2):
        for k in range(2):
            ans += dp[-1][i][j][k]

print(ans%MOD)

Solution (Python):
MOD = 10**9 + 7
l, r = map(int, input().split())

def func(x, y):
	if y == 0:
		return 1
	dp = [[0 for _ in range(6)] for _ in range(61)]
	dp[60][0] = 1
	for i in range(59, -1, -1):
		if (y>>i) & 1 == 0 and (x>>i) & 1 == 0:
			dp[i][0] = dp[i+1][0]
			dp[i][1] = dp[i+1][1]
			dp[i][2] = dp[i+1][2]
			dp[i][3] = (dp[i+1][3]*2) % MOD
			dp[i][4] = dp[i+1][4]
			dp[i][5] = (dp[i+1][1] + dp[i+1][3] + dp[i+1][5]*3) % MOD
		elif (y>>i) & 1 == 1 and (x>>i) & 1 == 1:
			dp[i][0] = 0
			dp[i][1] = 0
			dp[i][2] = (dp[i+1][0] + dp[i+1][2]) % MOD
			dp[i][3] = (dp[i+1][1] + dp[i+1][3]) % MOD
			dp[i][4] = (dp[i+1][4]*2) % MOD
			dp[i][5] = (dp[i+1][4] + dp[i+1][5]*3) % MOD
		elif (y>>i) & 1 == 1 and (x>>i) & 1 == 0:
			dp[i][0] = 0
			dp[i][1] = (dp[i+1][0] + dp[i+1][1]) % MOD
			dp[i][2] = dp[i+1][2]
			dp[i][3] = (dp[i+1][2] + dp[i+1][3]*2) % MOD
			dp[i][4] = (dp[i+1][0] + dp[i+1][2] + dp[i+1][4]*2) % MOD
			dp[i][5] = (dp[i+1][1] + dp[i+1][3] + dp[i+1][4] + dp[i+1][5]*3) % MOD
		elif (y>>i) & 1 == 0 and (x>>i) & 1 == 1:
			dp[i][0] = 0
			dp[i][1] = 0
			dp[i][2] = 0
			dp[i][3] = (dp[i+1][1] + dp[i+1][3]) % MOD
			dp[i][4] = dp[i+1][4]
			dp[i][5] = (dp[i+1][5]*3) % MOD

	return (sum(dp[0]))%MOD

print(func(l, r))

Solution (Python):
L, R = map(int, input().split())
MOD = 10**9 + 7

*BL, = map(int, bin(L)[2:])
*BR, = map(int, bin(R)[2:])

a = len(BL)
b = len(BR)
BL = [0]*(len(BR) - len(BL)) + BL

BL.reverse()
BR.reverse()

memo = [[[-1]*2 for i in range(2)] for j in range(70)]
def dfs(i, p, q):
    if memo[i][p][q] != -1:
        return memo[i][p][q]
    if i == -1:
        memo[i][p][q] = 1
        return 1
    r = 0
    if p == q == 0:
        if BL[i] == 1 and BR[i] == 0:
            memo[i][0][0] = 0
            return 0
        if BL[i] == 1 or BR[i] == 0:
            r = dfs(i-1, 0, 0)
        else:
            r = (dfs(i-1, 0, 1) + dfs(i-1, 1, 0) + dfs(i-1, 0, 0)) % MOD
    elif p == 0:
        if BL[i] == 1:
            r = dfs(i-1, 0, 1)
        else:
            r = (2*dfs(i-1, 0, 1) + dfs(i-1, 1, 1)) % MOD
    elif q == 0:
        if BR[i] == 0:
            r = dfs(i-1, 1, 0)
        else:
            r = (2*dfs(i-1, 1, 0) + dfs(i-1, 1, 1)) % MOD
    else:
        r = 3 * dfs(i-1, 1, 1)
    memo[i][p][q] = r
    return r
ans = 0
for l in range(a-1, b):
    ans += dfs(l-1, +(not a-1 == l), +(not b-1 == l))
print(ans % MOD)

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  





mod = 1000000007

def ca(x, y):
    ct = 0
    for i in range(x, y+1):
        for j in range(i, y+1):
            if j % i == (j^i):
                ct += 1
    return ct

ans = 0
l, r = na()

dp = [0] * 4
for i in range(60, -1, -1):
    ndp = [0] * 4
    base = 1<<i
    if base <= r and l < base*2:
        ptn = 0
        if base+1 <= l:
            ptn += 1
        if r < base*2-1:
            ptn += 2
        ndp[ptn] += 1

    if l>>i != r>>i:
        if (l>>i&1) == 0 and (r>>i&1) == 1:
            ndp[1] += dp[3]
            ndp[2] += dp[3]
    if ((l>>i)&(r>>i)) == (l>>i):
        ndp[3] += dp[3]

    if (l >> i & 1) == 0:
        ndp[0] += dp[1]
        ndp[1] += dp[1]
    ndp[1] += dp[1]

    if (r >> i & 1) == 1:
        ndp[0] += dp[2]
        ndp[2] += dp[2]
    ndp[2] += dp[2]

    ndp[0] += dp[0] * 3

    for k in range(4):
        ndp[k] %= mod
    dp = ndp
print(sum(dp) % mod)

Solution (Python):
from functools import lru_cache
import sys
sys.setrecursionlimit(1000000)
P = 10**9+7
@lru_cache(maxsize=None)
def subcalc(l, r):
    if l < 0 or r < 0:
        print("ERROR")
        print("l, r =", l, r)
        print(1//0)
    if l > r: return 0
    if r == 0: return 1
    aa, bb = l.bit_length(), r.bit_length()
    if aa == bb:
        return subcalc(l-(1<<aa-1), r-(1<<bb-1))
    if (r & (r+1) == 0) and (l == 0):
        return pow(3, r.bit_length(), P)
    t = (subcalc(l, r-(1<<bb-1)) + subcalc(l, (1<<bb-1)-1) + subcalc(0, r-(1<<bb-1))) % P
    
    return t
 
@lru_cache(maxsize=None)
def calc(L, R):
    if L < 0 or R < 0:
        print("ERROR")
        print("l, r =", l, r)
        print(1//0)
    if L > R: return 0
    a = L.bit_length()
    b = R.bit_length()
    if b > a:
        t = (calc(L, (1<<b-1)-1) + calc(1<<b-1, R)) % P
        
        return t
    
        
        
    a = 1 << L.bit_length() - 1 if L else 0
    t = subcalc(L-a, R-a)
    
    return t
 
L, R = map(int, input().split())
print(calc(L, R))

Solution (Python):
from functools import lru_cache
P = 10**9+7
@lru_cache(maxsize=None)
def subcalc(l, r):
    if l > r: return 0
    if r == 0: return 1
    aa, bb = l.bit_length(), r.bit_length()
    if aa == bb:
        return subcalc(l-(1<<aa-1), r-(1<<bb-1))
    if (r & (r+1) == 0) and (l == 0):
        return pow(3, r.bit_length(), P)
    return (subcalc(l, r-(1<<bb-1)) + subcalc(l, (1<<bb-1)-1) + subcalc(0, r-(1<<bb-1))) % P

@lru_cache(maxsize=None)
def calc(L, R):
    if L > R: return 0
    a = L.bit_length()
    b = R.bit_length()
    if b > a:
        return (calc(L, (1<<b-1)-1) + calc(1<<b-1, R)) % P
    a = 1 << L.bit_length() - 1 if L else 0
    return subcalc(L-a, R-a)

L, R = map(int, input().split())
print(calc(L, R))

Solution (Python):
L, R = map(int, raw_input().split())

"""
R ????????1??????
y 0001????0??????
     ^d   ^rd
x 0001???????1??
             ^ld
L ???????????0???
"""

ans = 0

for d in range(70):
  if d<len(bin(L))-3:
    LD = []
  elif d==len(bin(L))-3:
    LD = [i for i in range(-1,d) if i==-1 or (L>>i&1)==0]
  else:
    LD = [d]
  if d<len(bin(R))-3:
    RD = [d]
  elif d==len(bin(R))-3:
    RD = [i for i in range(-1,d) if i==-1 or (R>>i&1)==1]
  else:
    RD = []
  for ld in LD:
    for rd in RD:
      a = 1
      for i in range(d):
        if i<ld:
          xc = [0, 1]
        elif i==ld:
          xc = [1]
        else:
          xc = [L>>i&1]
        if i<rd:
          yc = [0, 1]
        elif i==rd:
          yc = [0]
        else:
          yc = [R>>i&1]
        c = 0
        for x in xc:
          for y in yc:
            if y>=x:
              c += 1
        a *= c
      ans += a
print ans%(10**9+7)

Solution (Python):
from functools import lru_cache
import sys
sys.setrecursionlimit(1000000)
P = 10**9+7
@lru_cache(maxsize=None)
def subcalc(l, r):
    if l < 0 or r < 0:
        print("ERROR")
        print("l, r =", l, r)
        print(1//0)
    if l > r: return 0
    if r == 0: return 1
    aa, bb = l.bit_length(), r.bit_length()
    if aa == bb:
        return subcalc(l-(1<<aa-1), r-(1<<bb-1))
    if (r & (r+1) == 0) and (l == 0):
        return pow(3, r.bit_length(), P)
    t = (subcalc(l, r-(1<<bb-1)) + subcalc(l, (1<<bb-1)-1) + subcalc(0, r-(1<<bb-1))) % P
    
    return t

@lru_cache(maxsize=None)
def calc(L, R):
    if L < 0 or R < 0:
        print("ERROR")
        print("l, r =", l, r)
        print(1//0)
    if L > R: return 0
    a = L.bit_length()
    b = R.bit_length()
    if b > a:
        t = (calc(L, (1<<b-1)-1) + calc(1<<b-1, R)) % P
        
        return t
    
        
        
    a = 1 << L.bit_length() - 1 if L else 0
    t = subcalc(L-a, R-a)
    
    return t

L, R = map(int, input().split())
print(calc(L, R))

Solution (Python):
L, R = map(int, raw_input().split())

"""
R ????????1??????
y 0001????0??????
     ^d   ^rd
x 0001???????1??
             ^ld
L ???????????0???
"""

ans = 0

for d in range(70):
  if d<len(bin(L))-3:
    LD = []
  elif d==len(bin(L))-3:
    LD = [i for i in range(-1,d) if i==-1 or (L>>i&1)==0]
  else:
    LD = [d]
  if d<len(bin(R))-3:
    RD = [d]
  elif d==len(bin(R))-3:
    RD = [i for i in range(-1,d) if i==-1 or (R>>i&1)==1]
  else:
    RD = []
  for ld in LD:
    for rd in RD:
      a = 1
      for i in range(d):
        
        
        
        
        
        
        
        if i<ld and i<rd:
          a *= 3
        if i<ld and i==rd:
          a *= 1
        if i<ld and i>rd:
          a *= (R>>i&1)+1
        if i==ld and i<rd:
          a *= 1
        if i==ld and i==rd:
          a *= 0
        if i==ld and i>rd:
          a *= (R>>i&1)
        if i>ld and i<rd:
          a *= 2-(L>>i&1)
        if i>ld and i==rd:
          if (L>>i&1)==0:
            a *= 1
          else:
            a *= 0
        if i>ld and i>rd:
          if (L>>i&1)==(R>>i&1):
            a *= 1
          if (L>>i&1)==0 and (R>>i&1)==1:
            a *= 1
          if (L>>i&1)==1 and (R>>i&1)==0:
            a *= 0
      ans += a
print ans%(10**9+7)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from functools import lru_cache

MOD = 10 ** 9 + 7

"""
桁数が異なると矛盾
したがって商が1であることが必要
すると、xがyのsubsetであることが必要
一番上の桁を含むsubsetがx
"""

@lru_cache()
def F_naive(L,R):
    
    ret = 0
    ret += F((L+1)//2,(R+1)//2) 
    answer = 0
    for x in range(L,R+1):
        for y in range(x,R+1):
            if x.bit_length() == y.bit_length() and (x^y)+x == y:
                answer += 1
    return answer

@lru_cache()
def F(L,R):
    
    if L < 0:
        L = 0
    if R < L:
        return 0
    if R == 0:
        return 0
    ret = 0
    
    ret += F((L+1)//2, R//2)
    
    
    ret += F((L+1)//2,(R-1)//2)
    
    

    
    
    ret += G(L//2,(R-1)//2)
    return ret

@lru_cache()
def G(L,R):
    
    if L < 0:
        L = 0
    if R < L:
        return 0
    if R == 0:
        return 1
    ret = 0
    
    ret += G((L+1)//2, R//2)
    
    
    ret += F((L+1)//2,(R-1)//2)
    
    

    
    
    ret += G(L//2,(R-1)//2)
    return ret

L,R = map(int,input().split())
answer = F(L,R) % MOD
print(answer)

Solution (Python):
L, R = map(int, input().split())
MOD = 10 ** 9 + 7
l = '{:060b}'.format(L)[::-1]
r = '{:060b}'.format(R)[::-1]

memo = [[[[-1 for l in range(2)] for k in range(2)] for j in range(2)] for i in range(60)]

def f(pos, flagX, flagY, flagZ):
    if pos == -1:
        return 1
    if memo[pos][flagX][flagY][flagZ] != -1:
        return memo[pos][flagX][flagY][flagZ]
    ret = 0
    
    if flagX or l[pos] == '0':
        ret += f(pos - 1, flagX, 1 if r[pos] == '1' else flagY, flagZ)
    
    if (flagX or l[pos] == '0') and (flagY or r[pos] == '1') and flagZ:
        ret += f(pos - 1, flagX, flagY, flagZ)
    
    if flagY or r[pos] == '1':
        ret += f(pos - 1, 1 if l[pos] == '0' else flagX, flagY, 1)
    ret %= MOD
    memo[pos][flagX][flagY][flagZ] = ret
    return ret

ans = f(59, 0, 0, 0)
print(ans)