def splitmod(n, k):
    """
    Split n into k lists containing the elements of n in positions i (mod k).
    Return the heads of the lists and the tails.
    """
    heads = [None]*k
    tails = [None]*k
    i = 0
    while n is not None:
        if heads[i] is None:
            heads[i] = n
        if tails[i] is not None:
            tails[i].next = n
        tails[i] = n
        n.next, n = None, n.next
        i = (i+1)%k
    return heads, tails