module Main where

import Control.Applicative
import Data.Array

main = do
    n <- read <$> getLine
    ns <- map read <$> words <$> getLine
    print $ solve n ns

solve :: Int -> [Int] -> Int
solve 1 _ = 1
solve 2 _ = 2
solve n ns =
    let arr = listArray (0, n-1) ns
        res = foldl (\(m, b) i ->
                        if arr ! i == i
                        then (m + 1, b)
                        else (m, b || arr ! (arr ! i) == i)
                    ) (0, False) [0 .. n-1]
    in case res of
        (m, False) -> min n (m + 1)
        (m, _)     -> m + 2

module Main where

import Control.Applicative
import Data.Array

main = do
    n <- read <$> getLine
    ns <- map read <$> words <$> getLine
    print $ solve n ns

solve :: Int -> [Int] -> Int
solve 1 _ = 1
solve 2 _ = 2
solve n ns =
    let arr = listArray (0, n-1) ns
        res = foldr (\i (m, b) ->
                        if arr ! i == i
                        then (m + 1, b)
                        else (m, b || arr ! (arr ! i) == i)
                    ) (0, False) [0 .. n-1]
    in case res of
        (m, False) -> min n (m + 1)
        (m, _)     -> m + 2

main = do
    input <- getLine
    let n = read (input) :: Int
    input <- getLine
    let input' = zip [0..] (f $ split ' ' input)
    let orig = sum [ 1 | (a, b) <- input', a == b ]
    let swap = qsort [ (b, a) | (a, b) <- input' ]
    let count = sum [ 1 | ((a, b), (c, d)) <- (zip input' swap), b == d ]

    print (if orig == n then orig else (if orig < count then orig + 2 else orig + 1))

f :: [String] -> [Int]
f x = map read x

qsort :: (Ord a) => [a] -> [a]
qsort [] = []
qsort (x:xs) = qsort [ a | a <- xs, a <= x ] ++ [x] ++ qsort [ a | a <- xs, a > x ]

split' :: (Eq a) => a -> [a] -> [a] -> [[a]]
split' _ [] l = [l]
split' v (x:xs) l
   | x /= v = split' v xs (l ++ [x])
   | otherwise = [l] ++ split' v xs []

split :: (Eq a) => a -> [a] -> [[a]]
split v xs = split' v xs []

import Control.Monad (liftM, unless, when)
import Data.List (group, intercalate, sort)
import Data.Array
import Prelude hiding (reads)

reads :: Read a => IO [a]
reads = liftM (map read . words) getLine

main :: IO ()
main = do
  n <- getLine
  vals <- reads
  putStrLn $ show $ solve (read n) vals

foldla :: (b -> Int -> a -> b) -> b -> Array Int a -> b
foldla f z0 ar = go z0 lo
  where go z i
          | i > hi = z
          | otherwise = go (f z i (ar ! i)) $ i + 1
        (lo, hi) = bounds ar

samePos :: Array Int Int -> Int
samePos a = foldla (\acc idx val -> if idx == val then acc + 1 else acc) 0 a

improvement :: Array Int Int -> Int
improvement perm
  = foldla (\best idx val -> if idx == val then best
                             else if perm ! val == idx then 2
                                  else max 1 best) 0 perm

solve :: Int -> [Int] -> Int
solve n xs = (samePos perm) + (improvement perm)
  where
    dim = (0, n - 1)
    perm = array dim $ zip (range dim) xs

import Control.Monad (liftM)
import Data.Array ((!), bounds, listArray)
import Data.List (intercalate)
import Prelude hiding (reads)

reads :: Read a => IO [a]
reads  =  liftM (map read . words) getLine

solve :: [Int] -> Int
solve xs  =  min n (k + l)
  where
    n  =  length xs
    a  =  listArray (0, n-1) xs
    k  =  length [0 | i <- [0 .. n-1], i == a ! i]
    l  =  if null [0 | i <- [0 .. n-1], i /= a ! i, i == a ! (a ! i)]
                 then 1 else 2

prints :: Show a => [a] -> IO ()
prints  =  putStrLn . intercalate " " . map show

main :: IO ()
main  =  getLine >> reads >>= print . solve

import Data.IntMap (IntMap, (!), foldlWithKey, fromList)

boolToInt :: Bool -> Integer
boolToInt True  = 1
boolToInt False = 0

stationaryCount :: IntMap Int -> Integer
stationaryCount = foldlWithKey (\z k x -> z + (boolToInt (k == x))) 0

hasTransp :: IntMap Int -> Bool
hasTransp perm = foldlWithKey (\z k x -> z || ((k /= x) && (perm ! x == k))) False perm

solve :: Integer -> [Int] -> Integer
solve n xs = case (hasTransp xs') of
                True  -> s + 2
                False -> s + (boolToInt $ n - s > 1)
         where xs' = fromList $ zip [0..] xs
               s = stationaryCount xs'
main = do
    [n] <- (map (read :: String -> Integer) . words) `fmap` getLine
    seq <- (map (read :: String -> Int) . words) `fmap` getLine
    print $ solve n seq

import Control.Applicative ((<$>), (<*>))
import Data.Array ((!), listArray)
import Data.List (sortBy)
import Data.Maybe (listToMaybe)

main :: IO ()
main = print =<< solve <$> readLn <*> (map read . words <$> getLine)

solve :: Int -> [Int] -> Int
solve n as = length [ a | (i, a) <- zip [0..] as, i == a ]
         + maybe 0 succ (listToMaybe (sortBy (flip compare) [ fromEnum (as' ! a == i) | (i, a) <- zip [0..] as, i /= a ]))
         where as' = listArray (0, n - 1) as

module Main where

import Control.Applicative
import Data.Array
import Data.List

main = do
    n <- read <$> getLine
    ns <- map read <$> words <$> getLine
    print $ solve n ns

solve :: Int -> [Int] -> Int
solve 1 _ = 1
solve 2 _ = 2
solve n ns =
    let arr = listArray (0, n-1) ns
        res = foldl' (\(m, b) i ->
                        if arr ! i == i
                        then (m + 1, b)
                        else (m, b || arr ! (arr ! i) == i)
                    ) (0, False) [0 .. n-1]
    in case res of
        (m, False) -> min n (m + 1)
        (m, _)     -> m + 2

import Control.Monad (liftM)
import Data.Array ((!), bounds, listArray)
import Data.List (intercalate)
import Prelude hiding (reads)

reads :: Read a => IO [a]
reads  =  liftM (map read . words) getLine

solve :: [Int] -> Int
solve xs  =  min n (k + l)
  where
    n  =  length xs
    a  =  listArray (0, n-1) xs
    k  =  length [0 | i <- [0 .. n-1], i == a ! i]
    l  =  if null [0 | i <- [0, n-1], i /= a ! i, i == a ! (a ! i)]
                 then 1 else 2

prints :: Show a => [a] -> IO ()
prints  =  putStrLn . intercalate " " . map show

main :: IO ()
main  =  getLine >> reads >>= print . solve

module Main where

import Control.Applicative
import Data.Array

main = do
    n <- read <$> getLine
    ns <- map read <$> words <$> getLine
    print $ solve n ns

solve :: Int -> [Int] -> Int
solve n ns =
    let arr = listArray (0 :: Int,n-1) ns
        res = foldr (\i (m, b) ->
                        if arr ! i == i
                        then (m + 1, b)
                        else (m, arr ! (arr ! i) == i)
                    ) (0, False) [0 .. n-1]
    in case res of
        (m, False) -> min n (m + 1)
        (m,_) -> m + 2

module Main where

import Control.Applicative
import Data.Array

main = do
    n <- read <$> getLine
    ns <- map read <$> words <$> getLine
    print $ solve n ns

solve :: Int -> [Int] -> Int
solve 1 _ = 1
solve 2 _ = 2
solve n ns =
    let arr = listArray (0, n-1) ns
        res = foldr (\i (m, b) ->
                        if arr ! i == i
                        then (m + 1, b)
                        else (m, arr ! (arr ! i) == i)
                    ) (0, False) [0 .. n-1]
    in case res of
        (m, False) -> min n (m + 1)
        (m, _)     -> m + 2

module Main where

import Control.Applicative
import Data.Array

main = do
    n <- read <$> getLine
    ns <- map read <$> words <$> getLine
    print $ solve n ns

solve :: Int -> [Int] -> Int
solve 1 _ = 1
solve 2 _ = 2
solve n ns =
    let arr = listArray (0 :: Int,n-1) ns
        res = foldr (\i (m, b) ->
                        if arr ! i == i
                        then (m + 1, b)
                        else (m, arr ! (arr ! i) == i)
                    ) (0, False) [0 .. n-1]
    in case res of
        (m, False) -> min n (m + 1)
        (m,_) -> m + 2

main = do
    input <- getLine
    let n = read (input) :: Int
    input <- getLine
    let input' = zip [0..] (f $ split ' ' input)
    let orig = sum [ 1 | (a, b) <- input', a == b ]
    let swap = qsort [ (b, a) | (a, b) <- input' ]
    let count = sum [ 1 | ((a, b), (c, d)) <- (zip input' swap), b == d ]

    print (if count == n then orig else (if orig < count then orig + 2 else orig + 1))

f :: [String] -> [Int]
f x = map read x

qsort :: (Ord a) => [a] -> [a]
qsort [] = []
qsort (x:xs) = qsort [ a | a <- xs, a <= x ] ++ [x] ++ qsort [ a | a <- xs, a > x ]

split' :: (Eq a) => a -> [a] -> [a] -> [[a]]
split' _ [] l = [l]
split' v (x:xs) l
   | x /= v = split' v xs (l ++ [x])
   | otherwise = [l] ++ split' v xs []

split :: (Eq a) => a -> [a] -> [[a]]
split v xs = split' v xs []

main = do
    input <- getLine
    let n = read (input) :: Int
    input <- getLine
    let input' = zip [0..] (f $ split ' ' input)
    print (sum [1 | (a, b) <- input', a == b] + swap input' 0)

swap [] m = m
swap [_] m = m
swap ((x1,y1):x@(x2,y2):xs) m = swap (x:xs) (max m v)
    where v = (if x1 == y1 then -1 else 0) + (if x2 == y2 then -1 else 0) + (if x1 == y2 then 1 else 0) + (if x2 == y1 then 1 else 0)

f :: [String] -> [Int]
f x = map read x

split' :: (Eq a) => a -> [a] -> [a] -> [[a]]
split' _ [] l = [l]
split' v (x:xs) l
   | x /= v = split' v xs (l ++ [x])
   | otherwise = [l] ++ split' v xs []

split :: (Eq a) => a -> [a] -> [[a]]
split v xs = split' v xs []


{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE Strict #-}

import Control.Applicative (liftA2)
import Control.Monad (replicateM)
import Control.Monad.State (State, evalState, get, gets, put)
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Maybe (fromJust)

main :: IO ()
-- main = C.interact $ runScanner (C.pack <$> testCase)
main = C.interact $ runScanner (C.unlines <$> numberOf (C.pack <$> testCase))

testCase :: Scanner String
testCase = (: "") . maximum <$> str

-------------------------- Template ------------------------------------------
type Scanner = State [C.ByteString]

runScanner :: Scanner a -> C.ByteString -> a
runScanner = runScannerWith C.words

runScannerWith :: (C.ByteString -> [C.ByteString]) -> Scanner a -> C.ByteString -> a
runScannerWith t s = evalState s . t

peek :: Scanner C.ByteString
peek = gets head

bstr :: Scanner C.ByteString
bstr = get >>= \case s : ss -> put ss >> return s

str :: Scanner String
str = C.unpack <$> bstr

int :: Scanner Int
int = fst . fromJust . C.readInt <$> bstr

integer :: Scanner Integer
integer = read . C.unpack <$> bstr

double :: Scanner Double
double = read . C.unpack <$> bstr

decimal :: Int -> Scanner Int
decimal p = round . ((10 ^ p) *) <$> double

numberOf :: Scanner a -> Scanner [a]
numberOf s = int >>= flip replicateM s

many :: Scanner a -> Scanner [a]
many s = get >>= \case [] -> return []; _ -> (:) <$> s <*> many s

till :: (C.ByteString -> Bool) -> Scanner a -> Scanner [a]
till p s = do
  t <- peek
  if p t
    then return []
    else (:) <$> s <*> till p s

times :: Int -> Scanner a -> Scanner [a]
times = replicateM

(><) = times

two, three, four :: Scanner a -> Scanner [a]
[two, three, four] = map times [2 .. 4]

pair :: Scanner a -> Scanner b -> Scanner (a, b)
pair = liftA2 (,)

{-# LANGUAGE TypeApplications, OverloadedStrings, BlockArguments #-}

module Main where

import           Data.Char
import           Data.List
import           Data.Functor
import           Control.Monad

main :: IO ()
main =
  getLine
    <&> read @Int
    >>= flip replicateM (getLine <&> map digitToInt)
    >>= mapM_ (print . maximum)

import Control.Monad

lgst :: [Char] -> Char -> [Char]
lgst [a] b = if a > b then [a] else [b]
lgst (a:xs) mx = if a>mx then lgst xs a else lgst xs mx

main = do
    times <- readLn
    replicateM_ times $ do
        n <- getLine
        let l = lgst n '0'
        putStrLn l

module Main where

main = interact solve

solve inp =
    let
        linp = lines inp
        t = read $ head linp :: Int
        xs = take t . tail $ linp
        logic x = maximum x
    in
        unlines . map ((\x -> x : "") . logic) $ xs

import Control.Monad ( forM )
import Data.List ( intersperse )

main :: IO ()
main = do
    s <- getLine
    let n = read s :: Int
    r <- forM [1..n] $ \_ -> do
        z <- getLine
        return (maximum z) :: IO Char
    putStrLn $ intersperse '\n' r

{-# OPTIONS_GHC -O2 #-}

import Control.Monad
import Prelude as P
import Data.Int (Int64)
import Data.List as L
import qualified Data.Bits as Bits
import qualified Data.Char as Char
import qualified System.IO as Io

import Data.Word (Word8)
import qualified Data.ByteString as B
--import Data.IntSet

convert :: [Int] -> [[Int]]
convert xs
  | null xs || all (==0) xs = []
  | otherwise = nextnum : convert nextxs
  where nextnum = map (\x -> if x > 0 then 1 else 0) xs
        nextxs = map (\x -> if x > 0 then x-1 else 0) xs

solve :: IO ()
solve = do
  s <- getLine
  let dig = map Char.digitToInt $ s
      res = convert dig
    in putStrLn $ show $ length res

main = do
  t <- readLn :: IO Int
  replicateM_ t solve

import qualified Data.ByteString.Lazy.Char8 as P
import qualified Data.ByteString.Builder as B
import System.IO

import Control.Monad
import Control.Monad.Trans.State
import Control.Monad.Trans.Class
import Data.Semigroup

main :: IO ()
main = (P.getContents >>=) . evalStateT $ do
  [t] <- readInts
  replicateM_ t $ do
    s <- readLine
    let  ans = P.foldl' max minBound s
    lift $ B.hPutBuilder stdout (B.char7 ans <> B.char7 '\n')

type SIO = StateT P.ByteString IO

readLine :: SIO P.ByteString
readLine = do
  (out, next) <- P.break (<= '\r') <$> get
  put $ P.dropWhile (<= '\r') next
  pure out

readInts :: SIO [Int]
readInts = do
  currLine <- readLine
  pure [x | Just (x, _) <- P.readInt <$> P.words currLine]

import Control.Monad

solve 0 = 0
solve n = max (mod n 10) $ solve (div n 10)

printResult = do
    n <- readLn :: IO Int
    let r = solve n
    print r

main = do
    t <- readLn :: IO Int
    replicateM_ t printResult

module Main where

import Control.Monad
import Data.List
import qualified Data.Set as S

getStrList :: IO [String]
getStrList = words <$> getLine

getIntList :: IO [Int]
getIntList = map read <$> getStrList

printList :: (Show a) => [a] -> IO ()
printList = putStrLn . list2Str

list2Str :: (Show a) => [a] -> String
list2Str = concat . intersperse " " . fmap show

yesOrNo :: Bool -> String
yesOrNo True = "YES"
yesOrNo False = "NO"

tCase :: IO () -> IO ()
tCase x = do
    t <- readLn
    replicateM_ t $ x

main :: IO ()
main = tCase process

--------------------------------------------------------------------------------

process :: IO ()
process = do
    s <- getLine
    putChar $ foldl max '1' s
    putChar '\n'


{-#LANGUAGE Strict #-}

import Control.Monad(replicateM)

data Point = Point Int Int

main :: IO ()
main = do
    t <- read <$> getLine
    res <- replicateM t $ do
        [w, h] <- map read . words <$> getLine :: IO [Int]
        bottom <- tail . map read . words <$> getLine :: IO [Int]
        top <- tail . map read . words <$> getLine :: IO [Int]
        left <- tail . map read . words <$> getLine :: IO [Int]
        right <- tail . map read . words <$> getLine :: IO [Int]
        return $ solve (Point w h) bottom top left right
    putStrLn $ unlines . map show $ res
    return ()

solve :: Point -> [Int] -> [Int] -> [Int] -> [Int] -> Int
solve p@(Point w h) b t l r = max (horz * h) (vert * w)
    where
        horz    = max top bottom
        vert    = max left right
        top     = last t - head t
        bottom  = last b - head b
        left    = last l - head l
        right   = last r - head r

{-#LANGUAGE Strict #-}

import Control.Monad(replicateM)

data Point = Point Int Int

main :: IO ()
main = do
    t <- read <$> getLine
    res <- replicateM t $ do
        [w, h] <- map read . words <$> getLine -- :: IO [Int]
        bottom <- breadth . fAndL . tail . map read . words <$> getLine -- :: IO [Int]
        top <- breadth . fAndL . tail . map read . words <$> getLine -- :: IO [Int]
        left <- breadth . fAndL . tail . map read . words <$> getLine -- :: IO [Int]
        right <- breadth . fAndL . tail . map read . words <$> getLine -- :: IO [Int]
        return $ solve (Point w h) bottom top left right
    putStrLn $ unlines . map show $ res
    return ()

solve :: Point -> Int -> Int -> Int -> Int -> Int
solve p@(Point w h) b t l r = max (horz * h) (vert * w)
    where
        horz    = max t b
        vert    = max l r
        -- top     = last t - head t
        -- bottom  = last b - head b
        -- left    = last l - head l
        -- right   = last r - head r

fAndL :: [a] -> (a, a)
fAndL xs = (head xs, last xs)

breadth :: Num a => (a, a) -> a
breadth (f, l) = abs (l - f)

{-# LANGUAGE TypeApplications #-}

module Main where

import Control.Monad
import Data.Maybe
import Data.List

removeElem :: Int -> [a] -> [a]
removeElem i xs = take i xs <> tail (drop i xs)

main = do
  n <- read @Int <$> getLine
  replicateM n $ do
    [w,h] <- fmap (read @Integer) . words <$> getLine
    let getPs = fmap (read @Integer) .  tail . words <$> getLine
    let getMaxLen l = last l - head l
    lowHoriz <- getPs
    upHoriz <- getPs
    leftVert <- getPs
    rightVert <- getPs
    print @Integer $ maximum [getMaxLen lowHoriz * h,
                             getMaxLen upHoriz * h,
                             getMaxLen leftVert * w,
                             getMaxLen rightVert * w]


import Data.List (foldl')

main :: IO ()
main = getContents >>= print . solve . map (map read . words) . lines

solve :: [[Integer]] -> Integer
solve [[n, _], as] = snd $ foldl' (\(a, b) c -> (c, b + (c + n - a) `mod` n)) (1, 0) as
solve _ = undefined

calc n t
  | t < 0 = t+n
  | otherwise = t
count c n h [] = c
count c n h (m:ms)
  | h == m = count c n h ms
  | otherwise = count (c+(calc n (m-h))) n m (m:ms)
parse=(map (map read)).(map words).lines
main=interact$ show.(\[x, y]->count 0 (head x) 1 y).parse

module Main (main)
    where

import Control.Monad (replicateM)

main :: IO ()
main = print . solve . map (map read . words) =<< replicateM 2 getLine
    where solve [[n,_],xs] = go n 1 0 xs
          go _ _ t [] = t
          go n c t (x:xs)
            | x >= c    = go n x (t + x - c) xs
            | otherwise = go n x (t + n - c + x) xs

main = do
	s<-getLine
	rest<-getLine
	putStrLn $ show $ solve (read (head (words s))) (map read (words rest)) 1
solve::Integer->[Integer]->Integer->Integer
solve n [] c = 0
solve n w c = travel n c (head w) + solve n (tail w) (head w)

travel n a b | b >= a = (b-a)
             | otherwise = n-a+b

main :: IO()
main = print . solve . map read . words =<< getContents

solve :: [Integer] -> Integer
solve (n:m:a) = solve' a 1
    where
        solve' :: [Integer] -> Integer -> Integer
        solve' []     _    = 0
        solve' (a:ax) last | a >= last = a - last + (solve' ax a)
                           | otherwise = a - last + n + (solve' ax a)

import Data.Int

solution :: Int64 -> [Int64] -> [Int64]
solution n [] = []
solution n [k] = []
solution n [a, b]
    | a > b = [n + b - a]
    | otherwise = [b - a]
solution n xs = (solution n [xs!!0, xs!!1]) ++ (solution n $ tail xs)

main :: IO ()
main = do
    line1 <- getLine
    line2 <- getLine
    let n = head $ map (\x -> read x :: Int64) $ words line1
        args = map (\x -> read x :: Int64) $ words line2
    print $ sum $ solution n (1:args)

main = do
  [n, m] <- fmap (map read . words) getLine
  xs <- fmap (map read . words) getLine

  let
    a = 1:xs
    s = sum $ zipWith (\x y -> if x <= y then y-x else n-(x-y)) a (tail a)
  print s

{-# LANGUAGE MultiParamTypeClasses,FlexibleContexts,FlexibleInstances,TypeSynonymInstances,BangPatterns,RankNTypes #-}

import Control.Monad
import Control.Monad.ST
import Control.Applicative
import Control.Arrow
import Debug.Trace

import Data.List
import Data.Int
import Data.Maybe
import Data.Array.Unboxed
import Data.Array.ST
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.ByteString.Char8 as B

readInt = B.readInt >>> fmap fst >>> fromMaybe 0
readLine :: Read a => IO a
readLine = read <$> getLine
readsLine :: Read a => IO [a]
readsLine = map read . words <$> getLine

main = do
    n:m:l <- map readInt . B.words <$> B.getContents
    print $ solve n m l

solve :: Int -> Int -> [Int] -> Integer
solve n m l = sum $ zipWith f (1:l) l
    where
    f a b = fromIntegral $ (b + n - a) `mod` n

import Control.Applicative

main :: IO ()
main = do
        [n,_] <- (map read . words) <$> getLine
        ts <- (map read . words) <$> getLine
        print . snd $ foldl (h n) (1,0) ts

-- |
-- >>> length $ g (f 4) [3,2,3]
--6
-- >>> length $ g (f 4) [2,3,3]
--2
--
---- prop> \n xs -> length (g (f n) xs) == foldl (h n) 0 xs

f :: Integer -> [Integer]
f n = cycle [1..n]

g :: [Integer] -> [Integer] -> [Integer]
g _ [] = []
g (x:xs) (t:ts) | x == t = g (x:xs) ts
                | otherwise = x : g xs (t:ts)
g _ _ = []

-- |
-- >>> snd $ foldl (h 4) (1,0) [3,2,3]
--6
-- >>> snd $ foldl (h 4) (1,0) [2,3,3]
--2
h :: Integer -> (Integer,Integer) -> Integer -> (Integer,Integer)
h n (c,r) t | t < c = (t,r + n - c + t)
            | otherwise = (t,r + t - c)

import Data.List
import Data.Maybe
import Data.Char
main=   readsolveprint
readsolveprint::IO()
readsolveprint =  interact $ solve. (\(a:b:bs)->a:1:bs).concat.map (map read.words).take 2.lines
solve :: [Integer]->String
solve xs = show $ fst $ slv1 xs
slv1 (x:xs)=foldr f (0,0) xs where
  f a (0,0) = (0,a)
  f a (b1,b2) = if b2-a>=0 then (b1+b2-a,a) else (b1+b2+x-a,a)

import Control.Monad (liftM)
import Data.List (foldl')
import Prelude hiding (reads)

reads :: Read a => IO [a]
reads  =  liftM (map read . words) getLine

solve :: Integer -> [Integer] -> Integer
solve n as  =  snd $ foldl' solve' (1, 0) as
  where
  	solve' (m, s) a
  	  | a >= m  =  (a, s + a - m)
  	  | a <  m  =  (a, s + (n - m + a))

main :: IO ()
main  =  do
	[n, m]  <-  reads
	as  <-  reads
	print $ solve n as

import Control.Applicative
import Data.Char
import qualified Data.ByteString.Lazy.Char8 as B

readInt :: B.ByteString -> Integer
readInt = B.foldl' (\x c -> 10 * x + fromIntegral (ord c) - 48) 0

main = do
  [n,m] <- map read . words <$> getLine :: IO [Integer]
  ls <- (1:) . map readInt . B.words <$> B.getContents
  print . sum . map ((`mod`n) . uncurry (flip (-))) $ zip ls (drop 1 ls)

import Data.List (sort)
import Control.Applicative
import Data.Int (Int64)

main = do
  [n,m] <- map read . words <$> getLine :: IO [Int64]
  ls <- (1:) . map read . words <$> getLine
  print . sum . map ((`mod`n) . uncurry (flip (-))) $ zip ls (drop 1 ls)

import Data.List (sort)
import Control.Applicative

main = do
  [n,m] <- map read . words <$> getLine :: IO [Integer]
  ls <- (1:) . map read . words <$> getLine
  let m = map ((`mod`n) . uncurry (flip (-))) $ zip ls (drop 1 ls)
  print . sum $ m

import Data.List (sort)
import Control.Applicative

main = do
  [n,m] <- map read . words <$> getLine :: IO [Integer]
  ls <- (1:) . map read . words <$> getLine
  print . sum . map ((`mod`n) . uncurry (flip (-))) $ zip ls (drop 1 ls)

import Data.List

solve n xs = fst $ foldl' moveToHouse (0, 1) xs
  where moveToHouse (time, position) destination
          | position <= destination = (time + destination - position,
                                       destination)
          | position > destination = (time + n - position + destination,
                                      destination)

main = do [n, _] <- fmap (map read . words) getLine
          xs <- fmap (map read . words) getLine
          putStrLn $ show $ solve n xs

import Data.List

solve n xs = fst $ foldl moveToHouse (0, 1) xs
  where moveToHouse (time, position) destination
          | position <= destination = (time + destination - position,
                                       destination)
          | position > destination = (time + n - position + destination,
                                      destination)

main = do [n, _] <- fmap (map read . words) getLine
          xs <- fmap (map read . words) getLine
          putStrLn $ show $ solve n xs

import Control.Monad
import Data.Array
import Data.Char
import Data.Functor
import Data.Int
import Data.List
import Data.List.Split
import Data.Maybe
-- import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
getList :: Read a => IO [a]
getList = map (read . B.unpack) . B.words <$> B.getLine
getIntList :: IO [Int]
getIntList = unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine

dist :: Int -> Int -> Int -> Int
dist n a b = if b >= a then b - a else n + b - a

solve :: Int -> [Int] -> Int64
solve n a = sum $ map fromIntegral $ zipWith (dist n) (1:a) a

main :: IO ()
main = do
    [n, _] <- getIntList
    a <- getIntList
    print $ solve n a

import Control.Applicative
import Data.List
import Data.Char

process [] _ _ cou = cou
process (a:as) n cu cou |  a>=cu = process as n a (cou + a-cu)
                        |  otherwise = process as n a (cou +n-cu+a )

main=do

   [n,m]<- map read <$> words <$> getLine ::IO [Integer]
   a<- map read <$> words <$> getLine ::IO [Integer]

   print $  process a n 1 0

main = do
  [n,m] <- fmap (map read . words) getLine
  as <- fmap (map read . words) getLine
  let d a b = (b-a) `mod` n
  print . sum $ zipWith d (1:as) as

-- Vicfred
-- https://codeforces.com/problemset/problem/339/B

import Data.Int

dist :: Int -> Int -> Int -> Int
dist n a b = if b >= a then b - a else n + b - a

solve :: Int -> [Int] -> Int64
solve n a = sum $ map fromIntegral $ zipWith (dist n) (1:a) a

main :: IO ()
main = do
    [n, _] <- map read . words <$> getLine :: IO [Int]
    a <- map read . words <$> getLine :: IO [Int]
    print $ solve n a

module Main where
import Data.List
import Data.Char
import Data.Maybe
import System.Environment
import qualified Data.ByteString.Char8 as C

main = do
	params <- readInts
	a <- readInts
	print $ gao (map fromIntegral a) (fromIntegral (params!!0))

--	let a = take 100000 [1, 2, 1 ..]
--		in print $ gao (map fromIntegral a) (fromIntegral (params!!0))

gao :: [Integer] -> Integer -> Integer
gao xs n = ((fromIntegral $ length (groupBy' (<=) xs)) - 1) * n + last xs - 1

readInts ::IO [Int]
readInts = fmap (map $ fst . fromJust . C.readInt) (fmap C.words C.getLine)

groupBy'                 :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy' _  []           = []
groupBy' _	(x:[])		 = [[x]]
groupBy' eq (x:y:zs)     | eq x y = (x:(head tmp)) : (tail tmp)
						 | otherwise = (x:[]):tmp
						 where tmp = (groupBy' eq (y:zs))

main = interact $ show . path . map read . words

path (n:_:ls) = fst $ foldl (\(s,p) e -> (s + diff n p e, e)) (0,1) ls

diff n p e = if e<p then n+e-p else e-p

-- @betaveros :: vim:set fdm=marker:
{-# LANGUAGE LambdaCase, NPlusKPatterns, TupleSections #-}
{-# OPTIONS_GHC -fno-warn-unused-imports -fno-warn-missing-signatures #-}
-- import ALL the things! {{{
-- hiding clauses are to allow Data.Foldable's generalizations
import Prelude hiding (mapM, mapM_, sequence, sequence_, foldl, foldl1, foldr, foldr1, and, or, any, all, sum, product, concat, concatMap, maximum, minimum, elem, notElem)
import Control.Applicative
import Control.Arrow
import Control.Exception
import Control.Monad hiding (mapM, mapM_, forM, forM_, sequence, sequence_, msum)
import Control.Monad.ST

import qualified Data.ByteString.Char8 as BS
import Data.ByteString.Char8 (ByteString)
import Data.Bits
import Data.Char
import Data.Either
import Data.Foldable
import Data.Function
import Data.IORef
import Data.List hiding (foldl, foldl', foldl1, foldl1', foldr, foldr1, concat, concatMap, and, or, any, all, sum, product, maximum, minimum, elem, notElem, find)
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.STRef
import Data.String
import Data.Traversable
import Data.Tuple

import qualified Data.Map as Map
import Data.Map (Map)
import qualified Data.Set as Set
import Data.Set (Set)
import qualified Data.Sequence as Seq
import Data.Sequence (Seq, (<|), (|>), (><))

import Debug.Trace
import Text.Printf
import System.IO
-- }}}
-- silly utilities {{{
-- stolen from lens:
a & f = f a
a <&> f = fmap f a
infixl 1 &, <&>

fi :: (Integral a, Num b) => a -> b
fi = fromIntegral
glength :: (Num b) => [a] -> b
glength = genericLength

readInt     = read :: String -> Int
readInteger = read :: String -> Integer
-- (!?) :: (Ord k) => Map k v -> k -> Maybe v
-- (!?) = flip Map.lookup
histogram :: (Ord a, Num b) => [a] -> Map a b
histogram = Map.fromListWith (+) . map (,1)
-- }}}
-- input and output {{{
bsGetLine :: IO ByteString
bsGetLine = fst . BS.spanEnd isSpace <$> BS.getLine

inputInt     = (read <$> getLine) :: IO Int
inputInteger = (read <$> getLine) :: IO Integer
inputDouble  = (read <$> getLine) :: IO Double

inputRow :: (Read a) => IO [a]
inputRow = map read . words <$> getLine
inputInts     = inputRow :: IO [Int]
inputIntegers = inputRow :: IO [Integer]
inputDoubles  = inputRow :: IO [Double]

ssUnwords :: [ShowS] -> ShowS
ssUnwords [] = id
ssUnwords (x:xs) = x . (' ':) . ssUnwords xs

ssUnlines :: [ShowS] -> ShowS
ssUnlines [] = id
ssUnlines (x:xs) = x . ('\n':) . ssUnlines xs

showMany :: (Show a) => [a] -> String
showMany xs = ssUnwords (map shows xs) ""
showMatrix :: (Show a) => [[a]] -> String
showMatrix xs = ssUnlines (map (ssUnwords . map shows) xs) ""

printMany :: (Show a) => [a] -> IO ()
printMany xs = putStrLn (showMany xs)
printMatrix :: (Show a) => [[a]] -> IO ()
printMatrix xs = putStr (showMatrix xs)
-- }}}

til :: Integer -> Integer -> Integer -> Integer
til n x1 x2 = if x2 >= x1 then x2 - x1 else x2 + n - x1

main :: IO ()
main = do
	[n, _] <- inputIntegers
	xs <- inputIntegers
	print . sum $ zipWith (til n) (1 : xs) xs

import Control.Applicative
import Data.List
import qualified Data.ByteString.Char8 as C
import Data.Maybe

main=do
	[n,m]<- map read. words <$>  getLine  ::IO [Integer]
	s<- map (fst.fromJust.C.readInteger). C.words <$>  C.getLine  ::IO [Integer]
	print $   sum $ zipWith (\a b-> mod (a-b) n) s (1:s)

import Data.List
import qualified Data.Map as Map
import qualified Data.Set as Set
import Data.Char
import Data.Ratio

main :: IO ()
main = do
	line1 <- getLine
	line2 <- getLine
	let
		[n, m] = map (\x->read x::Integer). words $ line1
		arr = map (\x->x-1). map (\x->read x::Integer). words $ line2
		solve _ [] = 0
		solve p (x:xs) = ((x-p)`mod`n + n)`mod`n + (solve x xs)
	print$ solve 0 arr

process :: [Integer] -> Integer -> Integer
process xs n = sum $ zipWith f xs' (0:xs')
    where
        f x y = (x - y) `mod` n
        xs' = map (subtract 1) xs

readInt :: String -> Integer
readInt = read

main :: IO ()
main = do
    [n,m] <- fmap (map readInt.words) getLine
    as <- fmap (map readInt.words) getLine
    print $ process as n

module Main where
import Data.List (foldl')

main :: IO ()
main = do
  n:_ <- fmap (map read . words) getLine
  ts <- fmap (map read . words) getLine
  print $ rush n ts

rush :: Integer -> [Integer] -> Integer
rush n = snd . foldl' (\(b,acc) t -> (t, (if t >= b then t - b else n + t - b) + acc)) (1, 0)

import Data.Int

time :: Int64 -> [Int64] -> Int64
time n xs = sum $ zipWith d (1:xs) xs
    where d a b = (b - a) `mod` n

solve :: String -> String
solve s = show $ time  n  xs
    where l:ls = lines s
          n:_ = fmap read.words $ l
          xs = fmap read.words.head $ ls

main :: IO ()
main = interact $ solve

import Data.Int

f :: Int64 -> (Int64, Int64) -> Int64 -> (Int64, Int64)
f n (currentHouse, elapsedTime) nextHouse = (nextHouse, elapsedTime + newTime)
    where newTime = (nextHouse + n - currentHouse) `rem` n

solve :: String -> String
solve s = show $ snd $ foldl (f n) (1, 0)  xs
    where l:ls = lines s
          n:_ = fmap read.words $ l
          xs = fmap read.words.head $ ls

main :: IO ()
main = interact $ solve

import Data.List

solve :: [Int] -> Int
solve (n:_:xs) = fst $ foldl' moveToHouse (0, 1) xs
  where moveToHouse (time, position) destination
          | position <= destination = (time + destination - position,
                                       destination)
          | position > destination = (time + n - position + destination,
                                      destination)

main = interact $ show . solve . map read . words

import Control.Monad
import Data.Array
import Data.Char
import Data.Functor
import Data.Int
import Data.List
import Data.List.Split
import Data.Maybe
-- import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
getList :: Read a => IO [a]
getList = map (read . B.unpack) . B.words <$> B.getLine
getIntList :: IO [Int]
getIntList = unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine

dist :: Int -> Int -> Int -> Int
dist n a b = if b >= a then b - a else n + b - a

solve :: Int -> [Int] -> Int
solve n a = sum $ zipWith (dist n) (1:a) a

main :: IO ()
main = do
    [n, _] <- getIntList
    a <- getIntList
    print $ solve n a

import Control.Applicative
import Data.List
import Data.Char

process [] _ _ cou = cou
process (a:as) n cu cou |  a>=cu = process as n a (cou + a-cu)
                        |  otherwise = process as n a (cou +n-cu+a )

main=do

   [n,m]<- map read <$> words <$> getLine ::IO [Int]
   a<- map read <$> words <$> getLine ::IO [Int]

   print $  process a n 1 0

module Main where
import Data.List
import Data.Char
import Data.Maybe
import System.Environment
import qualified Data.ByteString.Char8 as C

main = do
	params <- readInts
	a <- readInts
	print $ gao a (params!!0)

gao :: [Int] -> Int -> Int
gao xs n = (length (groupBy (<) xs) - 1) * n + last xs - 1

readInts ::IO [Int]
readInts = fmap (map $ fst . fromJust . C.readInt) (fmap C.words C.getLine)

module Main where
import Data.List
import Data.Char
import Data.Maybe
import System.Environment
import qualified Data.ByteString.Char8 as C

main = do
	params <- readInts
	a <- readInts
	print $ gao (map fromIntegral a) (fromIntegral (params!!0))

gao :: [Integer] -> Integer -> Integer
gao xs n = ((fromIntegral $ length (groupBy (<=) xs)) - 1) * n + last xs - 1

readInts ::IO [Int]
readInts = fmap (map $ fst . fromJust . C.readInt) (fmap C.words C.getLine)

module Main where
import Data.List
import Data.Char
import Data.Maybe
import System.Environment
import qualified Data.ByteString.Char8 as C

main = do
	params <- readInts
	a <- readInts
	print $ gao a (params!!0)

gao :: [Int] -> Int -> Int
gao xs n = (length (groupBy (<=) xs) - 1) * n + last xs - 1

readInts ::IO [Int]
readInts = fmap (map $ fst . fromJust . C.readInt) (fmap C.words C.getLine)

import Control.Applicative
import Data.List

main=do
	[n,m]<- map read. words <$>  getLine  ::IO [Int]
	s<- map read. words <$>  getLine  ::IO [Int]
	print $   sum $ zipWith (\a b-> mod (a-b) n) s (1:s)

process :: [Int] -> Int -> Int
process xs n = sum $ zipWith f xs' (0:xs')
    where
        f x y = (x - y) `mod` n
        xs' = map (subtract 1) xs

readInt :: String -> Int
readInt = read

main :: IO ()
main = do
    [n,m] <- fmap (map readInt.words) getLine
    as <- fmap (map readInt.words) getLine
    print $ process as n

f :: Int -> (Int, Int) -> Int -> (Int, Int)
f n (currentHouse, elapsedTime) nextHouse = (nextHouse, elapsedTime + newTime)
    where newTime = (nextHouse + n - currentHouse) `rem` n

solve :: String -> String
solve s = show $ snd $ foldl (f n) (1, 0)  xs
    where l:ls = lines s
          n:_ = fmap read.words $ l
          xs = fmap read.words.head $ ls

main :: IO ()
main = interact $ solve

main = do
	s<-getLine
	rest<-getLine
	putStrLn $ show $ solve (read (head (words s))) (map read (words rest)) 1
solve::Int->[Int]->Int->Int
solve n [] c = 0
solve n w c = travel n c (head w) + solve n (tail w) (head w)

travel n a b | b >= a = (b-a)
             | otherwise = n-a+b

solution :: Int -> [Int] -> [Int]
solution n [] = []
solution n [k] = []
solution n [a, b]
    | a > b = [n + b - a]
    | otherwise = [b - a]
solution n xs = (solution n [xs!!0, xs!!1]) ++ (solution n $ tail xs)

main :: IO ()
main = do
    line1 <- getLine
    line2 <- getLine
    let n = head $ map (\x -> read x :: Int) $ words line1
        args = map (\x -> read x :: Int) $ words line2
    print $ sum $ solution n (1:args)

import Control.Applicative

main :: IO ()
main = do
        [n,_] <- (map read . words) <$> getLine
        ts <- (map read . words) <$> getLine
        print . snd $ foldl (h n) (1,0) ts

-- |
-- >>> length $ g (f 4) [3,2,3]
--6
-- >>> length $ g (f 4) [2,3,3]
--2
--
---- prop> \n xs -> length (g (f n) xs) == foldl (h n) 0 xs

f :: Int -> [Int]
f n = cycle [1..n]

g :: [Int] -> [Int] -> [Int]
g _ [] = []
g (x:xs) (t:ts) | x == t = g (x:xs) ts
                | otherwise = x : g xs (t:ts)
g _ _ = []

-- |
-- >>> snd $ foldl (h 4) (1,0) [3,2,3]
--6
-- >>> snd $ foldl (h 4) (1,0) [2,3,3]
--2
h :: Int -> (Int,Int) -> Int -> (Int,Int)
h n (c,r) t | t < c = (t,r + n - c + t)
            | otherwise = (t,r + t - c)

import Data.List (sort)
import Control.Applicative

main = do
  [n,m] <- map read . words <$> getLine :: IO [Int]
  ls <- (1:) . map read . words <$> getLine
  let m = map ((`mod`n) . uncurry (flip (-))) $ zip ls (drop 1 ls)
  print m
  print . sum $ m

import Data.List (sort)
import Control.Applicative

main = do
  [n,m] <- map read . words <$> getLine :: IO [Int]
  ls <- (1:) . map read . words <$> getLine
  print . sum . map ((`mod`4) . uncurry (flip (-))) $ zip ls (drop 1 ls)

import Data.List (sort)
import Control.Applicative
import Data.Word

main = do
  [n,m] <- map read . words <$> getLine :: IO [Word64]
  ls <- (1:) . map read . words <$> getLine
  print . sum . map ((`mod`n) . uncurry (flip (-))) $ zip ls (drop 1 ls)

import Data.List (sort)
import Control.Applicative

main = do
  [n,m] <- map read . words <$> getLine :: IO [Int]
  ls <- (1:) . map read . words <$> getLine
  let m = map ((`mod`n) . uncurry (flip (-))) $ zip ls (drop 1 ls)
  print . sum $ m


import Text.ParserCombinators.ReadP
import List

main = interact $ unwords.map show.solve.concat.lines

solve :: String -> [Int]
solve = sort.fst.head.readP_to_S table

table :: ReadP [Int]
table = do
      { string "
"
      ; xss <- manyTill row (string "
")
      ; return $ ((length $ concat xss):) $ concat $ filter(not.null) $ concat xss
      }

row :: ReadP [[Int]]
row = do
      { string "
"
      ; xs <- manyTill cell (string "
")
      ; return xs
      }

cell :: ReadP [Int]
cell = do
      { string ""
      ; xs <- option [] table
      ; string "	"
      ; return xs
      }

import Control.Monad
import Data.List

main = do
    s <- getContents >>= return . concat . lines
    mapM (\x -> putStr (show x ++ " ")) $ solve s
    putStrLn ""

solve s = sort $ count $ preproc s

preproc s = foldl replaceStr s [("
","["),("
", "]"),("
", ""),("
", ""),("", "."),("	", "")]

replaceStr [] _ = []
replaceStr xs@(y:ys) p@(from, to) = case stripPrefix from xs of
    Nothing  -> y:replaceStr ys p
    Just ys' -> to ++ replaceStr ys' p

count s = count' s [] [] where
    count' []     _  acc = acc
    count' (s:ss) st acc = case s of
        '[' -> count' ss (0:st) acc
        '.' -> count' ss ((head st + 1):(tail st)) acc
        ']' -> count' ss (tail st) ((head st):acc)

import List
main = interact solve
solve input = output where
  input0 = concat $ lines input
  output = unwords $ map show $ sort $ map count $ divide (translate input0) []
translate [] = []
translate xs
  | take 7 xs == "
"  = 'a':(translate $ drop 7 xs)
  | take 8 xs == "
" = 'b':(translate $ drop 8 xs)
  | take 4 xs == "
"     = 'c':(translate $ drop 4 xs)
  | take 5 xs == "
"    = 'd':(translate $ drop 5 xs)
  | take 4 xs == ""     = 'e':(translate $ drop 4 xs)
  | take 5 xs == "	"    = 'f':(translate $ drop 5 xs)
divide [] _ = []
divide (x:xs) ys
  | x=='a'    = divide xs ([]:ys)
  | x=='b'    = y:(divide xs ys')
  | otherwise = divide xs ((x:y):ys')
   where y = head ys
         ys' = tail ys
count xs = length $ filter (=='e') xs

import Text.ParserCombinators.ReadP
import List

main = interact $ unwords.map show.solve.concat.lines

solve :: String -> [Int]
solve = fst.head.readP_to_S table

table :: ReadP [Int]
table = do
      { string "
"
      ; xss <- manyTill row (string "
")
      ; return $ ((length $ concat xss):) $ concat $ filter(not.null) $ concat xss
      }

row :: ReadP [[Int]]
row = do
      { string "
"
      ; xs <- manyTill cell (string "
")
      ; return xs
      }

cell :: ReadP [Int]
cell = do
      { string ""
      ; xs <- option [] table
      ; string "	"
      ; return xs
      }

import Control.Monad
import Data.List

main = do
    s <- getContents >>= return . concat . lines
    mapM (\x -> putStr (show x ++ " ")) $ solve s
    putStrLn ""

solve s = count $ preproc s

preproc s = foldl replaceStr s [("
","["),("
", "]"),("
", ""),("
", ""),("", "."),("	", "")]

replaceStr [] _ = []
replaceStr xs@(y:ys) p@(from, to) = case stripPrefix from xs of
    Nothing  -> y:replaceStr ys p
    Just ys' -> to ++ replaceStr ys' p

count s = count' s [] [] where
    count' []     _  acc = acc
    count' (s:ss) st acc = case s of
        '[' -> count' ss (0:st) acc
        '.' -> count' ss ((head st + 1):(tail st)) acc
        ']' -> count' ss (tail st) ((head st):acc)


{-# LANGUAGE BangPatterns #-}

import qualified Data.ByteString.Char8 as C
import Control.Monad
import Data.Maybe

obtainable :: [Int] -> Bool
obtainable = go . reverse where
  go [0] = True
  go [_] = False
  go (0:xs) = go xs
  go (x1:x2:xs)
    | x1 > 0 = False
    | (-x1) == x2 && not (null xs) = False
    | otherwise = go (x2+x1:xs)

main = do
  [!t] <- readInts
  replicateM_ t $ do
    readInts
    as <- readInts
    putStrLn (if obtainable as then "Yes" else "No")

readInts :: IO [Int]
readInts = map (fst . fromJust . C.readInt) . C.words <$> C.getLine

module Main where

import Control.Monad (liftM2, replicateM_)
import Data.Bits
-- import Debug.Trace
import Data.Char (digitToInt)
import Data.Maybe (fromJust)
import Data.String
import qualified Data.ByteString.Char8 as C
import Data.List (dropWhileEnd)

solve :: [Integer] -> Bool
solve xs = all (==0) xs || all (<0) (tail ys) && head ys == 0
  where ys = scanr1 (+) $ dropWhileEnd (==0) xs

parseInteger :: String -> Integer
parseInteger = fromIntegral . fst . fromJust . C.readInt . fromString

parseIntegerList :: String -> [Integer]
parseIntegerList = map parseInteger . words

printAns :: Bool -> IO ()
printAns True = putStrLn "YES"
printAns False = putStrLn "NO"

main :: IO ()
main = do
  n <- parseInteger <$> getLine
  replicateM_ (fromIntegral n) (printAns <$> solve =<< (parseIntegerList <$> (getLine >> getLine)))

--
-- Michael V. Antosha
-- 2022
-- Michael.Antosha@gmail.com
--

{-# language Safe                      #-}
{-# language ImportQualifiedPost       #-}
{-# language NoMonomorphismRestriction #-}

{-# options_ghc -O3                              #-}

{-# options_ghc -Weverything                     #-}
{-# options_ghc -Werror                          #-}

{-# options_ghc -Wno-missing-import-lists        #-}
{-# options_ghc -Wno-missing-local-signatures    #-}

import Prelude               hiding (getLine)
import Control.Arrow
import Control.Monad
import Data.ByteString.Char8 qualified as B
import Data.Char
import Data.List             qualified as L
import Data.Maybe

ri = readInts <$> B.getLine :: IO [Int]
  where
    readInts  = L.unfoldr ((second dropSpace <$>) . B.readInt) . dropSpace
    dropSpace = B.dropWhile isSpace

main :: IO ()
main = do
    [t] <- ri
    tcs <- replicateM t $ do
        [[_n],xs] <- replicateM 2 ri
        return xs
    mapM_ (putStrLn.showYN.solve) tcs

showYN False = "NO"
showYN True  = "YES"

solve xs = not (headFail||negPSumFail||finSumFail||splitFail)
  where
    psums = scanl1 (+) xs
    psumsNozeros = reverse . dropWhile (==0) . reverse $ psums
    headFail = (not.null) psumsNozeros && head psumsNozeros <= 0
    negPSumFail = isJust . L.find (<0) $ psumsNozeros
    finSumFail = (last psums /= 0)
    splitFail = isJust . L.find (==0) $ psumsNozeros

module Main where

import Control.Monad (liftM2, replicateM_)
import Data.Bits
-- import Debug.Trace
import Data.Char (digitToInt)
import Data.Maybe (fromJust)
import Data.String ( IsString(fromString) )
import qualified Data.ByteString.Char8 as C
import Data.List (dropWhileEnd)

-- solve :: [Integer] -> Bool
-- solve xs = null ys || all (<0) (tail ys) && head ys == 0
--   where ys = scanr1 (+) $ dropWhileEnd (==0) xs

solve :: [Integer] -> Bool
solve xs = null ys || all (>0) (init ys) && last ys == 0
  where ys = scanl1 (+) $ dropWhileEnd (==0) xs

-- solve :: [Integer] -> Bool
-- solve [] = True
-- solve [0] = True
-- solve [_] = False
-- solve (x:y:xs)
--   | z == 0 = all (==0) (xs)
--   | z > 0 = solve (z:xs)
--   | otherwise = False
--   where z = x + y

parseInteger :: String -> Integer
parseInteger = fromIntegral . fst . fromJust . C.readInt . fromString

parseIntegerList :: String -> [Integer]
parseIntegerList = map parseInteger . words

printAns :: Bool -> IO ()
printAns True = putStrLn "YES"
printAns False = putStrLn "NO"

main :: IO ()
main = do
  n <- parseInteger <$> getLine
  replicateM_ (fromIntegral n) (printAns <$> solve =<< (parseIntegerList <$> (getLine >> getLine)))

module Main where

import Control.Monad (liftM2, replicateM_)
import Data.Bits
-- import Debug.Trace
import Data.Char (digitToInt)
import Data.Maybe (fromJust)
import Data.String ( IsString(fromString) )
import qualified Data.ByteString.Char8 as C
import Data.List (dropWhileEnd)

-- solve :: [Integer] -> Bool
-- solve xs = null ys || all (<0) (tail ys) && head ys == 0
--   where ys = scanr1 (+) $ dropWhileEnd (==0) xs

solve :: [Integer] -> Bool
solve xs = null ys || all (>0) (init ys) && last ys == 0
  where ys = scanl1 (+) xs

-- solve :: [Integer] -> Bool
-- solve [] = True
-- solve [x] = x == 0
-- solve (x:y:xs)
--   | z == 0 = all (==0) xs
--   | z > 0 = solve (z:xs)
--   | otherwise = False
--   where z = x + y

parseInteger :: String -> Integer
parseInteger = fromIntegral . fst . fromJust . C.readInt . fromString

parseIntegerList :: String -> [Integer]
parseIntegerList = map parseInteger . words

printAns :: Bool -> IO ()
printAns True = putStrLn "YES"
printAns False = putStrLn "NO"

main :: IO ()
main = do
  n <- parseInteger <$> getLine
  replicateM_ (fromIntegral n) (printAns <$> solve =<< (parseIntegerList <$> (getLine >> getLine)))

module Main where

import Control.Monad (liftM2, replicateM_)
import Data.Bits
-- import Debug.Trace
import Data.Char (digitToInt)
import Data.Maybe (fromJust)
import Data.String ( IsString(fromString) )
import qualified Data.ByteString.Char8 as C
import Data.List (dropWhileEnd)

-- solve :: [Integer] -> Bool
-- solve xs = null ys || all (<0) (tail ys) && head ys == 0
--   where ys = scanr1 (+) $ dropWhileEnd (==0) xs

-- solve :: [Integer] -> Bool
-- solve xs = null ys || all (>0) (init ys) && last ys == 0
--   where ys = scanl1 (+) $ dropWhileEnd (==0) xs

solve :: [Integer] -> Bool
solve [] = True
solve [x] = x == 0
solve (x:y:xs)
  | z == 0 = all (==0) xs
  | z > 0 = solve (z:xs)
  | otherwise = False
  where z = x + y

parseInteger :: String -> Integer
parseInteger = fromIntegral . fst . fromJust . C.readInt . fromString

parseIntegerList :: String -> [Integer]
parseIntegerList = map parseInteger . words

printAns :: Bool -> IO ()
printAns True = putStrLn "YES"
printAns False = putStrLn "NO"

main :: IO ()
main = do
  n <- parseInteger <$> getLine
  replicateM_ (fromIntegral n) (printAns <$> solve =<< (parseIntegerList <$> (getLine >> getLine)))

module Main where

import Control.Monad (liftM2, replicateM_)
import Data.Bits
-- import Debug.Trace
import Data.Char (digitToInt)
import Data.Maybe (fromJust)
import Data.String ( IsString(fromString) )
import qualified Data.ByteString.Char8 as C
import Data.List (dropWhileEnd)

-- solve :: [Integer] -> Bool
-- solve xs = null ys || all (<0) (tail ys) && head ys == 0
--   where ys = scanr1 (+) $ dropWhileEnd (==0) xs

solve :: [Integer] -> Bool
solve xs = null ys || all (>0) (tail ys) && last ys == 0
  where ys = scanl1 (+) $ dropWhileEnd (==0) xs

-- solve :: [Integer] -> Bool
-- solve [] = True
-- solve [0] = True
-- solve [_] = False
-- solve (x:y:xs)
--   | z == 0 = all (==0) (xs)
--   | z > 0 = solve (z:xs)
--   | otherwise = False
--   where z = x + y

parseInteger :: String -> Integer
parseInteger = fromIntegral . fst . fromJust . C.readInt . fromString

parseIntegerList :: String -> [Integer]
parseIntegerList = map parseInteger . words

printAns :: Bool -> IO ()
printAns True = putStrLn "YES"
printAns False = putStrLn "NO"

main :: IO ()
main = do
  n <- parseInteger <$> getLine
  replicateM_ (fromIntegral n) (printAns <$> solve =<< (parseIntegerList <$> (getLine >> getLine)))

module Main where

import Control.Monad (liftM2, replicateM_)
import Data.Bits
-- import Debug.Trace
import Data.Char (digitToInt)
import Data.Maybe (fromJust)
import Data.String ( IsString(fromString) )
import qualified Data.ByteString.Char8 as C
import Data.List (dropWhileEnd)

-- solve :: [Integer] -> Bool
-- solve xs = null ys || all (<0) (tail ys) && head ys == 0
--   where ys = scanr1 (+) $ dropWhileEnd (==0) xs

solve :: [Integer] -> Bool
solve [] = True
solve [0] = True
solve [_] = False
solve (x:y:xs)
  | z == 0 = all (==0) (xs)
  | z > 0 = solve (z:xs)
  | otherwise = False
  where z = x + y

parseInteger :: String -> Integer
parseInteger = fromIntegral . fst . fromJust . C.readInt . fromString

parseIntegerList :: String -> [Integer]
parseIntegerList = map parseInteger . words

printAns :: Bool -> IO ()
printAns True = putStrLn "YES"
printAns False = putStrLn "NO"

main :: IO ()
main = do
  n <- parseInteger <$> getLine
  replicateM_ (fromIntegral n) (printAns <$> solve =<< (parseIntegerList <$> (getLine >> getLine)))

module Main where

import Control.Monad (liftM2, replicateM_)
import Data.Bits
-- import Debug.Trace
import Data.Char (digitToInt)
import Data.Maybe (fromJust)
import Data.String
import qualified Data.ByteString.Char8 as C

solve :: [Integer] -> Bool
solve xs = all (==0) xs || all (<0) (tail ys) && head ys == 0
  where ys = scanr1 (+) xs

parseInteger :: String -> Integer
parseInteger = fromIntegral . fst . fromJust . C.readInt . fromString

parseIntegerList :: String -> [Integer]
parseIntegerList = map parseInteger . words

printAns :: Bool -> IO ()
printAns True = putStrLn "YES"
printAns False = putStrLn "NO"

main :: IO ()
main = do
  n <- parseInteger <$> getLine
  replicateM_ (fromIntegral n) (printAns <$> solve =<< (parseIntegerList <$> (getLine >> getLine)))

module Main where

import Control.Monad (liftM2, replicateM_)
import Data.Bits
-- import Debug.Trace
import Data.Char (digitToInt)
import Data.Maybe (fromJust)
import Data.String
import qualified Data.ByteString.Char8 as C

solve :: [Integer] -> Bool
solve [] = False
solve [0] = True
solve [_] = False
solve (x:y:xs)
  | x == 0 = all (==0) (y:xs)
  | z >= 0 = solve (z:xs)
  | otherwise = False
  where z = x + y

parseInteger :: String -> Integer
parseInteger = fromIntegral . fst . fromJust . C.readInt . fromString

parseIntegerList :: String -> [Integer]
parseIntegerList = map parseInteger . words

printAns :: Bool -> IO ()
printAns True = putStrLn "YES"
printAns False = putStrLn "NO"

main :: IO ()
main = do
  n <- parseInteger <$> getLine
  replicateM_ (fromIntegral n) (printAns <$> solve =<< (parseIntegerList <$> (getLine >> getLine)))

module Main where

import Control.Monad (liftM2, replicateM_)
import Data.Bits
-- import Debug.Trace
import Data.Char (digitToInt)
import Data.Maybe (fromJust)
import Data.String
import qualified Data.ByteString.Char8 as C

solve :: [Integer] -> Bool
solve [] = False
solve [0] = True
solve [_] = False
solve (x:y:xs)
  | z == 0 = all (==0) xs
  | z > 0 = solve (z:xs)
  | otherwise = False
  where z = x + y

parseInteger :: String -> Integer
parseInteger = fromIntegral . fst . fromJust . C.readInt . fromString

parseIntegerList :: String -> [Integer]
parseIntegerList = map parseInteger . words

printAns :: Bool -> IO ()
printAns True = putStrLn "YES"
printAns False = putStrLn "NO"

main :: IO ()
main = do
  n <- parseInteger <$> getLine
  replicateM_ (fromIntegral n) (printAns <$> solve =<< (parseIntegerList <$> (getLine >> getLine)))

module Main where

import Control.Monad (liftM2, replicateM_)
import Data.Bits
-- import Debug.Trace
import Data.Char (digitToInt)
import Data.Maybe (fromJust)
import Data.String
import qualified Data.ByteString.Char8 as C

solve :: [Integer] -> Bool
solve [] = False
solve [0] = True
solve [_] = False
solve (x:y:xs)
  | x + y == 0 = all (==0) xs
  | x + y > 0 = solve (x+y : xs)
  | otherwise = False

parseInteger :: String -> Integer
parseInteger = fromIntegral . fst . fromJust . C.readInt . fromString

parseIntegerList :: String -> [Integer]
parseIntegerList = map parseInteger . words

printAns :: Bool -> IO ()
printAns True = putStrLn "YES"
printAns False = putStrLn "NO"

main :: IO ()
main = do
  n <- parseInteger <$> getLine
  replicateM_ (fromIntegral n) (printAns <$> solve =<< (parseIntegerList <$> (getLine >> getLine)))

module Main where

import Control.Monad (liftM2, replicateM_)
import Data.Bits
-- import Debug.Trace
import Data.Char (digitToInt)
import Data.Maybe (fromJust)
import Data.String
import qualified Data.ByteString.Char8 as C

solve :: [Integer] -> Bool
solve [] = True
solve [0] = True
solve [_] = False
solve (x:y:xs)
  | x + y == 0 = all (==0) xs
  | x + y > 0 = solve (x+y : xs)
  | otherwise = False

parseInteger :: String -> Integer
parseInteger = fromIntegral . fst . fromJust . C.readInt . fromString

parseIntegerList :: String -> [Integer]
parseIntegerList = map parseInteger . words

printAns :: Bool -> IO ()
printAns True = putStrLn "YES"
printAns False = putStrLn "NO"

main :: IO ()
main = do
  n <- parseInteger <$> getLine
  replicateM_ (fromIntegral n) (printAns <$> solve =<< (parseIntegerList <$> (getLine >> getLine)))

module Main where

import Control.Monad (liftM2, replicateM_)
import Data.Bits
-- import Debug.Trace
import Data.Char (digitToInt)
import Data.Maybe (fromJust)
import Data.String
import qualified Data.ByteString.Char8 as C

solve :: [Integer] -> Bool
solve [] = True
solve [0] = True
solve [_] = False
solve (x:y:xs)
  | x == 0 = all (==0) (y:xs)
  | x + y > 0 = solve (x+y : xs)
  | otherwise = False

parseInteger :: String -> Integer
parseInteger = fromIntegral . fst . fromJust . C.readInt . fromString

parseIntegerList :: String -> [Integer]
parseIntegerList = map parseInteger . words

printAns :: Bool -> IO ()
printAns True = putStrLn "YES"
printAns False = putStrLn "NO"

main :: IO ()
main = do
  n <- parseInteger <$> getLine
  replicateM_ (fromIntegral n) (printAns <$> solve =<< (parseIntegerList <$> (getLine >> getLine)))

module Main where

import Control.Monad (liftM2, replicateM_)
import Data.Bits
-- import Debug.Trace
import Data.Char (digitToInt)
import Data.Maybe (fromJust)
import Data.String
import qualified Data.ByteString.Char8 as C

solve :: [Integer] -> Bool
solve [] = True
solve [0] = True
solve [_] = False
solve (x:y:xs)
  | x == 0 = all (==0) (y:xs)
  | x + y >= 0 = solve (x+y : xs)
  | otherwise = False

parseInteger :: String -> Integer
parseInteger = fromIntegral . fst . fromJust . C.readInt . fromString

parseIntegerList :: String -> [Integer]
parseIntegerList = map parseInteger . words

printAns :: Bool -> IO ()
printAns True = putStrLn "YES"
printAns False = putStrLn "NO"

main :: IO ()
main = do
  n <- parseInteger <$> getLine
  replicateM_ (fromIntegral n) (printAns <$> solve =<< (parseIntegerList <$> (getLine >> getLine)))


{- This code was written by
Russell Emerine - linguist,
mathematician, coder,
musician, and metalhead. -}
import Control.Monad
import Data.Bool

main = getLine >>= flip replicateM_ test . read

test = do
  n <- read <$> getLine
  putStrLn $ bool "FastestFinger" "Ashishgup" $ solve n

solve n
  | p == 0, x == 1 = False
  | p == 0 = True
  | p == 1, x == 1 = True
  | p == 1 = any ((== 0) . (x `mod`)) $ takeWhile (<= floor (sqrt $ fromIntegral x)) [3, 5 ..]
  | x == 1 = False
  | otherwise = True
  where
    (p, x) = powerTwo n

powerTwo = f . (,) 0
  where
    f (p, n)
      | odd n = (p, n)
      | otherwise = f (p + 1, n `div` 2)

import Control.Monad

main :: IO ()
main = do
  t <- readLn
  replicateM_ t routine

routine :: IO ()
routine = do
  n <- readLn
  putStrLn $ if solve n then "Ashishgup" else "FastestFinger"

solve :: Int -> Bool
solve n
  | n == 1 = False
  | odd n = True
  | n == 2 = True
  | pow2 n = False
  | otherwise = not $ isPrime (n `div` 2)

isPrime :: Int -> Bool
isPrime n = not $ any (\x -> n `mod` x == 0) [2..sq]
  where
    sq = floor $ sqrt $ fromIntegral n

pow2 :: Int -> Bool
pow2 n = x == n
  where
      x = until (>=n) (*2) 1

{- This code was written by
Russell Emerine - linguist,
mathematician, coder,
musician, and metalhead. -}
import Control.Monad
import Data.Bool

main = getLine >>= flip replicateM_ test . read

test = do
  n <- read <$> getLine
  putStrLn $ bool "FastestFinger" "Ashishgup" $ solve n

solve n
  | p == 0, x == 1 = False
  | p == 0 = True
  | p == 1, x == 1 = True
  | p == 1 = any ((== 0) . (x `div`)) $ takeWhile (<= floor (sqrt $ fromIntegral x)) [1, 3 ..]
  | x == 1 = False
  | otherwise = True
  where
    (p, x) = powerTwo n

powerTwo = f . (,) 0
  where
    f (p, n)
      | odd n = (p, n)
      | otherwise = f (p + 1, n `div` 2)

{- This code was written by
Russell Emerine - linguist,
mathematician, coder,
musician, and metalhead. -}
import Control.Monad
import Data.Bool

main = getLine >>= flip replicateM_ test . read

test = do
  n <- read <$> getLine
  putStrLn $ bool "FastestFinger" "Ashishgup" $ solve n

solve n
  | p == 0, x == 1 = False
  | p == 0 = True
  | p == 1, x == 1 = True
  | p == 1 = any ((== 0) . (x `div`)) $ takeWhile (<= floor (sqrt $ fromIntegral x)) [3, 5 ..]
  | x == 1 = False
  | otherwise = True
  where
    (p, x) = powerTwo n

powerTwo = f . (,) 0
  where
    f (p, n)
      | odd n = (p, n)
      | otherwise = f (p + 1, n `div` 2)

import Control.Monad

main :: IO ()
main = do
  t <- readLn
  replicateM_ t routine

routine :: IO ()
routine = do
  n <- readLn
  putStrLn $ if solve n then "Ashishgup" else "FastestFinger"

solve :: Int -> Bool
solve n
  | n == 1 = False
  | odd n = True
  | n == 2 = True
  | otherwise = not $ isPrime (n `div` 2)

isPrime :: Int -> Bool
isPrime n = not $ any (\x -> n `mod` x == 0) [2..sq]
  where
    sq = floor $ sqrt $ fromIntegral n


module Main where
import Control.Monad
import Control.Applicative
import Data.List
import Data.Bits

solve :: String -> Int
solve = snd . foldl' (\(a, b) c -> if c == 'b' then (a, b +% a +% (base - 1)) else (a *% 2, b)) (1, 0)
  where
    base = 1000000007
    a +% b = (a + b) `mod` base

    a *% b = (a * b) `mod` base

main = solve <$> getLine >>= print

-- abbaaabaabaaaaabbbbaababaaaaabaabbaaaaabbaabbaaaabbbabbbabb
-- bbaba
-- bbbbaa
-- aab
-- abba
-- bbab a
-- bbbbaa

-- X'abbaY
-- X'bbabaY
-- ab  b
-- bba b
-- aab
-- ab -> bba

-- aa ab
-- aa bba  1
-- a ab ba
-- a bba ba 2
-- abb ab a
-- abb bba a 3
-- ab bbbaa
-- bba bbbaa 4
-- bb ab bb aa
-- bb bba bb aa 5
-- bbbb ab baa
-- bbbb bba baa 6

-- first takes 1, add 2  -> 2
-- second takes 2, add 2 -> 4
-- third takes 4, add 4 -> 8
-- 1 + 2 + 4
-- bbbbbbb aaa

main = getLine >>= print . solve 0 1

solve ans _ [] = ans
solve ans a ('a':cs) = solve ans (a * 2 `mod` 1000000007) cs
solve ans a ('b':cs) = solve ((ans + a - 1) `mod` 1000000007) a cs

module Main where
import Control.Monad
import Control.Applicative
import Data.List
import Data.Bits

solve :: String -> Int
solve = snd . foldl' (\(a, b) c -> if c == 'b' then (a, if a == 0 then b else b +% ((2 `shiftL` (a - 1) - 1) `mod` base)) else (a +% 1, b)) (0, 0)
  where
    base = 1000000007
    a +% b = (a + b) `mod` base

main = solve <$> getLine >>= print

-- aab
-- abba
-- bbab a
-- bbbbaa

-- X'abbaY
-- X'bbabaY
-- ab  b
-- bba b
-- aab
-- ab -> bba

-- aa ab
-- aa bba  1
-- a ab ba
-- a bba ba 2
-- abb ab a
-- abb bba a 3
-- ab bbbaa
-- bba bbbaa 4
-- bb ab bb aa
-- bb bba bb aa 5
-- bbbb ab baa
-- bbbb bba baa 6

-- first takes 1, add 2  -> 2
-- second takes 2, add 2 -> 4
-- third takes 4, add 4 -> 8
-- 1 + 2 + 4
-- bbbbbbb aaa


{-# OPTIONS_GHC -O2 -optc-Ofast -fignore-asserts #-}
import Control.Applicative
import Data.Int (Int64)
import Data.Maybe
import qualified Data.ByteString.Char8 as C

readInt :: Integral a => C.ByteString -> a
readInt = fromIntegral . fst . fromJust . C.readInt

gao :: Int64 -> Int64 -> [(Int64, Int64)] -> Int -> Int
gao s t [] res = res + if s < t then 1 else 0
gao _ _ [_] res = res + 1
gao s t a@((i, _): _) res | s > i = gao i t a res
gao s t ((i, x): (j, y): a) res
  | m /= 0 || lval <= 0 = gao j t ((j, y): a) $ res + 1
  | otherwise = gao ridx t a' $ res + 1
  where
  (d, m) = (y - x) `quotRem` (j - i)
  lval = x - (i - s) * d
  ridx = if d >= 0 then t else j + (y-1) `quot` (-d) + 1
  a' = dropWhile (\(k, z) -> z == y + (k - j) * d) a

main :: IO ()
main = do
  n <- readInt <$> C.getLine
  a <- map readInt . C.words <$> C.getLine
  print $ gao 0 n [ie | ie@(_, e) <- zip [0..] a, e > 0] 0

import Control.Applicative
import Control.Monad
import Data.Int (Int64)
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString.Char8 as C

readInt :: Integral a => C.ByteString -> a
readInt = fromIntegral . fst . fromJust . C.readInt

getInts :: IO [Int64]
getInts = map readInt . C.words <$> C.getLine

getInt :: IO Int64
getInt = readInt <$> C.getLine

gao :: Int64 -> [(Int64, Int64)] -> Int64 -> Int -> Int
gao s [] n res = res + if s < n then 1 else 0
gao _ [_] _ res = res + 1
gao s all@((i,x) : (j,y) : xs) n res
  | s > i = gao i all n res
  | m /= 0 || l <= 0 = gao j ((j,y) : xs) n $ res + 1
  | otherwise = gao next xs' n $ res + 1
  where
  (d,m) = (y - x) `quotRem` (j - i)
  l = x - (i - s) * d
  next = if d >= 0 then n else j + (y-1) `quot` (-d) + 1
  xs' = dropWhile (\(k,z) -> z == (k-j)*d + y) xs

main :: IO ()
main = do
  n <- getInt
  array <- getInts
  print $ gao 0 [k | k@(_,i) <- zip [0..] array, i > 0] n 0

import Control.Applicative
import Control.Monad
import Data.Int (Int64)
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString.Char8 as C

readInt :: Integral a => C.ByteString -> a
readInt = fromIntegral . fst . fromJust . C.readInt

getInts :: IO [Int64]
getInts = map readInt . C.words <$> C.getLine

getInt :: IO Int64
getInt = readInt <$> C.getLine

gao :: Int64 -> [(Int64, Int64)] -> Int64 -> Int -> Int
gao s [] n res = res + if s < n then 1 else 0
gao _ [_] _ res = res + 1
gao s all@((i,x) : (j,y) : xs) n res
  | lval <= 0.0 = gao i all n $ res + 1
  | cant = gao n xs n $ res + 1
  | otherwise = gao j ((j,y):xs) n $ res
  where
  dif = fromIntegral $ y - x :: Double
  dis = fromIntegral $ j - i :: Double
  x' = fromIntegral $ x :: Double
  y' = fromIntegral $ y :: Double
  dis' = if i <= s then 0.0 else fromIntegral $ i - s :: Double
  d = dif / dis :: Double
  lval = x' - dis' * d
  next = if d >= 0 then n else ceiling $ y' / (-d)
  calc (t1,t2) (t3,t4) = (t4 - t2) `quotRem` (t3 - t1)
  cant = if null xs then True else calc (j,y) (i,x) /= calc (head xs) (j,y)

main :: IO ()
main = do
  n <- getInt
  array <- getInts
  print $ gao 0 [k | k@(_,i) <- zip [0..] array, i > 0] n 0

import Control.Applicative
import Control.Monad
import Data.Int (Int64)
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString.Char8 as C

readInt :: Integral a => C.ByteString -> a
readInt = fromIntegral . fst . fromJust . C.readInt

getInts :: IO [Int64]
getInts = map readInt . C.words <$> C.getLine

getInt :: IO Int64
getInt = readInt <$> C.getLine

gao :: Int64 -> [(Int64, Int64)] -> Int64 -> Int -> Int
gao s [] n res = res + if s < n then 1 else 0
gao _ [_] _ res = res + 1
gao s all@((i,x) : (j,y) : xs) n res
  | m /= 0 || l <= 0 = if s < i then gao i all n (res+1) else gao next xs n (res+2)
  | cant = gao next xs n $ res + 1
  | otherwise = gao i ((j,y):xs) n res
  where
  (d,m) = (y - x) `quotRem` (j - i)
  l = x - (i - s) * d
  cant = if null xs then True else (snd $ head xs) /= (y + d * ((fst $ head xs) - j))
  next = if null xs then n else fst $ head xs

main :: IO ()
main = do
  n <- getInt
  array <- getInts
  print $ gao 0 [k | k@(_,i) <- zip [0..] array, i > 0] n 0

import Control.Applicative
import Control.Monad
import Data.Int (Int64)
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString.Char8 as C

readInt :: Integral a => C.ByteString -> a
readInt = fromIntegral . fst . fromJust . C.readInt

getInts :: IO [Int64]
getInts = map readInt . C.words <$> C.getLine

getInt :: IO Int64
getInt = readInt <$> C.getLine

gao :: Int64 -> [(Int64, Int64)] -> Int64 -> Int -> Int
gao s [] n res = res + if s < n then 1 else 0
gao _ [_] _ res = res + 1
gao s all@((i,x) : (j,y) : xs) n res
  | m /= 0 || l <= 0 = if x > i - s then gao j ((j,y):xs) n (res+1) else gao i xs n (res+1)
  | cant = gao next xs n $ res + 1
  | otherwise = gao i ((j,y):xs) n res
  where
  (d,m) = (y - x) `quotRem` (j - i)
  l = x - (i - s) * d
  cant = if null xs then True else (snd $ head xs) /= (y + d * ((fst $ head xs) - j))
  next' = if d >= 0 then n else j + (y-1) `quot` (-d) + 1
  next = if null xs then next' else fst $ head xs

main :: IO ()
main = do
  n <- getInt
  array <- getInts
  print $ gao 0 [k | k@(_,i) <- zip [0..] array, i > 0] n 0

import Control.Applicative
import Control.Monad
import Data.Int (Int64)
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString.Char8 as C

readInt :: Integral a => C.ByteString -> a
readInt = fromIntegral . fst . fromJust . C.readInt

getInts :: IO [Int64]
getInts = map readInt . C.words <$> C.getLine

getInt :: IO Int64
getInt = readInt <$> C.getLine

gao :: Int64 -> [(Int64, Int64)] -> Int64 -> Int -> Int
gao s [] n res = res + if s < n then 1 else 0
gao _ [_] _ res = res + 1
gao s all@((i,x) : (j,y) : xs) n res
  | m /= 0 || l <= 0 = gao j ((j,y) : xs) n $ res + 1
  | otherwise = gao next xs' n $ res + 1
  where
  (d,m) = (y - x) `quotRem` (j - i)
  l = x - (i - s) * d
  next = if d >= 0 then n else j + (y-1) `quot` (-d) + 1
  xs' = dropWhile (\(k,z) -> z == (k-j)*d + y) xs

main :: IO ()
main = do
  n <- getInt
  array <- getInts
  print $ gao 0 [k | k@(_,i) <- zip [0..] array, i > 0] n 0

import Control.Applicative
import Control.Monad
import Data.Int (Int64)
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString.Char8 as C

readInt :: Integral a => C.ByteString -> a
readInt = fromIntegral . fst . fromJust . C.readInt

getInts :: IO [Int64]
getInts = map readInt . C.words <$> C.getLine

getInt :: IO Int64
getInt = readInt <$> C.getLine

gao :: Int64 -> [(Int64, Int64)] -> Int64 -> Int -> Int
gao s [] n res = res + if s < n then 1 else 0
gao _ [_] _ res = res + 1
gao s all@((i,x) : (j,y) : xs) n res
  | m /= 0 || l <= 0 = if s < i then gao j ((j,y):xs) n (res+1) else gao next xs n (res+2)
  | cant = gao next xs n $ res + 1
  | otherwise = gao i ((j,y):xs) n res
  where
  (d,m) = (y - x) `quotRem` (j - i)
  l = x - (i - s) * d
  cant = if null xs then True else (snd $ head xs) /= (y + d * ((fst $ head xs) - j))
  next = if null xs then n else fst $ head xs

main :: IO ()
main = do
  n <- getInt
  array <- getInts
  print $ gao 0 [k | k@(_,i) <- zip [0..] array, i > 0] n 0


-- import Debug.Trace
import Data.Bits

type InType  = [Int]
type OutType = Int

inpLines :: Int
inpLines = 2

chunksOf :: Int -> [a] -> [[a]]
chunksOf _ [] = []
chunksOf x l  = take x l : chunksOf x (drop x l)

toArr :: Read a => String -> [a]
toArr = map read . words

solve :: InType -> OutType
solve = foldr (.&.) all_bits
    where all_bits = 2 ^ 31 - 1

receive :: [String] -> InType
receive [n, a] = toArr a
receive _      = undefined

main :: IO ()
main = interact $
  unlines . map (show . solve . receive) . chunksOf inpLines . tail . lines
    where showb True = "YES"
          showb False = "NO"

{-# LANGUAGE BangPatterns #-}

module Main where

import qualified Data.ByteString.Char8 as C8
import           Data.Maybe (fromJust)
import           Control.Monad.Reader (replicateM_)
import           Data.Bits (Bits((.&.)))
import           Data.List (sort)

readInput :: C8.ByteString -> [[Int]]
readInput = map
  (map
     (\str -> let (Just (i, _)) = C8.readInt str
              in i)
   . C8.words)
  . C8.lines

main :: IO ()
main = do
  t <- readLn :: IO Int
  replicateM_ t
    $ do
      n <- readLn :: IO Int
      sequence <- map (fst . fromJust . C8.readInt) . C8.words <$> C8.getLine
      print $ solve sequence

solve :: [Int] -> Int
solve [x] = x
solve xs = foldr (.&.) (2 ^ 31-1) xs
{->>>solve [1,2]
0
-}

import Control.Arrow ((>>>))
import Data.Bits ((.&.))

solve :: [Int] -> Int
solve = foldl1 (.&.)

process [] = []
process (x:y:ys) = show (solve ((map read . words) y)) : process ys

main = interact $ lines >>> drop 1 >>> process >>> unlines

import Control.Monad
import Data.List
import Data.Bits ((.&.))

main = do
  line <- getLine
  let t = read line :: Int
  forM_ [1..t] $ \_ -> do
    unused <- getLine
    line <- getLine
    let numbers = map read $ words line :: [Int]
    putStrLn $ show $ foldl1 (.&.) numbers

import Data.Bits ((.&.))

main = do
    t <- read <$> getLine :: IO Int
    mapM_ solve [1..t]

solve :: Int -> IO ()
solve _ = do
    getLine
    a <- map read . words <$> getLine :: IO [Int]
    print $ foldr1 (.&.) a

import Control.Arrow ((>>>))
import Data.Bits ((.&.))

main :: IO ()
main =
  interact $
    lines
      >>> drop 1
      >>> chunksOf 2
      >>> map (last >>> words >>> map read >>> solve >>> show)
      >>> unlines

solve :: [Int] -> Int
solve = foldl1 (.&.)

chunksOf _ [] = []
chunksOf k xs = let (hs, ts) = splitAt k xs in hs : chunksOf k ts

{-# LANGUAGE BangPatterns #-}

module Main where

import qualified Data.ByteString.Char8 as C8
import           Data.Maybe (fromJust)
import           Control.Monad.Reader (replicateM_)
import           Data.Bits (Bits((.&.)))
import           Data.List (sort)

readInput :: C8.ByteString -> [[Int]]
readInput = map
  (map
     (\str -> let (Just (i, _)) = C8.readInt str
              in i)
   . C8.words)
  . C8.lines

main :: IO ()
main = do
  t <- readLn :: IO Int
  replicateM_ t
    $ do
      n <- readLn :: IO Int
      sequence <- map (fst . fromJust . C8.readInt) . C8.words <$> C8.getLine
      print $ solve sequence

solve :: [Int] -> Int
solve [x] = x
solve xs = maximum xs .&. minimum xs
{->>>solve [3,11,3,7]
3
-}

{-# LANGUAGE BangPatterns #-}

module Main where

import qualified Data.ByteString.Char8 as C8
import           Data.Maybe (fromJust)
import           Control.Monad.Reader (replicateM_)
import           Data.List (sort)

readInput :: C8.ByteString -> [[Int]]
readInput = map
  (map
     (\str -> let (Just (i, _)) = C8.readInt str
              in i)
   . C8.words)
  . C8.lines

main :: IO ()
main = do
  t <- readLn :: IO Int
  replicateM_ t
    $ do
      n <- readLn :: IO Int
      sequence <- map (fst . fromJust . C8.readInt) . C8.words <$> C8.getLine
      print $ solve sequence

solve :: [Int] -> Int
solve [x] = x
solve xs = y - z - 1
  where
    (y:z:ys) = reverse $ sort xs
{->>>solve [3,11,3,7]
3
-}


import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as LB8
import qualified Data.ByteString.Char8 as B8

find3 n = if n == 1 then 1 else go 2
  where go v = if v * pred v `div` 2 <= n then go (v + 1) else v - 1

getInt = (fromIntegral . fst . fromJust . B8.readInt) <$> B8.getLine

main :: IO ()
main = do
  a <- getInt
  replicateM_ a $ do
    b <- getInt
    let c3 = find3 b
    putStr "1"
    putStr "33"
    when (c3 /= 1) $ LB8.putStr $ LB8.replicate (fromIntegral $ b - ((c3 * pred c3) `div` 2)) '7'
    LB8.putStr $ LB8.replicate (fromIntegral $ c3 - 2) '3'
    putStrLn "7"

    import Control.Monad

    triangleNums = [(i, i*(i+1) `div` 2) | i <- reverse [1..100000]]

    decomp 0 _ = []
    decomp _ [] = error "NOT DECOMPOSABLE"
    decomp i xs@(x:_)
        | i >= snd x = fst x : decomp (i- snd x) xs
        | otherwise = decomp i $ tail xs

    printThreesAndSevens [] _ = return ()
    printThreesAndSevens is@(i:i') idx
        | i == idx = do
            putStr "7"
            printThreesAndSevens i' idx
        | otherwise = do
            putStr "3"
            printThreesAndSevens is $ idx + 1

    main :: IO ()
    main = do
        t <- liftM (read :: String -> Int) getLine
        forM_ [1..t] $ \x -> do
            n <- liftM read getLine
            let composed = reverse $ decomp n triangleNums
            putStr "13"
            printThreesAndSevens composed 0
            putStrLn ""

import Control.Monad

triangleNums = [(i, i*(i+1) `div` 2) | i <- reverse [1..100000]]

decomp 0 _ = []
decomp _ [] = error "NOT DECOMPOSABLE"
decomp i xs@(x:_)
    | i >= snd x = fst x : decomp (i- snd x) xs
    | otherwise = decomp i $ tail xs

printThreesAndSevens [] _ = return ()
printThreesAndSevens is@(i:i') idx
    | i == idx = do
        putStr "7"
        printThreesAndSevens i' idx
    | otherwise = do
        putStr "3"
        printThreesAndSevens is $ idx + 1

main :: IO ()
main = do
    n <- liftM read getLine
    let composed = reverse $ decomp n triangleNums
    putStr "13"
    printThreesAndSevens composed 0

import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as LB8
import qualified Data.ByteString.Char8 as B8

find3 n = if n == 1 then 1 else go 2
  where go v = if v * pred v `div` 2 <= n then go (v + 1) else v - 1

getInt = (fromIntegral . fst . fromJust . B8.readInt) <$> B8.getLine

main :: IO ()
main = do
  a <- getInt
  replicateM_ a $ do
    b <- getInt
    let c3 = find3 b
    putStr "1"
    putStr "33"
    when (c3 /= b) $ LB8.putStr $ LB8.replicate (fromIntegral $ b - ((c3 * pred c3) `div` 2)) '7'
    LB8.putStr $ LB8.replicate (fromIntegral $ c3 - 2) '3'
    putStrLn "7"

import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as LB8
import qualified Data.ByteString.Char8 as B8

find3 n = if n == 1 then 1 else go 2
  where go v = if v * pred v `div` 2 <= n then go (v + 1) else v - 1

getInt = (fromIntegral . fst . fromJust . B8.readInt) <$> B8.getLine

main :: IO ()
main = do
  a <- getInt
  replicateM_ a $ do
    b <- getInt
    let c3 = find3 b
    putStr "1"
    putStr "33"
    LB8.putStr $ LB8.replicate (fromIntegral $ b - ((c3 * pred c3) `div` 2)) '7'
    LB8.putStr $ LB8.replicate (fromIntegral $ c3 - 2) '3'
    putStrLn "7"


-- @betaveros :: vim:set fdm=marker:
{-# LANGUAGE LambdaCase, NPlusKPatterns, TupleSections #-}
{-# OPTIONS_GHC -fno-warn-unused-imports -fno-warn-missing-signatures #-}
-- import ALL the things! {{{
import Control.Applicative
import Control.Arrow
import Control.Exception
import Control.Monad
import Control.Monad.ST

import Data.Bits
import Data.Char
import Data.Either
import Data.Function
import Data.IORef
import Data.List
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.STRef
import Data.String
import Data.Tuple

import qualified Data.Map as Map
import Data.Map (Map)
import qualified Data.Set as Set
import Data.Set (Set)
import qualified Data.Sequence as Seq
import Data.Sequence (Seq, (<|), (|>), (><))

import Debug.Trace
import Text.Printf
-- }}}
-- silly utilities {{{
(#) = flip ($)
infixl 0 #

fi :: (Integral a, Num b) => a -> b
fi = fromIntegral
glength :: (Num b) => [a] -> b
glength = genericLength

readInt     = read :: String -> Int
readInteger = read :: String -> Integer
-- (!?) :: (Ord k) => Map k v -> k -> Maybe v
-- (!?) = flip Map.lookup
histogram :: (Ord a, Num b) => [a] -> Map a b
histogram = Map.fromListWith (+) . map (,1)

(!>) :: Seq a -> Int -> a
(!>) = Seq.index
-- }}}
-- input and output {{{
inputInt     = (read <$> getLine) :: IO Int
inputInteger = (read <$> getLine) :: IO Integer
inputDouble  = (read <$> getLine) :: IO Double

inputRow :: (Read a) => IO [a]
inputRow = map read . words <$> getLine
inputInts     = inputRow :: IO [Int]
inputIntegers = inputRow :: IO [Integer]
inputDoubles  = inputRow :: IO [Double]

ssUnwords :: [ShowS] -> ShowS
ssUnwords [] = id
ssUnwords (x:xs) = x . (' ':) . ssUnwords xs

ssUnlines :: [ShowS] -> ShowS
ssUnlines [] = id
ssUnlines (x:xs) = x . ('\n':) . ssUnlines xs

showMany :: (Show a) => [a] -> String
showMany xs = ssUnwords (map shows xs) ""
showMatrix :: (Show a) => [[a]] -> String
showMatrix xs = ssUnlines (map (ssUnwords . map shows) xs) ""

printMany :: (Show a) => [a] -> IO ()
printMany xs = putStrLn (showMany xs)
printMatrix :: (Show a) => [[a]] -> IO ()
printMatrix xs = putStr (showMatrix xs)
-- }}}

isMirror s = s == reverse s && all (`elem` "AHIMOTUVWXY") s

main :: IO ()
main = do
	s <- getLine
	putStrLn $ if isMirror s then "YES" else "NO"

-- @betaveros :: vim:set fdm=marker:
{-# LANGUAGE LambdaCase, NPlusKPatterns, TupleSections #-}
{-# OPTIONS_GHC -fno-warn-unused-imports -fno-warn-missing-signatures #-}
-- import ALL the things! {{{
import Prelude hiding (mapM, mapM_, sequence, sequence_, foldl, foldl1, foldr, foldr1, and, or, any, all, sum, product, concat, concatMap, maximum, minimum, elem, notElem) -- generalized by Data.Foldable
import Control.Applicative
import Control.Arrow
import Control.Exception
import Control.Monad hiding (mapM, mapM_, forM, forM_, sequence, sequence_, msum)
import Control.Monad.ST

import qualified Data.ByteString.Char8 as BS
import Data.ByteString.Char8 (ByteString)
import Data.Bits
import Data.Char
import Data.Either
import Data.Foldable
import Data.Function
import Data.IORef
import Data.List hiding (foldl, foldl', foldl1, foldl1', foldr, foldr1, concat, concatMap, and, or, any, all, sum, product, maximum, minimum, elem, notElem, find)
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.STRef
import Data.String
import Data.Tuple

import qualified Data.Map as Map
import Data.Map (Map)
import qualified Data.Set as Set
import Data.Set (Set)
import qualified Data.Sequence as Seq
import Data.Sequence (Seq, (<|), (|>), (><))

import Debug.Trace
import Text.Printf
import System.IO
-- }}}
-- silly utilities {{{
-- stolen from lens:
a & f = f a
a <&> f = fmap f a
infixl 1 &, <&>

fi :: (Integral a, Num b) => a -> b
fi = fromIntegral
glength :: (Num b) => [a] -> b
glength = genericLength

readInt     = read :: String -> Int
readInteger = read :: String -> Integer
-- (!?) :: (Ord k) => Map k v -> k -> Maybe v
-- (!?) = flip Map.lookup
histogram :: (Ord a, Num b) => [a] -> Map a b
histogram = Map.fromListWith (+) . map (,1)

(!>) :: Seq a -> Int -> a
(!>) = Seq.index

bsGetLine :: IO ByteString
bsGetLine = BS.getLine <&> fst . BS.spanEnd isSpace
-- }}}
-- input and output {{{
inputInt     = (read <$> getLine) :: IO Int
inputInteger = (read <$> getLine) :: IO Integer
inputDouble  = (read <$> getLine) :: IO Double

inputRow :: (Read a) => IO [a]
inputRow = map read . words <$> getLine
inputInts     = inputRow :: IO [Int]
inputIntegers = inputRow :: IO [Integer]
inputDoubles  = inputRow :: IO [Double]

ssUnwords :: [ShowS] -> ShowS
ssUnwords [] = id
ssUnwords (x:xs) = x . (' ':) . ssUnwords xs

ssUnlines :: [ShowS] -> ShowS
ssUnlines [] = id
ssUnlines (x:xs) = x . ('\n':) . ssUnlines xs

showMany :: (Show a) => [a] -> String
showMany xs = ssUnwords (map shows xs) ""
showMatrix :: (Show a) => [[a]] -> String
showMatrix xs = ssUnlines (map (ssUnwords . map shows) xs) ""

printMany :: (Show a) => [a] -> IO ()
printMany xs = putStrLn (showMany xs)
printMatrix :: (Show a) => [[a]] -> IO ()
printMatrix xs = putStr (showMatrix xs)
-- }}}

isMirror s = s == BS.reverse s && BS.all (`elem` "AHIMOTUVWXY") s

main :: IO ()
main = do
	s <- bsGetLine
	putStrLn $ if isMirror s then "YES" else "NO"

main = do
    line <- getLine
    if line==reverseWords line && checkString line "BCDEFGJKLNPQRSZ"
        then do
			putStrLn $ "YES"
        else do
            putStrLn $ "NO"

reverseWords :: String -> String
reverseWords = unwords . map reverse . words
checkElement :: Char -> String -> Bool
checkElement x [] = False
checkElement x (y:ys)
			| x==y 		= True
			| otherwise = checkElement x ys
checkString :: String -> String -> Bool
checkString x [] = True
checkString x (y:ys)
			| checkElement y x = False
			| otherwise = checkString x ys

main=getLine>>=putStrLn.f
f cs|all(`elem`"AHIMOTUVWXY")cs,cs==reverse cs="YES"|0<1="NO"

startUp s = (s == reverse s) && ( and [x `elem` ['A','H','I','M','T','W','Y','O','X','V','U'] | x <- s ])
main = do
    s <- getLine
    putStr $ if (startUp s) then "YES" else "NO"

-- Codeforces 420A

{-# OPTIONS_GHC -O2 #-}
{-# OPTIONS_GHC -optc-O2 #-}
{-# LANGUAGE BangPatterns #-}

import Control.Applicative
import Data.List

main :: IO ()
main = do
    xs <- getLine
    if xs == reverse xs && all (`elem` "AHIMTWYOXVU") xs
       then putStrLn "YES"
       else putStrLn "NO"

startUp s = (s == reverse s) && ( and [x `elem` ['A','H','I','M','T','W','Y','O','X'] | x <- s ])
main = do
    s <- getLine
    putStr $ if (startUp s) then "YES" else "NO"

startUp s = (s == reverse s) && ( and [x `elem` ['A','H','I','M','T','W','Y'] | x <- s ])
main = do
    s <- getLine
    putStr $ if (startUp s) then "YES" else "NO"

-- @betaveros :: vim:set fdm=marker:
{-# LANGUAGE LambdaCase, NPlusKPatterns, TupleSections #-}
{-# OPTIONS_GHC -fno-warn-unused-imports -fno-warn-missing-signatures #-}
-- import ALL the things! {{{
import Prelude hiding (all, elem)
import Control.Applicative
import Control.Arrow
import Control.Exception
import Control.Monad hiding (forM_)
import Control.Monad.ST

import qualified Data.ByteString.Char8 as BS
import Data.Bits
import Data.Char
import Data.Either
import Data.Foldable
import Data.Function
import Data.IORef
import qualified Data.List as List
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.STRef
import Data.String
import Data.Tuple

import qualified Data.Map as Map
import Data.Map (Map)
import qualified Data.Set as Set
import Data.Set (Set)
import qualified Data.Sequence as Seq
import Data.Sequence (Seq, (<|), (|>), (><))

import Debug.Trace
import Text.Printf
import System.IO
-- }}}
-- silly utilities {{{
(#) = flip ($)
infixl 0 #

fi :: (Integral a, Num b) => a -> b
fi = fromIntegral
glength :: (Num b) => [a] -> b
glength = List.genericLength

readInt     = read :: String -> Int
readInteger = read :: String -> Integer
-- (!?) :: (Ord k) => Map k v -> k -> Maybe v
-- (!?) = flip Map.lookup
histogram :: (Ord a, Num b) => [a] -> Map a b
histogram = Map.fromListWith (+) . map (,1)

(!>) :: Seq a -> Int -> a
(!>) = Seq.index
-- }}}
-- input and output {{{
inputInt     = (read <$> getLine) :: IO Int
inputInteger = (read <$> getLine) :: IO Integer
inputDouble  = (read <$> getLine) :: IO Double

inputRow :: (Read a) => IO [a]
inputRow = map read . words <$> getLine
inputInts     = inputRow :: IO [Int]
inputIntegers = inputRow :: IO [Integer]
inputDoubles  = inputRow :: IO [Double]

ssUnwords :: [ShowS] -> ShowS
ssUnwords [] = id
ssUnwords (x:xs) = x . (' ':) . ssUnwords xs

ssUnlines :: [ShowS] -> ShowS
ssUnlines [] = id
ssUnlines (x:xs) = x . ('\n':) . ssUnlines xs

showMany :: (Show a) => [a] -> String
showMany xs = ssUnwords (map shows xs) ""
showMatrix :: (Show a) => [[a]] -> String
showMatrix xs = ssUnlines (map (ssUnwords . map shows) xs) ""

printMany :: (Show a) => [a] -> IO ()
printMany xs = putStrLn (showMany xs)
printMatrix :: (Show a) => [[a]] -> IO ()
printMatrix xs = putStr (showMatrix xs)
-- }}}

isMirror s = s == BS.reverse s && BS.all (`elem` "AHIMOTUVWXY") s

main :: IO ()
main = do
	s <- BS.getLine
	print $ BS.length s
	forM_ (BS.unpack s) $ \x -> print x
	putStrLn $ if isMirror s then "YES" else "NO"

-- @betaveros :: vim:set fdm=marker:
{-# LANGUAGE LambdaCase, NPlusKPatterns, TupleSections #-}
{-# OPTIONS_GHC -fno-warn-unused-imports -fno-warn-missing-signatures #-}
-- import ALL the things! {{{
import Prelude hiding (all, elem)
import Control.Applicative
import Control.Arrow
import Control.Exception
import Control.Monad
import Control.Monad.ST

import qualified Data.ByteString.Char8 as BS
import Data.Bits
import Data.Char
import Data.Either
import Data.Foldable
import Data.Function
import Data.IORef
import qualified Data.List as List
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.STRef
import Data.String
import Data.Tuple

import qualified Data.Map as Map
import Data.Map (Map)
import qualified Data.Set as Set
import Data.Set (Set)
import qualified Data.Sequence as Seq
import Data.Sequence (Seq, (<|), (|>), (><))

import Debug.Trace
import Text.Printf
-- }}}
-- silly utilities {{{
(#) = flip ($)
infixl 0 #

fi :: (Integral a, Num b) => a -> b
fi = fromIntegral
glength :: (Num b) => [a] -> b
glength = List.genericLength

readInt     = read :: String -> Int
readInteger = read :: String -> Integer
-- (!?) :: (Ord k) => Map k v -> k -> Maybe v
-- (!?) = flip Map.lookup
histogram :: (Ord a, Num b) => [a] -> Map a b
histogram = Map.fromListWith (+) . map (,1)

(!>) :: Seq a -> Int -> a
(!>) = Seq.index
-- }}}
-- input and output {{{
inputInt     = (read <$> getLine) :: IO Int
inputInteger = (read <$> getLine) :: IO Integer
inputDouble  = (read <$> getLine) :: IO Double

inputRow :: (Read a) => IO [a]
inputRow = map read . words <$> getLine
inputInts     = inputRow :: IO [Int]
inputIntegers = inputRow :: IO [Integer]
inputDoubles  = inputRow :: IO [Double]

ssUnwords :: [ShowS] -> ShowS
ssUnwords [] = id
ssUnwords (x:xs) = x . (' ':) . ssUnwords xs

ssUnlines :: [ShowS] -> ShowS
ssUnlines [] = id
ssUnlines (x:xs) = x . ('\n':) . ssUnlines xs

showMany :: (Show a) => [a] -> String
showMany xs = ssUnwords (map shows xs) ""
showMatrix :: (Show a) => [[a]] -> String
showMatrix xs = ssUnlines (map (ssUnwords . map shows) xs) ""

printMany :: (Show a) => [a] -> IO ()
printMany xs = putStrLn (showMany xs)
printMatrix :: (Show a) => [[a]] -> IO ()
printMatrix xs = putStr (showMatrix xs)
-- }}}

isMirror s = s == BS.reverse s && BS.all (`elem` "AHIMOTUVWXY") s

main :: IO ()
main = do
	s <- BS.getLine
	putStrLn $ if isMirror s then "YES" else "NO"

-- @betaveros :: vim:set fdm=marker:
{-# LANGUAGE LambdaCase, NPlusKPatterns, TupleSections #-}
{-# OPTIONS_GHC -fno-warn-unused-imports -fno-warn-missing-signatures #-}
-- import ALL the things! {{{
import Prelude hiding (all, elem)
import Control.Applicative
import Control.Arrow
import Control.Exception
import Control.Monad
import Control.Monad.ST

import qualified Data.ByteString.Char8 as BS
import Data.Bits
import Data.Char
import Data.Either
import Data.Foldable
import Data.Function
import Data.IORef
import qualified Data.List as List
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.STRef
import Data.String
import Data.Tuple

import qualified Data.Map as Map
import Data.Map (Map)
import qualified Data.Set as Set
import Data.Set (Set)
import qualified Data.Sequence as Seq
import Data.Sequence (Seq, (<|), (|>), (><))

import Debug.Trace
import Text.Printf
import System.IO
-- }}}
-- silly utilities {{{
(#) = flip ($)
infixl 0 #

fi :: (Integral a, Num b) => a -> b
fi = fromIntegral
glength :: (Num b) => [a] -> b
glength = List.genericLength

readInt     = read :: String -> Int
readInteger = read :: String -> Integer
-- (!?) :: (Ord k) => Map k v -> k -> Maybe v
-- (!?) = flip Map.lookup
histogram :: (Ord a, Num b) => [a] -> Map a b
histogram = Map.fromListWith (+) . map (,1)

(!>) :: Seq a -> Int -> a
(!>) = Seq.index
-- }}}
-- input and output {{{
inputInt     = (read <$> getLine) :: IO Int
inputInteger = (read <$> getLine) :: IO Integer
inputDouble  = (read <$> getLine) :: IO Double

inputRow :: (Read a) => IO [a]
inputRow = map read . words <$> getLine
inputInts     = inputRow :: IO [Int]
inputIntegers = inputRow :: IO [Integer]
inputDoubles  = inputRow :: IO [Double]

ssUnwords :: [ShowS] -> ShowS
ssUnwords [] = id
ssUnwords (x:xs) = x . (' ':) . ssUnwords xs

ssUnlines :: [ShowS] -> ShowS
ssUnlines [] = id
ssUnlines (x:xs) = x . ('\n':) . ssUnlines xs

showMany :: (Show a) => [a] -> String
showMany xs = ssUnwords (map shows xs) ""
showMatrix :: (Show a) => [[a]] -> String
showMatrix xs = ssUnlines (map (ssUnwords . map shows) xs) ""

printMany :: (Show a) => [a] -> IO ()
printMany xs = putStrLn (showMany xs)
printMatrix :: (Show a) => [[a]] -> IO ()
printMatrix xs = putStr (showMatrix xs)
-- }}}

isMirror s = s == BS.reverse s && BS.all (`elem` "AHIMOTUVWXY") s

main :: IO ()
main = do
	s <- BS.getLine
	hPrint stderr $ BS.length s
	putStrLn $ if isMirror s then "YES" else "NO"

main = do
    line <- getLine
    if line==reverseWords line
        then do
			putStrLn $ "Yes"
        else do
            putStrLn $ "No"

reverseWords :: String -> String
reverseWords = unwords . map reverse . words

main = do
    line <- getLine
    if line==reverseWords line
        then do
			putStrLn $ "YES"
        else do
            putStrLn $ "NO"

reverseWords :: String -> String
reverseWords = unwords . map reverse . words

main = do
    line <- getLine
    if line==reverseWords line && checkString line "BCDEFGJKLNPRSZ"
        then do
			putStrLn $ "YES"
        else do
            putStrLn $ "NO"

reverseWords :: String -> String
reverseWords = unwords . map reverse . words
checkElement :: Char -> String -> Bool
checkElement x [] = False
checkElement x (y:ys)
			| x==y 		= True
			| otherwise = checkElement x ys
checkString :: String -> String -> Bool
checkString x [] = True
checkString x (y:ys)
			| checkElement y x = False
			| otherwise = checkString x ys

main=getLine>>=putStrLn.f
f cs|all(`elem`"ABHIMOTUVWXY")cs,cs==reverse cs="YES"|0<1="NO"