Python Solution
\n# Code\n\nclass Solution:\n    def findProductsOfElements(self, queries: List[List[int]]) -> List[int]:\n        def countOnes(max_val):\n            total_se
\n# Code\n```\nclass Solution:\n    def findProductsOfElements(self, queries: List[List[int]]) -> List[int]:\n        def countOnes(max_val):\n            total_set_bits = 0\n            n = max_val + 1\n            p = 0\n            while (1 << p) <= max_val:\n                total_pairs = n // (1 << (p + 1))\n                total_set_bits += total_pairs * (1 << p)\n                total_set_bits += max(0, n % (1 << (p + 1)) - (1 << p))\n                p += 1\n            return total_set_bits\n\n        def grab(n):\n            res = [0] * 64\n            for p in range(64):\n                total_pairs = (n + 1) // (1 << (p + 1))\n                res[p] += total_pairs * (1 << p)\n                remainder = (n + 1) % (1 << (p + 1))\n                if remainder > (1 << p):\n                    res[p] += remainder - (1 << p)\n            return res\n\n        def upTo(max_index):\n            min_val, max_val = 0, max_index + 5\n            highest, excess = 0, max_index + 1\n            while min_val <= max_val:\n                mid_val = min_val + (max_val - min_val) // 2\n                amt_of_ones = countOnes(mid_val)\n                if amt_of_ones < max_index + 1:\n                    highest = mid_val\n                    excess = max_index + 1 - amt_of_ones\n                    min_val = mid_val + 1\n                elif amt_of_ones > max_index + 1:\n                    max_val = mid_val - 1\n                else:\n                    highest = mid_val\n                    excess = 0\n                    break\n            bit_counts = grab(highest)\n            if excess == 0:\n                return bit_counts\n            higher = max_val + 1\n            for p in range(64):\n                if (higher & (1 << p)) != 0:\n                    bit_counts[p] += 1\n                    excess -= 1\n                    if excess == 0:\n                        return bit_counts\n            return bit_counts\n\n        def modPow(base, pow, mod):\n            if pow == 0:\n                return 1\n            lesser = modPow(base, pow // 2, mod)\n            lesser = (lesser * lesser) % mod\n            if pow % 2 == 1:\n                lesser = (lesser * base) % mod\n            return lesser\n\n        def fulfil(query):\n            from_val, to_val, mod = query\n            up_to = upTo(to_val)\n            littler = upTo(from_val - 1)\n            res = 1\n            pow_val = 1\n            for i in range(63):\n                amt = up_to[i] - littler[i]\n                if amt == 0:\n                    pow_val = (pow_val * 2) % mod\n                    continue\n                mult = modPow(1 << i, amt, mod)\n                res = (res * mult) % mod\n                pow_val = (pow_val * 2) % mod\n                if res == 0:\n                    return 0\n            return res\n\n        res = []\n        for query in queries:\n            res.append(fulfil(query))\n        return res\n\n```
['Python3']