问题描述:
Write a function to count array elements having modular inverse under given prime number p equal to itself.

测试用例:
['assert modular_inverse([ 1, 6, 4, 5 ], 4, 7) == 2', 'assert modular_inverse([1, 3, 8, 12, 12], 5, 13) == 3', 'assert modular_inverse([2, 3, 4, 5], 4, 6) == 1']

生成的代码:
def modular_inverse(arr, n, p):
    count = 0
    for num in arr:
        # Calculate modular inverse using Fermat's Little Theorem
        if pow(num, p-2, p) == num:
            count += 1
    return count

# Test cases
assert modular_inverse([1, 6, 4, 5], 4, 7) == 2
assert modular_inverse([1, 3, 8, 12, 12], 5, 13) == 3
assert modular_inverse([2, 3, 4, 5], 4, 6) == 1