```python
def modular_inverse(arr, N, P):
    """
    Counts array elements having a modular inverse equal to themselves under a given prime number P.

    An element 'x' is its own modular inverse modulo P if (x * x) % P == 1.
    This function iterates through the array and counts how many elements satisfy this condition.

    Args:
        arr: A list of integers.
        N: The size of the array (unused in this Python implementation).
        P: A prime number serving as the modulus.

    Returns:
        The count of elements in arr that are their own modular inverse modulo P.
    """
    self_inverse_count = 0

    for number in arr:
        # An element is its own modular inverse if its square is 1 modulo P.
        # We calculate the remainder first to work with smaller numbers.
        remainder = number % P

        if (remainder * remainder) % P == 1:
            self_inverse_count += 1

    return self_inverse_count
```