Problems > Sequence j=8a+3b (j0=1,j1=2)

Sequence j=8a+3b (j0=1,j1=2)

Difficulty: 1700 4000 ms 128 MB

math matrix-exponentiation

The sequence has j(0)=1, j(1)=2 and j(n)=8j(n−1)+3j(n−2). Print j(N) modulo 109+7.

Input: one integer N.

Output: j(N) mod 109+7.

Constraints:

  • 0 ≤ N ≤ 1018

Sample Tests

Input 1
4
Output 1
1321
Input 2
0
Output 2
1
Input 3
1
Output 3
2

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.