Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

from itertools import accumulate

def solve():
    N, M = map(int, input().split())
    S = map(int, input().split())
    *T, = map(int, input().split())
    dp = [0]*(M+1)
    for s in S:
        is_s = s.__eq__
        acc = [0]*(M+1)
        for i, t in enumerate(T):
            acc[i+1] = acc[i]+(dp[i] + 1 if is_s(t) else 0)
        *dp, = map(int.__add__, dp, acc)
    res = (dp[-1]+1)%(10**9+7)
    return str(res)

if __name__ == '__main__':
    print(solve())


Fixed submission:
# -*- coding: utf-8 -*-
def solve():
    N, M = map(int, input().split())
    S = map(int, input().split())
    *T, = map(int, input().split())
    dp = [1]*(M+1)
    for s in S:
        is_s = s.__eq__
        dp_ = dp[:]
        acc = 0
        for i, t in enumerate(T):
            if is_s(t):
                acc += dp_[i]
            dp[i+1] = dp_[i+1] + acc
    res = (dp[-1])%(10**9+7)
    return str(res)

if __name__ == '__main__':
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

from itertools import accumulate


def solve():
    N, M = map(int, input().split())
    if N <=M :
        *T, = map(int, input().split())
        S = map(int, input().split())
        dp = [0]*(N+1)
    else:
        S = map(int, input().split())
        *T, = map(int, input().split())
        dp = [0]*(M+1)
    for s in S:
        *dp, = map(int.__add__, dp, accumulate(map(lambda d,t: (d+1 if s.__eq__(t) else 0), [0]+dp, T)))
    res = (dp[-1]+1)%(10**9+7)
    return str(res)

if __name__ == '__main__':
    print(solve())


Fixed submission:
# -*- coding: utf-8 -*-
def solve():
    N, M = map(int, input().split())
    S = map(int, input().split())
    *T, = map(int, input().split())
    dp = [1]*(M+1)
    for s in S:
        is_s = s.__eq__
        dp_ = dp[:]
        acc = 0
        for i, t in enumerate(T):
            if is_s(t):
                acc += dp_[i]
            dp[i+1] = dp_[i+1] + acc
    res = (dp[-1])%(10**9+7)
    return str(res)

if __name__ == '__main__':
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

from itertools import accumulate


def solve():
    N, M = map(int, input().split())
    S = map(int, input().split())
    *T, = map(int, input().split())
    dp = [0]*(M+1)
    for s in S:
        *dp, = map(int.__add__, dp, accumulate(map(lambda d,t: (d+1 if s.__eq__(t) else 0), [0]+dp, T)))
    res = (dp[-1]+1)%(10**9+7)
    return str(res)

if __name__ == '__main__':
    print(solve())


Fixed submission:
# -*- coding: utf-8 -*-
def solve():
    N, M = map(int, input().split())
    S = map(int, input().split())
    *T, = map(int, input().split())
    dp = [1]*(M+1)
    for s in S:
        is_s = s.__eq__
        dp_ = dp[:]
        acc = 0
        for i, t in enumerate(T):
            if is_s(t):
                acc += dp_[i]
            dp[i+1] = dp_[i+1] + acc
    res = (dp[-1])%(10**9+7)
    return str(res)

if __name__ == '__main__':
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

from itertools import accumulate


def solve():
    N, M = map(int, input().split())
    S = map(int, input().split())
    *T, = map(int, input().split())
    dp = [0]*(M+1)
    for s in S:
        *acc, = accumulate(map(lambda d,t: (d+1 if s.__eq__(t) else 0), dp, T))
        *dp, = map(int.__add__, dp, [0]+acc)
    res = (dp[-1]+1)%(10**9+7)
    return str(res)

if __name__ == '__main__':
    print(solve())


Fixed submission:
# -*- coding: utf-8 -*-
def solve():
    N, M = map(int, input().split())
    S = map(int, input().split())
    *T, = map(int, input().split())
    dp = [1]*(M+1)
    for s in S:
        is_s = s.__eq__
        dp_ = dp[:]
        acc = 0
        for i, t in enumerate(T):
            if is_s(t):
                acc += dp_[i]
            dp[i+1] = dp_[i+1] + acc
    res = (dp[-1])%(10**9+7)
    return str(res)

if __name__ == '__main__':
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

def solve():
    N, M = map(int, input().split())
    S = map(int, input().split())
    *T, = map(int, input().split())
    dp = [0]*(M+1)
    for s in S:
        dp_ = dp[:]
        for i, t in enumerate(T):
            dp[i+1] = dp_[i+1] + dp[i] - dp_[i] + (dp_[i]+1 if t==s else 0)
    res = (dp[-1]+1)%(10**9+7)
    return str(res)

if __name__ == '__main__':
    print(solve())


Fixed submission:
# -*- coding: utf-8 -*-
def solve():
    N, M = map(int, input().split())
    S = map(int, input().split())
    *T, = map(int, input().split())
    dp = [1]*(M+1)
    for s in S:
        is_s = s.__eq__
        dp_ = dp[:]
        acc = 0
        for i, t in enumerate(T):
            if is_s(t):
                acc += dp_[i]
            dp[i+1] = dp_[i+1] + acc
    res = (dp[-1])%(10**9+7)
    return str(res)

if __name__ == '__main__':
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

def solve():
    N, M = map(int, input().split())
    S = map(int, input().split())
    *T, = map(int, input().split())
    dp = [[[0,0,0] for _ in range(M+1)] for _ in range(2)]
    k = 0
    for s in S:
        k ^= 1
        sum__ = 0
        for i, t in enumerate(T):
            sum_ = sum(dp[k^1][i+1])%(10**9+7)
            dp[k][i+1] = [sum_, sum(dp[k][i][1:])%(10**9+7), sum__ + 1 if t == s else 0]
            sum__ = sum_
    res = (sum(dp[k][-1]) + 1)%(10**9+7)

    return str(res)

if __name__ == '__main__':
    print(solve())


Fixed submission:
# -*- coding: utf-8 -*-
def solve():
    N, M = map(int, input().split())
    S = map(int, input().split())
    *T, = map(int, input().split())
    dp = [1]*(M+1)
    for s in S:
        is_s = s.__eq__
        dp_ = dp[:]
        acc = 0
        for i, t in enumerate(T):
            if is_s(t):
                acc += dp_[i]
            dp[i+1] = dp_[i+1] + acc
    res = (dp[-1])%(10**9+7)
    return str(res)

if __name__ == '__main__':
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

def solve():
    N, M = map(int, input().split())
    *S, = map(int, input().split())
    *T, = map(int, input().split())
    dp = [[[0]*3 for _ in range(M+1)] for _ in range(2)]
    k,l = 0,1
    for j, s in enumerate(S, start=1):
        k,l = l,k
        for i, t in enumerate(T, start=1):
            dp[k][i] = [sum(dp[l][i]), sum(dp[k][i-1][1:]), sum(dp[l][i-1]) + 1 if t == s else 0]
    res = (sum(dp[N%2][-1]) + 1)%(10**9+7)

    return str(res)

if __name__ == '__main__':
    print(solve())


Fixed submission:
# -*- coding: utf-8 -*-
def solve():
    N, M = map(int, input().split())
    S = map(int, input().split())
    *T, = map(int, input().split())
    dp = [1]*(M+1)
    for s in S:
        is_s = s.__eq__
        dp_ = dp[:]
        acc = 0
        for i, t in enumerate(T):
            if is_s(t):
                acc += dp_[i]
            dp[i+1] = dp_[i+1] + acc
    res = (dp[-1])%(10**9+7)
    return str(res)

if __name__ == '__main__':
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

def solve():
    N, M = map(int, input().split())
    *S, = map(int, input().split())
    *T, = map(int, input().split())
    dph = [[0]*(M+1), [0]*(M+1)]
    dpv = [[0]*(M+1), [0]*(M+1)]
    dpr = [[0]*(M+1), [0]*(M+1)]
    for j, s in enumerate(S, start=1):
        for i, t in enumerate(T, start=1):
            dph[j%2][i] = dph[(j-1)%2][i] + dpv[(j-1)%2][i] + dpr[(j-1)%2][i]
            dpv[j%2][i] = dpv[j%2][i-1] + dpr[j%2][i-1]
            dpr[j%2][i] = dph[(j-1)%2][i-1] + dpv[(j-1)%2][i-1] + dpr[(j-1)%2][i-1] + 1 if t == s else 0
    res = (dph[N%2][-1] + dpv[N%2][-1] + dpr[N%2][-1] + 1)%(10**9+7)


    return str(res)

if __name__ == '__main__':
    print(solve())


Fixed submission:
# -*- coding: utf-8 -*-
def solve():
    N, M = map(int, input().split())
    S = map(int, input().split())
    *T, = map(int, input().split())
    dp = [1]*(M+1)
    for s in S:
        is_s = s.__eq__
        dp_ = dp[:]
        acc = 0
        for i, t in enumerate(T):
            if is_s(t):
                acc += dp_[i]
            dp[i+1] = dp_[i+1] + acc
    res = (dp[-1])%(10**9+7)
    return str(res)

if __name__ == '__main__':
    print(solve())