invert :: M.Map (Int,Int) Int -> M.Map Int [Maybe (Int,Int)]
invert m = M.fromListWith (++) $ (0, [Nothing]) : do
    ((p,q),r) <- M.toList m
    return (r, [ Just (p,q) ])
invert :: M.Map (Int,Int) Int -> M.Map Int [Maybe (Int,Int)]
invert m = M.fromListWith (++) $ (0, [Nothing]) : do
    ((p,q),r) <- M.toList m
    return (r, [ Just (p,q) ])
invert m = M.fromListWith (++) $ (0, [Nothing]) : do
    ((p,q),r) <- M.toList m
    return (r, [ Just (p,q) ])