def inverse(self, N):
        """
        Returns the modular inverse of an integer with respect to the field
        characteristic, P.

        Use the Extended Euclidean Algorithm:
        https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
        """
        if N == 0:
            return 0
        lm, hm = 1, 0
        low, high = N % self.P, self.P
        while low > 1:
            r = high//low
            nm, new = hm - lm * r, high - low * r
            lm, low, hm, high = nm, new, lm, low

        return lm % self.P