Function to return the modular inverse using Fermat 's little theorem
def mi ( x ) : NEW_LINE INDENT global mod NEW_LINE p = mod - 2 NEW_LINE s = 1 NEW_LINE while p != 1 : NEW_LINE INDENT if p % 2 == 1 : NEW_LINE INDENT s = ( s * x ) % mod NEW_LINE DEDENT x = ( x * x ) % mod NEW_LINE p //= 2 NEW_LINE DEDENT return ( s * x ) % mod NEW_LINE DEDENT