main :: IO () main = print . solve =<< readLn solve :: Int -> Int solve m = (a + solve' (2 * 2017)) `mod` m where a = 2017 `mod` m :: Int solve' :: Int -> Int solve' e | e <= 1 = (a ^ e) `mod` m | even e = b | otherwise = a * b where b = (solve' (e `div` 2) `mod` m) ^ 2 :: Int