def METHOD_NAME(N,W=aleph0,H=aleph0,useDivmod=False):
    """getNthPairBounded(N, W, H) -> (x, y)
    Return the N-th pair such that 0 <= x < W and 0 <= y < H."""
    if W <= 0 or H <= 0:
        raise ValueError,"Invalid bounds"
    elif N >= W*H:
        raise ValueError,"Invalid input (out of bounds)"
    # Simple case...
    if W is aleph0 and H is aleph0:
        return getNthPair(N)
    # Otherwise simplify by assuming W < H
    if H < W:
        x,y = METHOD_NAME(N,H,W,useDivmod=useDivmod)
        return y,x
    if useDivmod:
        return N%W,N//W
    else:
        # Conceptually we want to slide a diagonal line across a
        # rectangle. This gives more interesting results for large
        # bounds than using divmod.
        # If in lower left, just return as usual
        cornerSize = base(W)
        if N < cornerSize:
            return getNthPair(N)
        # Otherwise if in upper right, subtract from corner
        if H is not aleph0:
            M = W*H - N - 1
            if M < cornerSize:
                x,y = getNthPair(M)
                return (W-1-x,H-1-y)
        # Otherwise, compile line and index from number of times we
        # wrap.
        N = N - cornerSize
        index,offset = N%W,N//W
        # p = (W-1, 1+offset) + (-1,1)*index
        return (W-1-index, 1+offset+index)