## Python # Approach : Bitmasking + Prefix Multiplication

```
def findGoodArray(n):
res = []
while n :
lastSetBit = n & -n
res.append(lastSetBit)
n -= lastSetBit
return res
def getQueryResults(n,queries):
goodArr = findGoodArray(n)
prefixMul = [1]*(len(goodArr)+1)
for i in range(len(goodArr)):
prefixMul[i+1] = prefixMul[i]*goodArr[i]
res = [0]*len(queries)
for idx,[l,r,m] in enumerate(queries):
res[idx] = ( prefixMul[r]//prefixMul[l-1] ) % m
return res
print(getQueryResults(26,[[1,2,1009],[3,3,5]]))
print(getQueryResults(6,[[1,2,4],[2,2,8],[1,1,4]]))
```