import Control.Applicative import Control.Monad import Control.Monad.State import Data.Vector.Unboxed (Vector, (!), (//)) import qualified Data.Vector.Unboxed as V main :: IO () main = do [n, m] <- f solve n <$> replicateM m getLine >>= putStrLn . unwords . map show where f = map read <$> words <$> getLine solve :: Int -> [String] -> [Int] solve n xs = evalState (f xs) (V.replicate n 0, 0, 1, 0, 0) where f :: [String] -> State (Vector Int, Int, Int, Int, Int) [Int] f [] = do (v, t, dr, _, _) <- get let pt = (t - dr) `mod` n return [pt + 1, v ! pt] f ("number":rs) = do (v, t, dr, d2, d4) <- get let nt = (t + dr) `mod` n if d2 > 0 then do put (v // [(t, (v ! t) - 2 * d2)], nt, dr, 0, d4) f ("number":rs) else if d4 > 0 then do put (v // [(t, (v ! t) - 4 * d4)], nt, dr, d2, 0) f ("number":rs) else do put (v // [(t, (v ! t) + 1)], nt, dr, d2, d4) f rs f ("drawtwo":rs) = do (v, t, dr, d2, d4) <- get let nt = (t + dr) `mod` n if d4 > 0 then do put (v // [(t, (v ! t) - 4 * d4)], nt, dr, d2, 0) f ("drawtwo":rs) else do put (v // [(t, (v ! t) + 1)], nt, dr, d2 + 1, d4) f rs f ("drawfour":rs) = do (v, t, dr, d2, d4) <- get let nt = (t + dr) `mod` n if d2 > 0 then do put (v // [(t, (v ! t) - 2 * d2)], nt, dr, 0, d4) f ("drawfour":rs) else do put (v // [(t, (v ! t) + 1)], nt, dr, d2, d4 + 1) f rs f ("skip":rs) = do (v, t, dr, d2, d4) <- get let nt = (t + dr) `mod` n if d2 > 0 then do put (v // [(t, (v ! t) - 2 * d2)], nt, dr, 0, d4) f ("skip":rs) else if d4 > 0 then do put (v // [(t, (v ! t) - 4 * d4)], nt, dr, d2, 0) f ("skip":rs) else do let nnt = (nt + dr) `mod` n put (v // [(t, (v ! t) + 1)], nnt, dr, d2, d4) f rs f ("reverse":rs) = do (v, t, dr, d2, d4) <- get let nt = (t + dr) `mod` n if d2 > 0 then do put (v // [(t, (v ! t) - 2 * d2)], nt, dr, 0, d4) f ("reverse":rs) else if d4 > 0 then do put (v // [(t, (v ! t) - 4 * d4)], nt, dr, d2, 0) f ("reverse":rs) else do let nt' = (t - dr) `mod` n put (v // [(t, (v ! t) + 1)], nt', negate dr, d2, d4) f rs