Count bits in range using BS
\ndef getcount(n, k):\n    n += 1\n    res = (n >> (k + 1)) << k\n    if (n >> k) & 1:\n        res += n & ((1 << k) - 1)\n    return res\n\ndef gettotal(n):\n 
```\ndef getcount(n, k):\n    n += 1\n    res = (n >> (k + 1)) << k\n    if (n >> k) & 1:\n        res += n & ((1 << k) - 1)\n    return res\n\ndef gettotal(n):\n    res = 0\n    for b in range(60):\n        res += getcount(n, b)\n    return res\n\nclass Solution:\n    def findProductsOfElements(self, queries: List[List[int]]) -> List[int]:\n        res = []\n        for f, t, mod in queries:\n            beg = 1\n            end = 10 ** 15\n            l = r = 0\n            while beg <= end:\n                mid = (beg + end) // 2\n                if gettotal(mid - 1) >= f:\n                    l = mid\n                    end = mid - 1\n                else:\n                    beg = mid + 1\n            beg = 1\n            end = 10 ** 15\n            while beg <= end:\n                mid = (beg + end) // 2\n                if gettotal(mid) - 1 <= t:\n                    r = mid\n                    beg = mid + 1\n                else:\n                    end = mid - 1\n            s = 0\n            for b in range(60):\n                if l > r:\n                    continue\n                s += b * (getcount(r, b) - getcount(l - 1, b))\n            curr = pow(2, s, mod)\n            val = l - 1\n            pos = gettotal(val - 1)\n            pw = 1\n            while val:\n                if val & 1:\n                    if f <= pos <= t:\n                        curr *= pw\n                        curr %= mod\n                    pos += 1\n                pw *= 2\n                val //= 2\n            if r + 1 != l - 1:\n                val = r + 1\n                pos = gettotal(val - 1)\n                pw = 1\n                while val:\n                    if val & 1:\n                        if f <= pos <= t:\n                            curr *= pw\n                            curr %= mod\n                        pos += 1\n                    pw *= 2\n                    val //= 2\n            res.append(curr)\n        return res\n```
['Python']