Range AND Java


where i am getting wrong?
/* package codechef; // don’t place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Main
{   static long mod=1000000007;
	public static void main (String[] args) throws java.lang.Exception
	{
	    Scanner sc=new Scanner(System.in);
	    int t=sc.nextInt();
	    while(t-->0){
	        long L=sc.nextLong();
	        long R=sc.nextLong();
	        long pow=0;
	        long newL=L;
	        while(Math.floor(L)>1){
	            L=L/2;pow++;
	        }
	        newL=newL%mod;
	        R=R%mod;
	        long near=(long)(Math.pow(2,pow))%mod;
	        //System.out.println(pow+"/"+newL);
	        long next=((long)(Math.pow(2,pow+1))-1)%mod;
	        //System.out.println(R+"/"+next);
	        long result=0;
	        if(R<=next){result=((R-newL)*(near)+newL)%mod;}
	        else{result=((next-newL)*(near)+newL)%mod;}
	        System.out.println(result);
	    }
	}
}

1
514 515
WA on this test case!!

514+(514&515)=514+514=1028