How to convert the probability P/Q to P⋅Q−1 where Q is co-prime with mod I was dealing with probability in programming but i was stuck on the final answer part.
Below is statement in which format i have to give the answer
Can you find the probabilities It can be proved that for each of these values, the probability can be expressed as a fraction PQ, where P and Q are integers (P≥0, Q>0) and Q is co-prime with 998,244,353. You should compute P⋅Q−1 modulo 998,244,353 for each of these values. 
Below is probability that can be calculated on paper and 2nd line is for what i have to print can any1 explain how should calculate P.Q-1.
For 1st Input
Calculated Probability = 1/4 
Answer : 748683265
For 2nd Input
Calculated Probability was = 1/16 , 3/16 , 3/16 , 9/16
Answer that was given = 436731905 935854081 811073537 811073537

If anything is unclear then pls comment as iam new to community I am not very good at asking questions.
Thank you for your reply in advance. 
 A: $$\frac{p}{q} \equiv p \cdot q^{-1} \mod 998244353$$
Since $q$ is coprime with $998244353$, $q^{-1}$ always exist. It can be found using extended euclidean algorithm, shown here.
For example, $\frac{1}{4} \equiv 4^{-1} \mod 998244353$.
You can check that $4 \cdot 748683265 = 2994733060 \equiv 1 \mod 998244353$,
so $4^{-1} \equiv 748683265 \mod 998244353$.
A: Works when m and a are coprime
The idea is to use Extended Euclidean algorithms that takes two integers ‘a’ and ‘b’, finds their gcd and also find ‘x’ and ‘y’ such that
ax + by = gcd(a, b) 
To find multiplicative inverse of ‘a’ under ‘m’, we put b = m in above formula. Since we know that a and m are relatively prime, we can put value of gcd as 1.
ax + my = 1 
If we take modulo m on both sides, we get
ax + my ≡ 1 (mod m)
We can remove the second term on left side as ‘my (mod m)’ would always be 0 for an integer y.
ax  ≡ 1 (mod m) 
So the ‘x’ that we can find using Extended Euclid Algorithm is multiplicative inverse of ‘a’
Code:
Modular multiplicative inverse Code
A: p=998244353;
int power(int x, int y, int p) 
{ 
    int res = 1;      // Initialize result 
x = x % p;  // Update x if it is more than or 
            // equal to p 

while (y > 0) 
{ 
    // If y is odd, multiply x with result 
    if (y & 1) 
        res = (res*x) % p; 

    // y must be even now 
    y = y>>1; // y = y/2 
    x = (x*x) % p; 
} 
return res; 

} 
// Returns n^(-1) mod p 
int modInverse(int n, int p) 
{ 
    return power(n, p-2, p); 
} 
A: This can be solved using Extended Euclidean Algorithm. You can visit the following link for further info and code solutions:
https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/
Also there is a same question on overflow for this.
