Given an integer n, return the decimal value of the binary string formed by concatenating the binary representations of 1 to n in order, modulo 109 + 7.

&nbsp;
Example 1:

Input: n = 1
Output: 1
Explanation: "1" in binary corresponds to the decimal value 1. 


Example 2:

Input: n = 3
Output: 27
Explanation: In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.


Example 3:

Input: n = 12
Output: 505379714
Explanation: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 109 + 7, the result is 505379714.


&nbsp;
Constraints:


	1 &lt;= n &lt;= 105


class Solution:
    def concatenatedBinary(self, n: int) -> int:
        modulo = 10 ** 9 + 7
        shift = 0 # tracking power of 2
        res = 0
        
        for i in range(1, n+1):
            if i & (i - 1) == 0: # see if num reaches a greater power of 2
                shift += 1
            res = ((res << shift) + i) % modulo # do the simulation
        
        return res
class Solution {
    public int concatenatedBinary(int n) {
        long res = 0;
        for (int i = 1; i <= n; i++) {
            res = (res * (int) Math.pow(2, Integer.toBinaryString(i).length()) + i) % 1000000007;
        }
        return (int) res;
    }
}
class Solution {
public:
    
    int numberOfBits(int n) {
		  return log2(n) + 1;
    }
    
    int concatenatedBinary(int n) {
        long ans = 0, MOD = 1e9 + 7;
        
        for (int i = 1; i <= n; ++i) {
            int len = numberOfBits(i);
            ans = ((ans << len) % MOD + i) % MOD;
        }
        return ans;
    }
};
var concatenatedBinary = function(n) {
    let num = 0;
    
    for(let i = 1; i <= n; i++) {
        //OR num *= (1 << i.toString(2).length)
        num *= (2 ** i.toString(2).length) 
        num += i;
        num %= (10 ** 9 + 7)
    }
    return num;
};
Concatenation of Consecutive Binary Numbers