Find the value of P and modular inverse of Q modulo 998244353 | Function to find the value of P * Q ^ - 1 mod 998244353 ; Loop to find the value until the expo is not zero ; Multiply p with q if expo is odd ; Reduce the value of expo by 2 ; Driver code ; Function call
function calculate ( P , Q ) { let mod = 998244353 , expo ; expo = mod - 2 ; p = 748683265 ; while ( expo != 0 ) { if ( ( expo & 1 ) == 1 ) { P = ( P * Q ) % mod ; } Q = ( Q * Q ) % mod ; expo >>= 1 ; } return p ; } let p = 1 , q = 4 ; document . write ( calculate ( p , q ) ) ;