def main():
    N, M = map(int, input().split())
    S = list(map(int, input().split())) + [0]
    T = list(map(int, input().split())) + [0]
    dp0 = [[0] * (M + 5) for _ in range(N + 5)]
    dp1 = [[0] * (M + 5) for _ in range(N + 5)]
    MOD = 10 ** 9 + 7
    dp0[0][0] = 1
    for i in range(N + 1):
        for j in range(M + 1):
            dp0[i+1][j] += dp0[i][j]
            dp0[i+1][j] %= MOD
            dp1[i][j] += dp0[i][j]
            dp1[i][j] %= MOD
            dp1[i][j+1] += dp1[i][j]
            dp1[i][j+1] %= MOD
            if S[i] == T[j]:
                dp0[i+1][j+1] += dp1[i][j]
                dp0[i+1][j+1] %= MOD

    ans = dp1[N][M] % MOD
    print(ans)



if __name__ == "__main__":
    main() def main():
    N, M = map(int, input().split())
    S = list(map(int, input().split())) + [0]
    T = list(map(int, input().split())) + [0]
    dp0 = [[0] * (M + 5) for _ in range(N + 5)]
    dp1 = [[0] * (M + 5) for _ in range(N + 5)]
    MOD = 10 ** 9 + 7
    dp0[0][0] = 1
    for i in range(N + 1):
        for j in range(M + 1):
            dp0[i+1][j] += dp0[i][j]
            dp0[i+1][j] %= MOD
            dp1[i][j] += dp0[i][j]
            dp1[i][j] %= MOD
            dp1[i][j+1] += dp1[i][j]
            dp1[i][j+1] %= MOD
            if S[i] == T[j]:
                dp0[i+1][j+1] += dp1[i][j]
                dp0[i+1][j+1] %= MOD

    ans = dp1[N][M] % MOD
    print(ans) main N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M S = list(map(int, input().split())) + [0] S list(map(int, input().split())) + [0] list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split [0] 0 T = list(map(int, input().split())) + [0] T list(map(int, input().split())) + [0] list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split [0] 0 [0] * (M + 5) for _ in range(N + 5) for _ in range(N + 5) _ range(N + 5) range N + 5 N 5 for _ in range(N + 5) [0] * (M + 5) [0] 0 M + 5 M 5 dp0 = [[0] * (M + 5) for _ in range(N + 5)] dp0 [[0] * (M + 5) for _ in range(N + 5)] [0] * (M + 5) for _ in range(N + 5) for _ in range(N + 5) _ range(N + 5) range N + 5 N 5 for _ in range(N + 5) [0] * (M + 5) [0] 0 M + 5 M 5 dp1 = [[0] * (M + 5) for _ in range(N + 5)] dp1 [[0] * (M + 5) for _ in range(N + 5)] MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 dp0[0][0] = 1 dp0[0][0] [0] dp0 0 0 1 for i in range(N + 1):
        for j in range(M + 1):
            dp0[i+1][j] += dp0[i][j]
            dp0[i+1][j] %= MOD
            dp1[i][j] += dp0[i][j]
            dp1[i][j] %= MOD
            dp1[i][j+1] += dp1[i][j]
            dp1[i][j+1] %= MOD
            if S[i] == T[j]:
                dp0[i+1][j+1] += dp1[i][j]
                dp0[i+1][j+1] %= MOD

     i range(N + 1) range N + 1 N 1 for j in range(M + 1):
            dp0[i+1][j] += dp0[i][j]
            dp0[i+1][j] %= MOD
            dp1[i][j] += dp0[i][j]
            dp1[i][j] %= MOD
            dp1[i][j+1] += dp1[i][j]
            dp1[i][j+1] %= MOD
            if S[i] == T[j]:
                dp0[i+1][j+1] += dp1[i][j]
                dp0[i+1][j+1] %= MOD

     j range(M + 1) range M + 1 M 1 dp0[i+1][j] += dp0[i][j] dp0[i+1][j] [i+1] dp0 i+1 i 1 j dp0[i][j] [i] dp0 i j dp0[i+1][j] %= MOD dp0[i+1][j] [i+1] dp0 i+1 i 1 j MOD dp1[i][j] += dp0[i][j] dp1[i][j] [i] dp1 i j dp0[i][j] [i] dp0 i j dp1[i][j] %= MOD dp1[i][j] [i] dp1 i j MOD dp1[i][j+1] += dp1[i][j] dp1[i][j+1] [i] dp1 i j+1 j 1 dp1[i][j] [i] dp1 i j dp1[i][j+1] %= MOD dp1[i][j+1] [i] dp1 i j+1 j 1 MOD if S[i] == T[j]:
                dp0[i+1][j+1] += dp1[i][j]
                dp0[i+1][j+1] %= MOD

     S[i] == T[j] S[i] S i T[j] T j dp0[i+1][j+1] += dp1[i][j] dp0[i+1][j+1] [i+1] dp0 i+1 i 1 j+1 j 1 dp1[i][j] [i] dp1 i j dp0[i+1][j+1] %= MOD dp0[i+1][j+1] [i+1] dp0 i+1 i 1 j+1 j 1 MOD ans = dp1[N][M] % MOD ans dp1[N][M] % MOD dp1[N][M] [N] dp1 N M MOD print(ans) print ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    N, M = map(int, input().split())
    S = list(map(int, input().split())) + [0]
    T = list(map(int, input().split())) + [0]
    dp0 = [[0] * (M + 5) for _ in range(N + 5)]
    dp1 = [[0] * (M + 5) for _ in range(N + 5)]
    MOD = 10 ** 9 + 7
    dp0[0][0] = 1
    for i in range(N + 1):
        for j in range(M + 1):
            dp0[i+1][j] += dp0[i][j]
            dp0[i+1][j] %= MOD
            dp1[i][j] += dp0[i][j]
            dp1[i][j] %= MOD
            dp1[i][j+1] += dp1[i][j]
            dp1[i][j+1] %= MOD
            if S[i] == T[j]:
                dp0[i+1][j+1] += dp1[i][j]
                dp0[i+1][j+1] %= MOD

    ans = dp1[N][M] % MOD
    print(ans) def main():
    N, M = map(int, input().split())
    S = list(map(int, input().split())) + [0]
    T = list(map(int, input().split())) + [0]
    dp0 = [[0] * (M + 5) for _ in range(N + 5)]
    dp1 = [[0] * (M + 5) for _ in range(N + 5)]
    MOD = 10 ** 9 + 7
    dp0[0][0] = 1
    for i in range(N + 1):
        for j in range(M + 1):
            dp0[i+1][j] += dp0[i][j]
            dp0[i+1][j] %= MOD
            dp1[i][j] += dp0[i][j]
            dp1[i][j] %= MOD
            dp1[i][j+1] += dp1[i][j]
            dp1[i][j+1] %= MOD
            if S[i] == T[j]:
                dp0[i+1][j+1] += dp1[i][j]
                dp0[i+1][j+1] %= MOD

    ans = dp1[N][M] % MOD
    print(ans) main
def main():
    N, M = map(int, input().split())
    S = list(map(int, input().split())) + [0]
    T = list(map(int, input().split())) + [0]
    dp0 = [[0] * (M + 5) for _ in range(N + 5)]
    dp1 = [[0] * (M + 5) for _ in range(N + 5)]
    MOD = 10 ** 9 + 7
    dp0[0][0] = 1
    for i in range(N + 1):
        for j in range(M + 1):
            dp0[i+1][j] += dp0[i][j]
            dp0[i+1][j] %= MOD
            dp1[i][j] += dp0[i][j]
            dp1[i][j] %= MOD
            dp1[i][j+1] += dp1[i][j]
            dp1[i][j+1] %= MOD
            if S[i] == T[j]:
                dp0[i+1][j+1] += dp1[i][j]
                dp0[i+1][j+1] %= MOD

    ans = dp1[N][M] % MOD
    print(ans)



if __name__ == "__main__":
    main()