import Data.Tuple import Data.List import Data.Ord data Tree a = Node a (Tree a) (Tree a) memoize f = memof where memof = f (query (blank memof)) blank f = blank' 1 where blank' k = Node (f k) (blank' (2*k)) (blank' (2*k+1)) query m x = query' m bits where bits = reverse (take w (unfoldr (return . swap . flip divMod 2) x)) w = floor (logBase 2 (fromIntegral x)) query' (Node v left right) bs | bs == [] = v | head bs == 0 = query' left (tail bs) | otherwise = query' right (tail bs) combBy :: Integer -> [a] -> [[a]] combBy 0 _ = [[]] combBy _ [] = [] combBy n (x:xs) = map (x:) (combBy (n-1) xs) ++ combBy n xs main = do [h,w,k,p] <- map read . words <$> getLine xyns <- map ((\[x,y,n] -> ((read x, read y),n)) . words) . lines <$> getContents let (r,fs) = japari (h+1) (w+1) k p xyns print (mod r (10^9+7)) mapM_ putStrLn fs japari :: Integer -> Integer -> Integer -> Integer -> [((Integer,Integer),String)] -> (Integer, [String]) japari h w k p xyns = (route h w (map (pos.fst) left) 0, (map snd xyns) \\ (map snd left)) where left = maximumBy (comparing (\fs -> route h w (map (pos.fst) fs) 0)) (combBy (k-p) xyns) pos (x,y) = y*h + x route h w fs = memoize $ \f n -> if isFriend n then 0 else if isGoal n then 1 else if isNB n then f (n+1) else if isEB n then f (n+h) else f (n+1) + f (n+h) where isGoal n = n == h*w-1 isNB n = div n h == w-1 isEB n = mod n h == h-1 isFriend n = elem n fs