Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,m=map(int,input().split())
    s=list(map(int,input().split()))
    t=list(map(int,input().split()))
    dp=[[0]*(m+1) for _ in [0]*(n+1)]
    for i in range(n+1):
        dp[i][m]=1
    for i in range(m+1):
        dp[n][i]=1
    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):
            dp[i][j]=(dp[i+1][j]+dp[i][j+1]-(s[i]!=t[j])*dp[i+1][j+1])%(10**9+7)
    print(dp[0][0])
main()


Fixed submission:
def main():
    n,m=map(int,input().split())
    s=list(map(int,input().split()))
    t=list(map(int,input().split()))
    dp=[[0]*(m+1) for _ in [0]*(n+1)]
    for i in range(n+1):
        dp[i][m]=1
    for i in range(m+1):
        dp[n][i]=1
    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):
            dp[i][j]=(dp[i+1][j]+dp[i][j+1]-(s[i]!=t[j])*dp[i+1][j+1])%(10**9+7)
    print(dp[0][0])
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import numpy as np
    n,m=map(int,input().split())
    s=list(map(int,input().split()))
    t=list(map(int,input().split()))
    dp=np.zeros((n+1,m+1),dtype=np.int32)
    dp[:,m]=1
    dp[n,:]=1
    for i in range(n-1,-1,-1):
        temp=s[i]
        for j in range(m-1,-1,-1):
            if t[j]!=temp:
                dp[i,j]=(dp[i+1,j]+dp[i,j+1]-dp[i+1,j+1])%(10**9+7)
            else:
                dp[i,j]=(dp[i+1,j]+dp[i,j+1])%(10**9+7)
    print(dp[0,0])
main()

Fixed submission:
def main():
    n,m=map(int,input().split())
    s=list(map(int,input().split()))
    t=list(map(int,input().split()))
    dp=[[0]*(m+1) for _ in [0]*(n+1)]
    for i in range(n+1):
        dp[i][m]=1
    for i in range(m+1):
        dp[n][i]=1
    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):
            dp[i][j]=(dp[i+1][j]+dp[i][j+1]-(s[i]!=t[j])*dp[i+1][j+1])%(10**9+7)
    print(dp[0][0])
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,m=map(int,input().split())
    s=list(map(int,input().split()))
    t=list(map(int,input().split()))
    dp=[[0]*(m+1) for _ in [0]*(n+1)]
    for i in range(n+1):
        dp[i][m]=1
    for i in range(m+1):
        dp[n][i]=1
    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):
            dp[i][j]=(dp[i+1][j]+dp[i][j+1]-(s[i]!=t[j])*dp[i+1][j+1])%(10**9+7)
    print(dp[0][0])
main()

Fixed submission:
def main():
    n,m=map(int,input().split())
    s=list(map(int,input().split()))
    t=list(map(int,input().split()))
    dp=[[0]*(m+1) for _ in [0]*(n+1)]
    for i in range(n+1):
        dp[i][m]=1
    for i in range(m+1):
        dp[n][i]=1
    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):
            dp[i][j]=(dp[i+1][j]+dp[i][j+1]-(s[i]!=t[j])*dp[i+1][j+1])%(10**9+7)
    print(dp[0][0])
main()
Buggy python submission (verdict: Runtime Error):
input_sample=iter("""
1
0 0
""".split("\n")[1:-1])

#問題を解く
N=int(input())
xy=[list(map(int,input().split())) for _ in range(N)]

from collections import Counter
list_=[]

for i in range(N):
    for j in range(i+1,N):
        if xy[i][0]-xy[j][0]<0:
            list_.append((abs(xy[i][0]-xy[j][0]),abs(xy[i][1]-xy[j][1])))
        else:
            list_.append((xy[i][0]-xy[j][0],xy[i][1]-xy[j][1]))

if N==1:
    print(1)
else:
    print(N-Counter(list_).most_common()[0][1])

Fixed submission:
N,K=map(int,input().split())

if K==1:
    print(0)
else:
    print(N-K)
Buggy python submission (verdict: Runtime Error):
N=int(input())
xy=[list(map(int,input().split())) for _ in range(N)]

from collections import Counter
list_=[]

for i in range(N):
    for j in range(i+1,N):
        list_.append((abs(xy[i][0]-xy[j][0]),abs(xy[i][1]-xy[j][1])))

print(N-Counter(list_).most_common()[0][1])

Fixed submission:
N=int(input())
xy=[list(map(int,input().split())) for _ in range(N)]

from collections import Counter
list_=[]

for i in range(N):
    for j in range(i+1,N):
        if xy[i][0]-xy[j][0]==0:
            list_.append(((xy[i][0]-xy[j][0]),abs(xy[i][1]-xy[j][1])))
        elif xy[i][0]-xy[j][0]<0:
            list_.append((-(xy[i][0]-xy[j][0]),-(xy[i][1]-xy[j][1])))
        else:
            list_.append((xy[i][0]-xy[j][0],xy[i][1]-xy[j][1]))

if N==1:
    print(1)
else:
    print(N-Counter(list_).most_common()[0][1])

Buggy python submission (verdict: Runtime Error):
N=int(input())
xy=[list(map(int,input().split())) for _ in range(N)]

from collections import Counter
list_=[]

for i in range(N):
    for j in range(i+1,N):
        list_.append((xy[i][0]-xy[j][0],xy[i][1]-xy[j][1]))

print(N-Counter(list_).most_common()[0][1])

Fixed submission:
N=int(input())
xy=[list(map(int,input().split())) for _ in range(N)]

from collections import Counter
list_=[]

for i in range(N):
    for j in range(i+1,N):
        if xy[i][0]-xy[j][0]==0:
            list_.append(((xy[i][0]-xy[j][0]),abs(xy[i][1]-xy[j][1])))
        elif xy[i][0]-xy[j][0]<0:
            list_.append((-(xy[i][0]-xy[j][0]),-(xy[i][1]-xy[j][1])))
        else:
            list_.append((xy[i][0]-xy[j][0],xy[i][1]-xy[j][1]))

if N==1:
    print(1)
else:
    print(N-Counter(list_).most_common()[0][1])
Buggy python submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
ans_list=[]
if len(A)%2==1:
    for i in range(len(A)-1):
        m=min(A)
        M=max(A)
        A.remove(m)
        A.remove(M)
        if i%2==0:
            ans_list.append([m,M])
            A.append(m-M)
        else:
            ans_list.append([M,m])
            A.append(M-m)
else:
    for i in range(len(A)-1):
        m=min(A)
        M=max(A)
        ans_list.append([])
        A.remove(m)
        A.remove(M)
        if i%2==0:
            ans_list.append([M,m])
            A.append(M-m)
        else:
            ans_list.append([m,M])
            A.append(m-M)

print(A[0])
for i in range(N-1):
    print(ans_list[i][0],ans_list[i][1])

Fixed submission:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    plus = []
    minus = []
    for i in a:
        if i > 0:
            plus.append(i)
        elif i < 0:
            minus.append(i)
        else:
            if len(minus) == 0:
                minus.append(i)
            else:
                plus.append(i)
    plus.sort(reverse=True)
    minus.sort()
    if len(minus) == 0:
        minus.append(plus.pop())
    elif len(plus) == 0:
        plus.append(minus.pop())
    print(sum(plus)-sum(minus))
    now = minus[-1]
    for i in range(len(plus) - 1):
        print(now, plus[i])
        now -= plus[i]
    print(plus[-1], now)
    now = plus[-1] - now
    for i in range(len(minus) - 1):
        print(now, minus[i])
        now -= minus[i]
main()
Buggy python submission (verdict: Runtime Error):
H,W=map(int,input().split())
S=[[i=="." for i in input()+"#"] for _ in range(H)]+[[False]*(W+1)]

map_=[[0]*W for _ in range(H)]
for i in range(H):
    cnt=0
    q=[]
    for j in range(W+1):
        if S[i][j]==False and cnt!=0:
            for Q in q:
                map_[Q[0]][Q[1]]=cnt
            cnt=0
            q=[]
        elif S[i][j]:
            cnt+=1
            q.append([i,j])
for j in range(W):
    cnt=0
    q=[]
    for i in range(H+1):
        if S[i][j]==False and cnt!=0:
            for Q in q:
                map_[Q[0]][Q[1]]+=cnt
            cnt=0
            q=[]
        elif S[i][j]:
            cnt+=1
            q.append([i,j])
print(max([max(i) for i in map_])-1)

Fixed submission:
P,Q,R=map(int,input().split())
print(min(P+Q,Q+R,R+P))
Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
a=[int(input()) for _ in range(M)]
A=[0]*(N+2)
A[0:2]=[0,1]

for i in range(N):
    if i+1 not in a:
        A[i+2]=(A[i]+A[i+1])%(10**9+7)
print(A[N+1])

Fixed submission:
flag=0
N,M=map(int,input().split())
A=[-1]+[int(input()) for _ in range(M)]+[N+1]
mod=10**9+7
    
fib=[1,1]
for i in range(10**5):
    fib.append((fib[i]+fib[i+1])%mod)
    
for i in range(1,M):
    if A[i+1]==A[i]+1:
        flag=1

if flag==0:
    B=[A[i+1]-A[i]-2 for i in range(M+1)]
    ans=1
    for i in range(M+1):
        ans=(ans*fib[B[i]])%mod
    print(ans)
else:
    print(0)
Buggy python submission (verdict: Time Limit Exceeded):
H,W=map(int,input().split())
S=[[i=="." for i in input()+"#"] for _ in range(H)]+[[False]*(W+1)]

map_=[[0]*W for _ in range(H)]
for i in range(H):
    cnt=0
    q=[]
    for j in range(W+1):
        if S[i][j]==False and cnt!=0:
            for Q in q:
                map_[Q[0]][Q[1]]=cnt
            cnt=0
            q=[]
        elif S[i][j]:
            cnt+=1
            q.append([i,j])
for j in range(W):
    cnt=0
    q=[]
    for i in range(H+1):
        if S[i][j]==False and cnt!=0:
            for Q in q:
                map_[Q[0]][Q[1]]+=cnt
            cnt=0
            q=[]
        elif S[i][j]:
            cnt+=1
            q.append([i,j])
print(max([max(i) for i in map_])-1)


Fixed submission:
H,W=map(int,input().split())
S=[[i=="." for i in input()+"#"] for _ in range(H)]+[[False]*(W+1)]

map_=[[0]*W for _ in range(H)]
for i in range(H):
    cnt=0
    q=[]
    for j in range(W+1):
        if S[i][j]==False and cnt!=0:
            for Q in q:
                map_[Q[0]][Q[1]]=cnt
            cnt=0
            q=[]
        elif S[i][j]:
            cnt+=1
            q.append([i,j])
for j in range(W):
    cnt=0
    q=[]
    for i in range(H+1):
        if S[i][j]==False and cnt!=0:
            for Q in q:
                map_[Q[0]][Q[1]]+=cnt
            cnt=0
            q=[]
        elif S[i][j]:
            cnt+=1
            q.append([i,j])
print(max([max(i) for i in map_])-1)
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    mod = 10**9+7
    fact = [1]*200001
    for i in range(1, 200001):
        fact[i] = fact[i-1]*i % mod

    def inv_n(n, mod=10**9+7): return pow(n, mod-2, mod)
    def nCr(n, r, mod=10**9 +
            7): return inv_n(fact[n-r]*fact[r] % mod, mod)*fact[n] % mod

    n, a, b, c = map(int, input().split())
    ans = 0
    for i in range(n, 2*n):
        j = i*100*inv_n(a+b) % mod
        ans = (ans + j*nCr(i-1, n-1)*(pow(a, n, mod)*pow(b, i-n, mod) +
                                      pow(a, i-n, mod)*pow(b, n, mod))*inv_n(pow(a+b, i, mod))) % mod
    print(ans)


main()


Fixed submission:
def main():
    mod = 10**9+7
    fact = [1]*200001
    for i in range(1, 200001):
        fact[i] = fact[i-1]*i % mod

    def inv_n(n, mod=10**9+7): return pow(n, mod-2, mod)
    def nCr(n, r, mod=10**9 +
            7): return inv_n(fact[n-r]*fact[r] % mod, mod)*fact[n] % mod

    n, a, b, c = map(int, input().split())
    ans = 0
    for i in range(n, 2*n):
        j = i*100*inv_n(a+b) % mod
        ans = (ans + j*nCr(i-1, n-1)*(pow(a, n, mod)*pow(b, i-n, mod) +
                                      pow(a, i-n, mod)*pow(b, n, mod))*inv_n(pow(a+b, i, mod))) % mod
    print(ans)


main()

Buggy python submission (verdict: Runtime Error):
    mod = 10**9+7
    fact = [1]*200001
    for i in range(1, 200001):
        fact[i] = fact[i-1]*i % mod

    def inv_n(n, mod=10**9+7): return pow(n, mod-2, mod)
    def nCr(n, r, mod=10**9 +
            7): return inv_n(fact[n-r]*fact[r] % mod, mod)*fact[n] % mod

    n, a, b, c = map(int, input().split())
    ans = 0
    for i in range(n, 2*n):
        j = i*100*inv_n(a+b) % mod
        ans = (ans + j*nCr(i-1, n-1)*(pow(a, n, mod)*pow(b, i-n, mod) +
                                      pow(a, i-n, mod)*pow(b, n, mod))*inv_n(pow(a+b, i, mod))) % mod
    print(ans)


main()


Fixed submission:
def main():
    mod = 10**9+7
    fact = [1]*200001
    for i in range(1, 200001):
        fact[i] = fact[i-1]*i % mod

    def inv_n(n, mod=10**9+7): return pow(n, mod-2, mod)
    def nCr(n, r, mod=10**9 +
            7): return inv_n(fact[n-r]*fact[r] % mod, mod)*fact[n] % mod

    n, a, b, c = map(int, input().split())
    ans = 0
    for i in range(n, 2*n):
        j = i*100*inv_n(a+b) % mod
        ans = (ans + j*nCr(i-1, n-1)*(pow(a, n, mod)*pow(b, i-n, mod) +
                                      pow(a, i-n, mod)*pow(b, n, mod))*inv_n(pow(a+b, i, mod))) % mod
    print(ans)


main()
Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
A=list(map(int,input().split()))
BC=[list(map(int,input().split())) for _ in range(M)]

dic=dict({})
for i in range(N):
    if A[i] not in dic.keys():
        dic[A[i]]=1
    else:
        dic[A[i]]+=1

for i in range(M):
    if BC[i][1] not in dic.keys():
        dic[BC[i][1]]=BC[i][0]
    else:
        dic[BC[i][1]]+=BC[i][0]

dic = sorted(dic.items(), reverse=True)
cnt=0
ans=0
for i in range(len(dic)):
    if cnt>=N:
        break
    else:
        if cnt+dic[i][1]<=N:
            cnt+=dic[i][1]
            ans+=dic[i][0]*dic[i][1]
        else:
            ans+=dic[i][0]*(N-cnt)
            break
print(ans)

Fixed submission:
N,M=map(int,input().split())
A=list(map(int,input().split()))
BC=[list(map(int,input().split())) for _ in range(M)]

dic=dict({})
for i in range(N):
    if A[i] not in dic.keys():
        dic[A[i]]=1
    else:
        dic[A[i]]+=1

for i in range(M):
    if BC[i][1] not in dic.keys():
        dic[BC[i][1]]=BC[i][0]
    else:
        dic[BC[i][1]]+=BC[i][0]

dic = sorted(dic.items(), reverse=True)
cnt=0
ans=0
for i in range(len(dic)):
    if cnt>=N:
        break
    else:
        if cnt+dic[i][1]<=N:
            cnt+=dic[i][1]
            ans+=dic[i][0]*dic[i][1]
        else:
            ans+=dic[i][0]*(N-cnt)
            break
print(ans)
Buggy python submission (verdict: Runtime Error):
def main():
    R,G,B,N=map(int,input().split())
    cnt=0
    for i in range(N//R+1):
        cnt+=sum([1 for j in range((N-i*R)//G+1) if i*R + j*G +((N-p-j*G)//B)*B==N])
    print(cnt)

main()

Fixed submission:
R,G,B,N=map(int,input().split())
cnt=0
for i in range(N//R+1):
    for j in range((N-i*R)//G+1):
            if i*R + j*G +((N-i*R-j*G)//B)*B==N:
                cnt+=1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
R,G,B,N=map(int,input().split())
cnt=0
for i in range(N//R+1):
    for j in range((N-i*R)//G+1):
            if i*R + j*G +((N-i*R-j*G)//B)*B==N:
                cnt+=1
print(cnt)

Fixed submission:
R,G,B,N=map(int,input().split())
cnt=0
for i in range(N//R+1):
    for j in range((N-i*R)//G+1):
            if i*R + j*G +((N-i*R-j*G)//B)*B==N:
                cnt+=1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
R,G,B,N=map(int,input().split())
cnt=0
for i in range(N//R+1):
    for j in range((N-i*R)//G+1):
            if i*R + j*G +((N-i*R-j*G)//B)*B==N:
                cnt+=1
print(cnt)

Fixed submission:
R,G,B,N=map(int,input().split())
cnt=0
for i in range(N//R+1):
    for j in range((N-i*R)//G+1):
            if i*R + j*G +((N-i*R-j*G)//B)*B==N:
                cnt+=1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
R,G,B,N=map(int,input().split())
cnt=0
for i in range(N//R+1):
    for j in range((N-i*R)//G+1):
        for k in range((N-i*R-j*G)//B+1):
            if R*i+G*j+B*k==N:
                cnt+=1
print(cnt)

Fixed submission:
R,G,B,N=map(int,input().split())
cnt=0
for i in range(N//R+1):
    for j in range((N-i*R)//G+1):
            if i*R + j*G +((N-i*R-j*G)//B)*B==N:
                cnt+=1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
R,G,B,N=map(int,input().split())
cnt=0
for i in range(N//R+1):
    for j in range((N-i*R)//G+1):
        for k in range((N-i*R-j*G)//B+1):
            if R*i+G*j+B*k==N:
                cnt+=1
print(cnt)

Fixed submission:
R,G,B,N=map(int,input().split())
cnt=0
for i in range(N//R+1):
    for j in range((N-i*R)//G+1):
            if i*R + j*G +((N-i*R-j*G)//B)*B==N:
                cnt+=1
print(cnt)
Buggy python submission (verdict: Runtime Error):
from math import gcd
N=int(input())
A=list(map(int,input().split()))
left,right=[0],[0]
[(left.append(gcd(left[i],A[i])),right.append(gcd(right[i],A[N-i-1]))) for i in range(N-1)]
right.reverse()
print(max([gcd(a,b) for a,b in zip(left,right)]))

Fixed submission:
N = int(input())
A = list(map(int,input().split()))

def gcd(x,y):
    if y > x:
        x,y = y,x
    if y == 0:
        return x
    if x%y == 0:
        return y
    else:
        return gcd(x%y,y)

L = [0]
for i in range(N):
    L.append(gcd(L[i],A[i]))
R = [0]
for i in range(N):
    R.append(gcd(R[i],A[-(i+1)]))

print(max([gcd(L[i],R[N-i-1]) for i in range(N)]))

Buggy python submission (verdict: Runtime Error):
from math import gcd
N=int(input())
A=list(map(int,input().split()))
left,right=[0],[0]
[(left.append(gcd(left[i],A[i])),right.append(gcd(right[i],A[N-i-1]))) for i in range(N-1)]
right.reverse()
max([gcd(a,b) for a,b in zip(left,right)])

Fixed submission:
N = int(input())
A = list(map(int,input().split()))

def gcd(x,y):
    if y > x:
        x,y = y,x
    if y == 0:
        return x
    if x%y == 0:
        return y
    else:
        return gcd(x%y,y)

L = [0]
for i in range(N):
    L.append(gcd(L[i],A[i]))
R = [0]
for i in range(N):
    R.append(gcd(R[i],A[-(i+1)]))

print(max([gcd(L[i],R[N-i-1]) for i in range(N)]))
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n=int(input())
s=input()
l=np.array([0])
r=np.array([0])
for i in range(n):
    if s[i]=="#":
        l=np.append(l,l[i]+1)
    else:
        l=np.append(l,l[i])
    if s[n-i-1]==".":
        r=np.append(r,r[i]+1)
    else:
        r=np.append(r,r[i])
r=r[::-1]
print(min(l+r))

Fixed submission:
n=int(input())
s=input()
l=[0]
r=[0]
for i in range(n):
    if s[i]=="#":
        l.append(l[i]+1)
    else:
        l.append(l[i])
    if s[n-i-1]==".":
        r.append(r[i]+1)
    else:
        r.append(r[i])
r.reverse()
print(min([l[i]+r[i] for i in range(n+1)]))
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    a = sorted([int(input()) for _ in [0]*n])
    dp = [1]
    DP = [1]
    s = 1
    mod = 998244353
    for i in a:
        dp2 = dp+([0]*i)
        DP2 = DP+([0]*i)
        for j in range(s):
            dp2[i+j] = (dp2[i+j]+2*dp[j]) % mod
            DP2[i+j] = (DP2[i+j]+DP[j]) % mod
        dp = dp2
        DP = DP2
        s += i
    ALL = sum(dp)
    A = sum(dp[(s-1)//2+1:])
    if s % 2 == 1:
        B = DP[s//2]
    else:
        B = 0
    print((3*A-2*ALL+3*B) % mod)


main()


Fixed submission:
def main():
    n = int(input())
    a = sorted([int(input()) for _ in [0]*n])
    dp = [1]
    DP = [1]
    s = 1
    mod = 998244353
    for i in a:
        dp2 = dp+([0]*i)
        DP2 = DP+([0]*i)
        for j in range(s):
            dp2[i+j] = (dp2[i+j]+2*dp[j]) % mod
            DP2[i+j] = (DP2[i+j]+DP[j]) % mod
        dp = dp2
        DP = DP2
        s += i
    ALL = sum(dp)
    A = sum(dp[(s-1)//2+1:])
    if s % 2 == 1:
        B = DP[s//2]
    else:
        B = 0
    print((3*A-2*ALL+3*B) % mod)


main()
Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().split())
S=input()+"2"
_list=[]
if S[0]=="1":
    _list=[]
else:
    _list=[0]
cnt=1
for i in range(N):
    if S[i]==S[i+1]:
        cnt+=1
    else:
        _list.append(cnt)
        cnt=1
if S[-2]=="0":
    _list.append(0)
import itertools
__list=[0]+list(itertools.accumulate(_list))
C=len(__list)
K=min(C,K)
if C//2<=K:
    print(_list[0])
else:
    print(max([__list[2*i+2*K+1]-__list[2*i] for i in range((C+1)//2-K)]))


Fixed submission:
A,B = list(map(int,input().split()))
if A != B:
    ans = 2 * max(A,B) - 1
else:
    ans = 2 * A
print(ans)
Buggy python submission (verdict: Runtime Error):
S=input()
N=len(S)
A=len([_ for i in range(N) if str(i%2)==S[i]])
print(min(A,N-A))

Fixed submission:
S = [int(i) for i in list(input())]
S2 = len([1 for i in range(len(S)) if S[i] == i%2])
ans = min(S2,len(S)-S2)
print(ans)
Buggy python submission (verdict: Runtime Error):
def main():
    n,q=map(int,input().split())
    s=input()
    td=[list(input().split()) for _ in [0]*q]
    l=0
    r=n-1
    for t,d in td[::-1]:
        if d=="R":
            if r<=n-1:
                if s[r]==t:
                    r-=1
            if l>=1:
                if s[l-1]==t:
                    l-=1
        else:
            if l>=0:
                if s[l]==t:
                    l+=1
            if r<=n-2:
                if s[r+1]==t:
                    r+=1
    print(r-l+1)
main()

Fixed submission:
def main():
    n,q=map(int,input().split())
    s=input()
    td=[list(input().split()) for _ in [0]*q]
    l=0
    r=n-1
    for t,d in td[::-1]:
        if d=="R":
            if 0<=r<=n-1:
                if s[r]==t:
                    r-=1
            if n-1>=l>=1:
                if s[l-1]==t:
                    l-=1
        else:
            if n-1>=l>=0:
                if s[l]==t:
                    l+=1
            if 0<=r<=n-2:
                if s[r+1]==t:
                    r+=1
    print(max(r-l+1,0))
main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    mod = 10**9+7
    fact = [1, 1]
    for i in range(2, 300):
        fact.append(fact[-1]*i % mod)

    n, x = map(int, input().split())
    s = sorted(list(map(int, input().split())))[::-1]
    memo = [dict() for _ in [0]*(n+1)]
    memo[0][x] = fact[n]
    for i in range(n):
        inv = pow(n-i, mod-2, mod)
        for j, a in memo[i].items():
            m = a*inv % mod
            for k, l in enumerate(s[i:]):
                memo[k+i+1][j % l] = (memo[k+i+1].get(j % l, 0)+m) % mod
    ans = 0
    for i, j in memo[n].items():
        ans = (ans+i*j) % mod
    print(ans)


main()

Fixed submission:
def main():
    mod = 10**9+7
    fact = [1, 1]
    for i in range(2, 300):
        fact.append(fact[-1]*i % mod)

    n, x = map(int, input().split())
    s = sorted(list(map(int, input().split())))[::-1]
    next_dict = {x: fact[n-1]}
    for i in range(n):
        memo = dict()
        inv = pow(n-i-1, mod-2, mod)
        ss = s[i]
        for j, a in next_dict.items():
            memo[j % ss] = (memo.get(j % ss, 0)+a) % mod
        for j, a in memo.items():
            next_dict[j] = (next_dict.get(j, 0)+a*inv) % mod
    ans = 0
    for i, j in memo.items():
        ans = (ans+i*j) % mod
    print(ans)


main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    mod = 10**9+7
    fact = [1, 1]
    for i in range(2, 300):
        fact.append(fact[-1]*i % mod)

    n, x = map(int, input().split())
    s = sorted(list(map(int, input().split())))[::-1]
    memo = [dict() for _ in [0]*(n+1)]
    memo[0][x] = fact[n]
    for i in range(n):
        inv = pow(n-i, mod-2, mod)
        for j, a in memo[i].items():
            m = a*inv % mod
            for k, l in enumerate(s[i:]):
                memo[k+i+1][j % l] = (memo[k+i+1].get(j % l, 0)+m) % mod
    ans = 0
    for i, j in memo[n].items():
        ans = (ans+i*j) % mod
    print(ans)


main()

Fixed submission:
def main():
    mod = 10**9+7
    fact = [1, 1]
    for i in range(2, 300):
        fact.append(fact[-1]*i % mod)

    n, x = map(int, input().split())
    s = sorted(list(map(int, input().split())))[::-1]
    next_dict = {x: fact[n-1]}
    for i in range(n):
        memo = dict()
        inv = pow(n-i-1, mod-2, mod)
        ss = s[i]
        for j, a in next_dict.items():
            memo[j % ss] = (memo.get(j % ss, 0)+a) % mod
        for j, a in memo.items():
            next_dict[j] = (next_dict.get(j, 0)+a*inv) % mod
    ans = 0
    for i, j in memo.items():
        ans = (ans+i*j) % mod
    print(ans)


main()