Sometimes, to find $$$E(X)$$$, where $$$X = f(X_1,X_2,...X_n)$$$ and $$$X_1,X_2..$$$ are R.Vs, we may need to brute force on all possibilities. This may be done easily by a bitmask, but I face the trouble to calculate the final answer. In virtually every problem, we are needed to report $$$(PQ^{-1})\%mod$$$, which seems undoable to me (in case the probabilities are also fractions). Is there any work-around for this? Please share tips related to not only this, but in general for other $$$E(X)$$$ calculations.