def modular_inverse(arr, N, P):
    count = 0
    for x in arr:
        # modular inverse of x under prime P is x^(P-2) mod P (Fermat's little theorem)
        if pow(x, P - 2, P) == x % P:
            count += 1
    return count