Getting the Nth Fibonacci number when N is 10 ^ 19 does not work if you do it naively (at least I think it will not work).
There is a much better way to do this. And this technique works with a lot of “like this” series. He called the matrix of the Q the Fibonacci .

Where

:
, A B:

. , , , 1000- , .
, , 10 ^ 19, ( ) .
. x ^ N , N, ..
x**100 == x**90 * x**10
, , :
x**2 , x*x - . x*x*x*x , (x**2)**2, . , . , 2 ( , ),
X**100 == X**64 * X**32 * X**4
.
X**100 == (((((X**2)**2)**2)**2)**2)**2 + ...
, , , , Q.
, :
fib_matrix = [[1,1],
[1,0]]
def matrix_square(A, mod):
return mat_mult(A,A,mod)
def mat_mult(A,B, mod):
if mod is not None:
return [[(A[0][0]*B[0][0] + A[0][1]*B[1][0])%mod, (A[0][0]*B[0][1] + A[0][1]*B[1][1])%mod],
[(A[1][0]*B[0][0] + A[1][1]*B[1][0])%mod, (A[1][0]*B[0][1] + A[1][1]*B[1][1])%mod]]
def matrix_pow(M, power, mod):
if power <= 0:
return M
powers = list(reversed([True if i=="1" else False for i in bin(power)[2:]]))
matrices = [None for _ in powers]
matrices[0] = M
for i in range(1,len(powers)):
matrices[i] = matrix_square(matrices[i-1], mod)
result = None
for matrix, power in zip(matrices, powers):
if power:
if result is None:
result = matrix
else:
result = mat_mult(result, matrix, mod)
return result
print matrix_pow(fib_matrix, 10**19, 1000000007)[0][1]
, .