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

    An element 'a' is its own modular inverse modulo P if (a * a) % P == 1.
    Since P is a prime number, this equation simplifies to:
    a^2 ≡ 1 (mod P)
    a^2 - 1 ≡ 0 (mod P)
    (a - 1)(a + 1) ≡ 0 (mod P)

    This implies either (a - 1) ≡ 0 (mod P) or (a + 1) ≡ 0 (mod P).
    Therefore, a ≡ 1 (mod P) or a ≡ -1 (mod P).
    a ≡ -1 (mod P) is equivalent to a ≡ P - 1 (mod P).

    So, we need to count elements 'x' in the array such that
    x % P == 1 or x % P == P - 1.
    """
    count = 0
    p_minus_1 = P - 1
    for x in arr:
        remainder = x % P
        if remainder == 1 or remainder == p_minus_1:
            count += 1
    return count

```