def zaction(op, inv):
    """
    Return a function
      f: (G, Z) -> G
          g, n |-> g op g op ... (n-1 times) ... op g

    If n is zero, it returns identity element.  If n is negative, it
    returns |n|-1 times op on the inverse of g.
    """
    def f(g, n):
        result = op(g, inv(g)) # identity
        if n < 0:
            g, n = inv(g), -n
        for i in range(n):
            result = op(result, g)
        return result
    return f