请问我编写如下的两个程序怎样提高运行速度
def fibonacci(n):
a, b = 1, 1
for i in range(n):
yield pow(a, 2)
a, b = b, a + b
num = int(input())
result = list(fibonacci(num))
print(sum(result) % 1000000007)
while True:
try:
a = 0
b = 0
c = 0
d = 0
mod = 1e9 + 7
str1 = input()
for i in range(len(str1)):
if str1[i] == '2':
a += 1
if str1[i] == '0':
b = (b + a) % mod
if str1[i] == '1':
c = (c + b) % mod
if str1[i] == '9':
d = (d + c) % mod
print(int(d))
except EOFError:
break