Bit Manipulation + Binary Search + Cyclic Property Of Bits | 65 ms
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(q * log(1e15))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(q)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long bin_exp(long long a, long long b, long long mod){\n        long long res = 1;\n        while(b){\n            if(b & 1){\n                res = (res * a) % mod;\n            }\n            a = (a * a) % mod;\n            b >>= 1;\n        }\n        return res;\n    }\n    unsigned long long f(long long num, int i){\n        // returns the count of i-th bit set in range [0...num]\n        // based on the cyclic property of bits\n        unsigned long long a = (num + 1) / (1LL << (i+1));\n        a = a * (1LL << i);\n        long long excess = ((num + 1) % (1LL << (i+1))) - (1LL << i);\n        return a + max(excess, 0LL);\n    }\n    unsigned long long g(long long num){\n        unsigned long long sum = 0;\n        for(int i=0; i<63; i++){\n            unsigned long long t = f(num, i);\n            sum += t;\n        }\n        return sum;\n    }\n    unsigned long long g_bit(long long num){\n        unsigned long long sum = 0;\n        for(int i=0; i<63; i++){\n            unsigned long long t = f(num, i);\n            sum += t * i;\n        }\n        return sum;\n    }\n    long long binary_search(long long k){\n        long long low = 1, high = 1e15;\n        while(low <= high){\n            long long mid = low + ((high - low) >> 1);\n            unsigned long long set_bits = g(mid);\n            if(set_bits >= k){\n                high = mid - 1;\n            }else{\n                low = mid + 1;\n            }\n        }\n        return low;\n    }\n    vector<int> findProductsOfElements(vector<vector<long long>>& queries) {\n        int q = queries.size();\n        vector<int> v;\n        for(auto& query: queries){\n            long long lb = query[0]+1, ub = query[1]+1, mod = query[2];\n            long long lb_num = binary_search(lb), ub_num = binary_search(ub);\n\n            unsigned long long diff1 = lb - g(lb_num-1);\n            unsigned long long diff2 = ub - g(ub_num-1);\n\n            if(lb_num == ub_num){\n                long long c = 0;\n                unsigned long long ans = 1;\n                for(int i=0; i<63; i++){\n                    if(lb_num & (1LL << i)){\n                        c++;\n                        if(c>=diff1 && c<=diff2){\n                            ans = ans * (1LL << i);\n                            ans %= mod;\n                        }\n                    }\n                }\n                v.push_back(ans);\n                continue;\n            }\n\n            unsigned long long mid_part_sum = max(0ULL, g_bit(ub_num-1) - g_bit(lb_num));\n\n            unsigned long long mid_part = bin_exp(2, mid_part_sum, mod);\n\n            unsigned long long excess = 1;\n            // for left part which is uneven\n            int a = 0;\n            for(int i=0; i<63; i++){\n                if(lb_num & (1LL << i)){\n                    a++;\n                    if(a >= diff1){\n                        excess = excess * (1LL << i);\n                        excess %= mod;\n                    }\n                }\n            }\n            // for right part which is uneven\n            int b = 0;\n            for(int i=0; i<63; i++){\n                if(ub_num & (1LL << i)){\n                    b++;\n                    if(b <= diff2){\n                        excess = excess * (1LL << i);\n                        excess %= mod;\n                    }\n                }\n            }\n            unsigned long long ans = (mid_part * excess) % mod;\n            v.push_back(ans);\n        }\n        return v;\n    }\n};\n```
['Binary Search', 'Bit Manipulation', 'C++']