ManavSinghal157/NoFunEval huggingface dataset:
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 218 ms):
import math
N, M = list(map(int, input().split()))


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])
    if temp != 1:
        arr.append([temp, 1])

    if arr == [] and n != 1:
        arr.append([n, 1])

    return arr


mod = 10**9 + 7


def cmb(n, r, mod):
    if (r < 0 or r > n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod


g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range(2, 2*10**5 + 1):
    g1.append((g1[-1] * i) % mod)
    inverse.append((-inverse[mod % i] * (mod//i)) % mod)
    g2.append((g2[-1] * inverse[-1]) % mod)


primes = factorization(M)
# 何箇所に分けるか（cnt以下）,その中でどう分けるか（しきりをどこにおくか（振り分けられないものが出ると選べれないのとおなじになるので、cnt - 選んだ数））

ans = 1

for p, cnt in primes:
    tmp = cmb(N+cnt-1, N-1, mod)
    ans *= tmp
    ans %= mod

print(ans)


Optimized program (CPU time: 103 ms):
def main():
    N, M = list(map(int, input().split()))

    def factorization(n):
        arr = []
        temp = n
        for i in range(2, int(-(-n**0.5//1))+1):
            if temp % i == 0:
                cnt = 0
                while temp % i == 0:
                    cnt += 1
                    temp //= i
                arr.append([i, cnt])
        if temp != 1:
            arr.append([temp, 1])

        if arr == [] and n != 1:
            arr.append([n, 1])

        return arr

    mod = 10**9 + 7

    def cmb(n, r, mod):
        if (r < 0 or r > n):
            return 0
        r = min(r, n-r)
        return g1[n] * g2[r] * g2[n-r] % mod

    g1 = [1, 1]
    g2 = [1, 1]
    inverse = [0, 1]
    for i in range(2, N+100 + 1):
        g1.append((g1[-1] * i) % mod)
        inverse.append((-inverse[mod % i] * (mod//i)) % mod)
        g2.append((g2[-1] * inverse[-1]) % mod)

    primes = factorization(M)
    # 何箇所に分けるか（cnt以下）,その中でどう分けるか（しきりをどこにおくか（振り分けられないものが出ると選べれないのとおなじになるので、cnt - 選んだ数））

    ans = 1

    for p, cnt in primes:
        tmp = cmb(N+cnt-1, N-1, mod)
        ans *= tmp
        ans %= mod

    print(ans)


main()
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 232 ms):
from collections import Counter,defaultdict,deque
import sys,bisect,math,itertools,string,queue
from heapq import heappop, heappush
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
res = float('inf')
for i in range(1,2*10**6):
    if (n/i).is_integer():
        res = min(res, i + n/i)
print((int(res-2)))


Optimized program (CPU time: 205 ms):
from collections import Counter,defaultdict,deque
import sys,bisect,math,itertools,string,queue
from heapq import heappop, heappush
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
res = float('inf')
for i in range(1,int(math.sqrt(n))+1):
    if n%i == 0:
        res = min(res, i + n/i)
print((int(res-2)))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 4740 ms):
import random, math
random.seed()
def gen(a, b, num):
    result = set()
    while 1:
        while 1:
            v = random.randint(a, b)//2*2+1
            if v not in result:
                break
        for x in range(3, int(math.sqrt(v))+1, 2):
            if v % x == 0:
                break
        else:
            result.add(v)
            if len(result) == num:
                break
    return result
class RH():
    def __init__(self, s, base, mod):
        self.base = base
        self.mod = mod
        self.rev = pow(base, mod-2, mod)

        l = len(s)
        self.h = h = [0]*(l+1)
        tmp = 0
        for i in range(l):
            num = ord(s[i])
            tmp = (tmp*base + num) % mod
            h[i+1] = tmp
    def calc(self, l, r):
        return (self.h[r] - self.h[l] * pow(self.base, r-l, self.mod)) % self.mod
    def fixed(self, length):
        v = pow(self.base, length, self.mod)
        h = self.h; mod = self.mod
        def fixed_calc(l):
            return (h[length+l] - h[l] * v) % mod
        return fixed_calc
class RRH():
    def __init__(self, s, num=10, primes=None):
        primes = primes or gen(2, 10**3, num)
        MOD = 10**9+7
        self.rhs = [RH(s, p, MOD) for p in primes]
    def calc(self, l, r):
        return [rh.calc(l, r) for rh in self.rhs]
    def fixed(self, length):
        fs = [rh.fixed(length) for rh in self.rhs]
        def multi_fixed_calc(l):
            return list(f(l) for f in fs)
        return multi_fixed_calc

T = (input()); P = (input())
primes = gen(2, 10**3, 2)
HT = RRH(T, primes=primes)
HP = RRH(P, primes=primes)

pv = HP.calc(0, len(P))
calc = HT.fixed(len(P))
for i in range(len(T)-len(P)+1):
    if calc(i) == pv:
        print(i)

Optimized program (CPU time: 2100 ms):
class RH():
    def __init__(self, s, base, mod):
        self.base = base
        self.mod = mod
        self.rev = pow(base, mod-2, mod)

        l = len(s)
        self.h = h = [0]*(l+1)
        tmp = 0
        for i in range(l):
            num = ord(s[i])
            tmp = (tmp*base + num) % mod
            h[i+1] = tmp
    def calc(self, l, r):
        return (self.h[r] - self.h[l] * pow(self.base, r-l, self.mod)) % self.mod
    def fixed(self, length):
        v = pow(self.base, length, self.mod)
        h = self.h; mod = self.mod
        def fixed_calc(l):
            return (h[length+l] - h[l] * v) % mod
        return fixed_calc
T = (input()); P = (input())
MOD = 10**18+9
HT = RH(T, 11, MOD)
HP = RH(P, 11, MOD)

pv = HP.calc(0, len(P))
calc = HT.fixed(len(P))
for i in range(len(T)-len(P)+1):
    if calc(i) == pv:
        print(i)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 665 ms):
import sys
input = sys.stdin.readline
import numpy as np
N = int(eval(input()))
S = [[] for _ in range(N)]
for i in range(N):
    A = int(eval(input()))
    temp = []
    for _ in range(A):
        temp.append([str(x) for x in input().split()])
        S[i] = temp
ans = 0
for i in range(2 ** N - 1, -1,-1):
    biti = list(bin(i)[2:].zfill(N))
    flag = 0
    for j in range(N):
        if biti[j] == "1":
            for k in S[j]:
                if biti[int(k[0])-1] != k[1]:
                    flag = 1
                    break
        if flag:
            break
    if not flag:    
        biti = np.array(biti)
        ans = max(ans, np.count_nonzero(biti == "1"))
print(ans)

Optimized program (CPU time: 313 ms):
import sys
input = sys.stdin.readline

n = int(eval(input()))

from collections import defaultdict
d = defaultdict(list)

for i in range(n):
    a = int(eval(input()))
    for _ in range(a):
        x, y = [int(x) for x in input().split()]
        d[i].append((x - 1, y))
ans = 0
for k in range(2**n):
    res = 0
    j = bin(k)[2:].zfill(n)
    flag = 1
    for i in range(n):
        if j[i] == "0":
            continue
        for x, y in d[i]:
            if int(j[x]) != int(y):
                flag = 0
                break
        res += 1
    if flag:
        ans = max(ans, res)
print(ans)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 1434 ms):
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import numpy as np

N = int(eval(input()))
prev_dst = [0,0,0]

for n in range(N):
    t, x, y = list(map(int, input().split(" ")))
    a = (t - prev_dst[0]) - (np.abs(prev_dst[1]-x) + np.abs(prev_dst[2]-y))
    if a >= 0 and a % 2 == 0:
        prev_dst = [t, x, y]
    else:
        print("No")
        exit(0)

print("Yes")



Optimized program (CPU time: 385 ms):
#!/usr/bin/python3
# -*- coding: utf-8 -*-

N = int(eval(input()))
prev_dst = [0,0,0]

for n in range(N):
    t, x, y = list(map(int, input().split(" ")))
    a = (t - prev_dst[0]) - (abs(prev_dst[1]-x) + abs(prev_dst[2]-y))
    if a >= 0 and a % 2 == 0:
        prev_dst = [t, x, y]
    else:
        print("No")
        exit(0)

print("Yes")
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 1558 ms):
import numpy as np

N=int(eval(input()))
A=list(map(int, input().split()))

AA=[]
for i,a in enumerate(A):
    AA.append(a-i+1)

b=np.median(AA)
ans=0

for a in AA:
    ans+=abs(a-b)
print((int(ans)))

Optimized program (CPU time: 225 ms):
N=int(eval(input()))
A=list(map(int,input().split()))
B=[]
for i in range(N):
    B.append(A[i]-i-1)
B.sort()
if N%2:
    b=B[N//2]
else:
    b=(B[N//2]+B[N//2-1])//2
ans=0
for k in B:
    ans+=abs(k-b)
print(ans)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 276 ms):
import numpy as np

a = [list(map(int, input().split())) for _ in range(3)]
a = np.array(a)
n = int(eval(input()))

for _ in range(n):
    b = int(eval(input()))
    
    for j in range(3):
        for i in range(3):
            if a[j][i] == b:
                a[j][i] = 0

at = a.transpose()
flag = False
for i in range(3):
    if sum(a[:][i]) == 0 or sum(at[:][i]) == 0:
        flag = True
        break
diag = a[0][0] + a[1][1] + a[2][2]
diag2 = a[2][0] + a[1][1] + a[0][2]
if diag == 0 or diag2 == 0:
    flag = True

print(("Yes" if flag else "No"))

Optimized program (CPU time: 18 ms):
a = [list(map(int, input().split())) for _ in range(3)]
n = int(eval(input()))
b = [int(eval(input())) for _ in range(n)]

flag = False
for i in range(3):
    if a[i][0] in b and a[i][1] in b and a[i][2] in b:
        flag = True
        break
    if a[0][i] in b and a[1][i] in b and a[2][i] in b:
        flag = True
        break
if a[0][0] in b and a[1][1] in b and a[2][2] in b:
    flag = True
if a[2][0] in b and a[1][1] in b and a[0][2] in b:
    flag = True
    
print(("Yes" if flag else "No"))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 36 ms):
# Problem https://atcoder.jp/contests/abc053/tasks/abc053_b
# Python 1st Try
import copy


class Problem:
    def __init__(self, stringdata):
        self.stringdata = stringdata

    def solver(self):
        strcont = copy.copy(self.stringdata)
        result = 0
        rightposition = 0
        leftposition = len(strcont)
        j = 0
        while True:
            if strcont[j] == 'A':
                rightposition = j
                break
            else:
                j = j + 1
        j = leftposition - 1
        while True:
            if strcont[j] == 'Z':
                leftposition = j
                break
            else:
                j = j - 1

        result = leftposition - rightposition + 1
        return result


if __name__ == "__main__":
    s = (input())
    print(("{}".format(Problem(s).solver())))


Optimized program (CPU time: 29 ms):
# Problem https://atcoder.jp/contests/abc053/tasks/abc053_b
# Python 2nd Try


def solver(string):
    result = 200000
    aposi = 0
    zposi = len(string)
    for j in range(0, len(string), 1):
        if string[j] == 'A':
            aposi = j
            break
    for j in range(len(string)-1, -1, -1):
        if string[j] == 'Z':
            zposi = j
            break
    result = zposi - aposi + 1
    return result


if __name__ == "__main__":
    s = (input())
    print(('{}'.format(solver(s))))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 182 ms):
from numpy import*
N,*A=open(0)
N,A,S,a=int(N),loadtxt(A,int),1,0
while S:B=A//N;S=sum(B);a+=S;A+=S-B*-~N
print(a)

Optimized program (CPU time: 44 ms):
N, *A = list(map(int, open(0).read().split()))

ans = 0
S = -1
while S:
    S = sum(a // N for a in A)
    ans += S
    A = [a % N + (S - a // N) for a in A]

print(ans)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 295 ms):
# coding: utf-8
import copy
import numpy as np

n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
rev = copy.copy(v)
rev.reverse()

lcs = [0]+np.cumsum(v)
rcs = [0]+np.cumsum(rev)
# print(lcs,rcs)

ans = -10**20
for l in range(n+1):
  for r in range(n+1-l):
    able = True
    d = k - ( l + r )
    if d < 0:
      able = False
      break
    # m = lcs[l] + rcs[r]
    t = v[0:l] + rev[0:r]
    m = sum(t)
    # print(t,l,r,m)
    t.sort()
    # print(t)
    d = min(d,l+r)
    for i in range(d):
      if 0 > t[i]:
        m -= t[i]
      else:
        break
    # print(m)
    ans = max(ans,m)

print(ans)
    

Optimized program (CPU time: 35 ms):
# coding: utf-8
import copy

n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
rev = copy.copy(v)
rev.reverse()

ans = -10**20
for l in range(n+1):
  for r in range(n+1-l):
    able = True
    d = k - ( l + r )
    if d < 0:
      able = False
      break
    t = v[0:l] + rev[0:r]
    m = sum(t)
    t.sort()
    d = min(d,l+r)
    for i in range(d):
      if 0 > t[i]:
        m -= t[i]
      else:
        break
    ans = max(ans,m)

print(ans)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 615 ms):
N = int(eval(input()))
A = list(map(int, input().split()))
n = N % 2

DP = [[0] * (N+1) for _ in range(N+1)]

for w in range(1, N+1):
    for i in range(N):
        j = i + w
        if j > N:
            continue
        if (w+n) % 2 == 1:
            DP[i][j] = min(DP[i+1][j] - A[i], DP[i][j-1] - A[j-1])
        else:
            DP[i][j] = max(DP[i+1][j] + A[i], DP[i][j-1] + A[j-1])

print((DP[0][N]))


Optimized program (CPU time: 234 ms):
N = int(eval(input()))
A = list(map(int, input().split()))
DP = [[0] * (N+1) for _ in range(N+1)]

for num in range(1, N + 1):
    for start in range(N):
        end = start + num
        if end > N:
            break
        if num == 1:
            DP[start][end] = A[start]
        else:
            DP[start][end] = max(A[start] - DP[start+1][end], A[end - 1] - DP[start][end - 1])
print((DP[0][N]))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 1842 ms):
# ABC 155 C
from collections import Counter
import numpy as np
N = int((input()))
S = [str((input())) for i in range(N)]
S = np.array(S)

count = Counter(S)
max_num = max(count.values())
max_list = [i for i,j in list(count.items()) if j==max_num]

max_list.sort()
print(('\n'.join(max_list)))

Optimized program (CPU time: 304 ms):
import sys
from collections import Counter
N = int(sys.stdin.readline())
S = sys.stdin.read().split()

count = Counter(S)
max_num = max(count.values())
max_list = [i for i,j in list(count.items()) if j==max_num]
 
max_list.sort()
print(('\n'.join(max_list)))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 316 ms):
import sys
import numpy as np
n,m = [int(x) for x in sys.stdin.readline().split()]
r = np.zeros(n)
for i in range(m):
	a, b = [int(x) for x in sys.stdin.readline().split()]
	r[a-1] += 1
	r[b-1] += 1
for i in range(n):
	print((int(r[i])))

Optimized program (CPU time: 17 ms):
import sys
n,m = [int(x) for x in sys.stdin.readline().split()]
r = [0]*n
for _ in range(m):
	a, b = [int(x) for x in sys.stdin.readline().split()]
	r[a-1] += 1
	r[b-1] += 1
for i in r:
	print(i)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 342 ms):
import numpy as np

X, Y, Z, K = list(map(int, input().split()))
A = np.array([int(i) for i in input().split()])
B = np.array([int(i) for i in input().split()])
C =np.array([int(i) for i in input().split()])
A = -np.sort(-A) #要チェックや
B = -np.sort(-B)
C = -np.sort(-C)
D = []
for i in range(min(K, X)):
    for j in range(min(K, Y)):
        if (i + 1) * (j + 1) > K:
            break
        for k in range(min(K, Z)):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            else:

                D.append(A[i] + B[j] + C[k])

X = np.array(D)
X = -np.sort(-X)
for i in range(K):
    print((X[i]))

Optimized program (CPU time: 121 ms):
X, Y, Z, K = list(map(int, input().split()))
A = sorted([int(i) for i in input().split()], reverse=True)
B = sorted([int(i) for i in input().split()], reverse=True)
C = sorted([int(i) for i in input().split()], reverse=True)
D = []
for i in range(min(K, X)):
    for j in range(min(K, Y)):
        if (i + 1) * (j + 1) > K:
            break
        for k in range(min(K, Z)):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            else:

                D.append(A[i] + B[j] + C[k])

D.sort(reverse=True)
for i in range(K):
    print((D[i]))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 266 ms):
N = int(eval(input()))
A = [int(i) for i in input().split()]
dp = [0] * 10 ** 7
ans = 0
for i in range(N):
    x = i + 1 - A[i]
    if x >= 0:
        ans += dp[x]
    y = i + 1 + A[i]
    if y < 10 ** 7:
        dp[y] += 1

print(ans)


Optimized program (CPU time: 106 ms):
N = int(eval(input()))
A = [int(i) for i in input().split()]
INF = 2 * 10 ** 5
dp = [0] * INF
ans = 0
for i in range(N):
    i_i_ = i + 1 - A[i]
    if 0 < i_i_ < INF:
        ans += dp[i_i_]
    i_ = i + 1 + A[i]
    if 0 < i_ < INF:
        dp[i_] += 1

print(ans)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 1774 ms):
import numpy as np
N=int(eval(input()))
def gcd(a,b):
    c=int(np.floor(a/b))
    return a-b*c
if gcd(N,2)==0:
    print(N)
else:
    print((2*N))

Optimized program (CPU time: 17 ms):
N=int(eval(input()))
if N%2==0:
    print(N)
else:
    print((2*N))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 1145 ms):
import numpy as np

n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
d = 2**18

f = np.zeros(d,dtype=int)
for i in a:
    f[i]+=1

tf = np.fft.fft(f)
f = np.fft.ifft(tf*tf)
f = [int(i+0.5) for i in f]

ans=0
for i in range(len(f)-1,0,-1):
    if f[i]<=m:
        ans+=i*f[i]
        m-=f[i]
    elif f[i]>m:
        ans+=i*m
        break
print(ans)

Optimized program (CPU time: 532 ms):
import numpy as np

n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
d = 2**18

f = np.zeros(d,dtype=int)
for i in a:
    f[i]+=1

tf = np.fft.rfft(f)
f = np.fft.irfft(tf*tf)
f = [int(i+0.5) for i in f]

ans=0
for i in range(len(f)-1,0,-1):
    if f[i]<=m:
        ans+=i*f[i]
        m-=f[i]
    elif f[i]>m:
        ans+=i*m
        break
print(ans)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 1985 ms):
s=(input())
t=(input())
s*=2
next=[[-1]*26 for _ in range(len(s))]
alph=[[]for _ in range(26)]
for i in range(len(s)):
	alph[ord(s[i])-ord("a")].append(i)
from bisect import bisect_right
for i in range(len(s)//2):
	for j in range(26):
		if len(alph[j])>bisect_right(alph[j],i):
			next[i][j]=alph[j][bisect_right(alph[j],i)]
ans=1
now=len(s)//2-1
for x in t:
	r=ord(x)-ord("a")
	now=next[now][r]
	if now==-1:
		print((-1))
		exit()
	if now>=len(s)//2:
		ans+=len(s)//2
		now-=len(s)//2
print((ans+now-len(s)//2))

Optimized program (CPU time: 360 ms):
#O(|S|log|s|)
def main():
	s=(input())
	t=(input())
	n=len(s)
	now=-1
	ans=1
	if not set(t)<=set(s):#tがsの部分集合でない
		print((-1))
		exit()
	for x in t:
		now=s.find(x,now+1)#nowより大きいところからxを探す
		if now==-1:#nowより大きいところでxが見つからなかったら
			ans+=n
			now=s.find(x)
	print((ans+now))
if __name__ == '__main__':
	main()
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 131 ms):
# coding: utf-8


def solve(*args: str) -> str:
    k = int(args[0])

    l = 9*(k//7 if k % 7 == 0 else k)
    if l % 2 == 0 or l % 5 == 0:
        return '-1'

    r = phi = l
    for i in range(2, int(-pow(l, 1/2))):
        if r % i == 0:
            phi = phi//i*(i-1)
            while r % i:
                r //= i

    a = 10 % l
    ret = 1
    while(a != 1):
        a = a*10 % l
        ret += 1
        if phi < ret:
            ret = -1
            break

    return str(ret)


if __name__ == "__main__":
    print((solve(*(open(0).read().splitlines()))))


Optimized program (CPU time: 33 ms):
# coding: utf-8
from math import sqrt


def solve(*args: str) -> str:
    k = int(args[0])

    l = 9*(k//7 if 0 == k % 7 else k)
    if 0 == l % 2 or 0 == l % 5:
        return '-1'

    r = phi = l
    for i in range(2, int(sqrt(l)+1)):
        if 0 == r % i:
            phi = phi*(i-1)//i
            while 0 == r % i:
                r //= i
    if 1 < r:
        phi = phi*(r-1)//r

    D = set()
    for d in range(1, int(sqrt(phi)+1)):
        if 0 == phi % d:
            D.add(d)
            D.add(phi//d)

    ret = -1
    for m in sorted(D):
        if 1 == pow(10, m, l):
            ret = m
            break

    return str(ret)


if __name__ == "__main__":
    print((solve(*(open(0).read().splitlines()))))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 450 ms):
import sys
sys.setrecursionlimit(100000)

def intinput(): return list(map(int,sys.stdin.readline().split()))

fib={0:0,1:1,2:1}
for i in range(3,100003):
    fib[i]=fib[i-1]+fib[i-2]
  #  fib.append(fib[i-1]+fib[i-2])

n,m=intinput()
mod=10**9+7
a=[int(sys.stdin.readline()) for _ in range(m)]
if m!=0:
    l=[a[0]]
    for i in range(len(a)-1):
        l.append(a[i+1]-a[i]-1)
    l.append(n-a[-1])
    k=1
    for i in l:
        k=fib[i]*k%mod
    print((k%mod))
else:
    print((fib[n+1]%mod))

Optimized program (CPU time: 113 ms):
import sys
sys.setrecursionlimit(100000)

def intinput(): return list(map(int,sys.stdin.readline().split()))


n, m = intinput()
a = {int(sys.stdin.readline()) for _ in range(m)}
memo = [0 for x in range(n + 4)]
memo[0] = 1
mod = 10 ** 9 + 7
for i in range(n):
    if i + 1 not in a:
        memo[i + 1] = (memo[i + 1] + memo[i]) % mod
    if i + 2 not in a:
        memo[i + 2] = (memo[i + 2] + memo[i]) % mod
print((memo[n]))