q = 10^9+7 fib :: Int -> Int -> Int fib modulo n = fst (fibr (0,1) n) where tpq (p,q) (a,b) = (mod (p*a + q*b) modulo, mod (q*a + (p+q)*b) modulo) fibr pq@(p,q) n | n < 1 = (0,1) | even n = fibr (p^2 + q^2, 2*p*q + q^2) (div n 2) | otherwise = tpq pq (fibr pq (n-1)) main = do n <- readLn :: IO Integer print $ (fib q . fib q) (fromIntegral (mod n (2 * (10^9) + 16)))