
"""
問題: https://atcoder.jp/contests/abc130/tasks/abc130_e
解説: https://drken1215.hatenablog.com/entry/2019/06/21/230200

共通部分列を数え上げる問題．
"""


def main1():
    """LCS (Longest Common Sequence)を改良する．

    LCSでは最長部分列の長さを求めるが，今回は個数を求める．
    重複して数えている分を引けば良い．
    """
    N, M = map(int, input().split())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))

    dp = [[0] * (M + 1) for _ in range(N + 1)]
    MOD = 10 ** 9 + 7

    for i in range(N + 1):
        dp[i][0] = 1

    for j in range(M + 1):
        dp[0][j] = 1

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

    print(dp[-1][-1])


def main2():
    N, M = map(int, input().split())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))

    dp = [[0] * (M + 1) for _ in range(N + 1)]
    sdp = [[0] * (M + 2) for _ in range(N + 2)]
    MOD = 10 ** 9 + 7

    dp[0][0] = 1
    sdp[1][1] = 1

    for i in range(N + 1):
        for j in range(M + 1):
            if i == 0 and j == 0:
                continue
            if i >= 1 and j >= 1 and S[i - 1] == T[j - 1]:
                dp[i][j] = sdp[i][j]
            sdp[i + 1][j + 1] = (sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j]
                                 + dp[i][j])
            sdp[i + 1][j + 1] %= MOD

    print(sdp[-1][-1])


if __name__ == "__main__":
    # main1()
    main2() """
問題: https://atcoder.jp/contests/abc130/tasks/abc130_e
解説: https://drken1215.hatenablog.com/entry/2019/06/21/230200

共通部分列を数え上げる問題．
""" def main1():
    """LCS (Longest Common Sequence)を改良する．

    LCSでは最長部分列の長さを求めるが，今回は個数を求める．
    重複して数えている分を引けば良い．
    """
    N, M = map(int, input().split())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))

    dp = [[0] * (M + 1) for _ in range(N + 1)]
    MOD = 10 ** 9 + 7

    for i in range(N + 1):
        dp[i][0] = 1

    for j in range(M + 1):
        dp[0][j] = 1

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

    print(dp[-1][-1]) main1 """LCS (Longest Common Sequence)を改良する．

    LCSでは最長部分列の長さを求めるが，今回は個数を求める．
    重複して数えている分を引けば良い．
    """ 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())) S list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split T = list(map(int, input().split())) T list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split [0] * (M + 1) for _ in range(N + 1) for _ in range(N + 1) _ range(N + 1) range N + 1 N 1 for _ in range(N + 1) [0] * (M + 1) [0] 0 M + 1 M 1 dp = [[0] * (M + 1) for _ in range(N + 1)] dp [[0] * (M + 1) for _ in range(N + 1)] MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 for i in range(N + 1):
        dp[i][0] = 1

     i range(N + 1) range N + 1 N 1 dp[i][0] = 1 dp[i][0] [i] dp i 0 1 for j in range(M + 1):
        dp[0][j] = 1

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

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

     j range(M) range M if S[i] == T[j]:
                dp[i + 1][j + 1] += dp[i][j]
             S[i] == T[j] S[i] S i T[j] T j dp[i + 1][j + 1] += dp[i][j] dp[i + 1][j + 1] [i + 1] dp i + 1 i 1 j + 1 j 1 dp[i][j] [i] dp i j dp[i + 1][j + 1] += dp[i][j + 1] + dp[i + 1][j] - dp[i][j] dp[i + 1][j + 1] [i + 1] dp i + 1 i 1 j + 1 j 1 dp[i][j + 1] + dp[i + 1][j] - dp[i][j] dp[i][j + 1] + dp[i + 1][j] dp[i][j + 1] [i] dp i j + 1 j 1 dp[i + 1][j] [i + 1] dp i + 1 i 1 j dp[i][j] [i] dp i j dp[i + 1][j + 1] %= MOD dp[i + 1][j + 1] [i + 1] dp i + 1 i 1 j + 1 j 1 MOD print(dp[-1][-1]) print dp[-1][-1] [-1] dp -1 -1 def main2():
    N, M = map(int, input().split())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))

    dp = [[0] * (M + 1) for _ in range(N + 1)]
    sdp = [[0] * (M + 2) for _ in range(N + 2)]
    MOD = 10 ** 9 + 7

    dp[0][0] = 1
    sdp[1][1] = 1

    for i in range(N + 1):
        for j in range(M + 1):
            if i == 0 and j == 0:
                continue
            if i >= 1 and j >= 1 and S[i - 1] == T[j - 1]:
                dp[i][j] = sdp[i][j]
            sdp[i + 1][j + 1] = (sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j]
                                 + dp[i][j])
            sdp[i + 1][j + 1] %= MOD

    print(sdp[-1][-1]) main2 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())) S list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split T = list(map(int, input().split())) T list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split [0] * (M + 1) for _ in range(N + 1) for _ in range(N + 1) _ range(N + 1) range N + 1 N 1 for _ in range(N + 1) [0] * (M + 1) [0] 0 M + 1 M 1 dp = [[0] * (M + 1) for _ in range(N + 1)] dp [[0] * (M + 1) for _ in range(N + 1)] [0] * (M + 2) for _ in range(N + 2) for _ in range(N + 2) _ range(N + 2) range N + 2 N 2 for _ in range(N + 2) [0] * (M + 2) [0] 0 M + 2 M 2 sdp = [[0] * (M + 2) for _ in range(N + 2)] sdp [[0] * (M + 2) for _ in range(N + 2)] MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 dp[0][0] = 1 dp[0][0] [0] dp 0 0 1 sdp[1][1] = 1 sdp[1][1] [1] sdp 1 1 1 for i in range(N + 1):
        for j in range(M + 1):
            if i == 0 and j == 0:
                continue
            if i >= 1 and j >= 1 and S[i - 1] == T[j - 1]:
                dp[i][j] = sdp[i][j]
            sdp[i + 1][j + 1] = (sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j]
                                 + dp[i][j])
            sdp[i + 1][j + 1] %= MOD

     i range(N + 1) range N + 1 N 1 for j in range(M + 1):
            if i == 0 and j == 0:
                continue
            if i >= 1 and j >= 1 and S[i - 1] == T[j - 1]:
                dp[i][j] = sdp[i][j]
            sdp[i + 1][j + 1] = (sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j]
                                 + dp[i][j])
            sdp[i + 1][j + 1] %= MOD

     j range(M + 1) range M + 1 M 1 if i == 0 and j == 0:
                continue
             i == 0 and j == 0 i == 0 i 0 j == 0 j 0 continue if i >= 1 and j >= 1 and S[i - 1] == T[j - 1]:
                dp[i][j] = sdp[i][j]
             i >= 1 and j >= 1 and S[i - 1] == T[j - 1] i >= 1 and j >= 1 i >= 1 i 1 j >= 1 j 1 S[i - 1] == T[j - 1] S[i - 1] S i - 1 i 1 T[j - 1] T j - 1 j 1 dp[i][j] = sdp[i][j] dp[i][j] [i] dp i j sdp[i][j] [i] sdp i j sdp[i + 1][j + 1] = (sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j]
                                 + dp[i][j]) sdp[i + 1][j + 1] [i + 1] sdp i + 1 i 1 j + 1 j 1 sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j]
                                 + dp[i][j] sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j] sdp[i + 1][j] + sdp[i][j + 1] sdp[i + 1][j] [i + 1] sdp i + 1 i 1 j sdp[i][j + 1] [i] sdp i j + 1 j 1 sdp[i][j] [i] sdp i j dp[i][j] [i] dp i j sdp[i + 1][j + 1] %= MOD sdp[i + 1][j + 1] [i + 1] sdp i + 1 i 1 j + 1 j 1 MOD print(sdp[-1][-1]) print sdp[-1][-1] [-1] sdp -1 -1 if __name__ == "__main__":
    # main1()
    main2() __name__ == "__main__" __name__ "__main__" main2() main2 def main2():
    N, M = map(int, input().split())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))

    dp = [[0] * (M + 1) for _ in range(N + 1)]
    sdp = [[0] * (M + 2) for _ in range(N + 2)]
    MOD = 10 ** 9 + 7

    dp[0][0] = 1
    sdp[1][1] = 1

    for i in range(N + 1):
        for j in range(M + 1):
            if i == 0 and j == 0:
                continue
            if i >= 1 and j >= 1 and S[i - 1] == T[j - 1]:
                dp[i][j] = sdp[i][j]
            sdp[i + 1][j + 1] = (sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j]
                                 + dp[i][j])
            sdp[i + 1][j + 1] %= MOD

    print(sdp[-1][-1]) def main2():
    N, M = map(int, input().split())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))

    dp = [[0] * (M + 1) for _ in range(N + 1)]
    sdp = [[0] * (M + 2) for _ in range(N + 2)]
    MOD = 10 ** 9 + 7

    dp[0][0] = 1
    sdp[1][1] = 1

    for i in range(N + 1):
        for j in range(M + 1):
            if i == 0 and j == 0:
                continue
            if i >= 1 and j >= 1 and S[i - 1] == T[j - 1]:
                dp[i][j] = sdp[i][j]
            sdp[i + 1][j + 1] = (sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j]
                                 + dp[i][j])
            sdp[i + 1][j + 1] %= MOD

    print(sdp[-1][-1]) main2 def main1():
    """LCS (Longest Common Sequence)を改良する．

    LCSでは最長部分列の長さを求めるが，今回は個数を求める．
    重複して数えている分を引けば良い．
    """
    N, M = map(int, input().split())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))

    dp = [[0] * (M + 1) for _ in range(N + 1)]
    MOD = 10 ** 9 + 7

    for i in range(N + 1):
        dp[i][0] = 1

    for j in range(M + 1):
        dp[0][j] = 1

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

    print(dp[-1][-1]) def main1():
    """LCS (Longest Common Sequence)を改良する．

    LCSでは最長部分列の長さを求めるが，今回は個数を求める．
    重複して数えている分を引けば良い．
    """
    N, M = map(int, input().split())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))

    dp = [[0] * (M + 1) for _ in range(N + 1)]
    MOD = 10 ** 9 + 7

    for i in range(N + 1):
        dp[i][0] = 1

    for j in range(M + 1):
        dp[0][j] = 1

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

    print(dp[-1][-1]) main1

"""
問題: https://atcoder.jp/contests/abc130/tasks/abc130_e
解説: https://drken1215.hatenablog.com/entry/2019/06/21/230200

共通部分列を数え上げる問題．
"""


def main1():
    """LCS (Longest Common Sequence)を改良する．

    LCSでは最長部分列の長さを求めるが，今回は個数を求める．
    重複して数えている分を引けば良い．
    """
    N, M = map(int, input().split())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))

    dp = [[0] * (M + 1) for _ in range(N + 1)]
    MOD = 10 ** 9 + 7

    for i in range(N + 1):
        dp[i][0] = 1

    for j in range(M + 1):
        dp[0][j] = 1

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

    print(dp[-1][-1])


def main2():
    N, M = map(int, input().split())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))

    dp = [[0] * (M + 1) for _ in range(N + 1)]
    sdp = [[0] * (M + 2) for _ in range(N + 2)]
    MOD = 10 ** 9 + 7

    dp[0][0] = 1
    sdp[1][1] = 1

    for i in range(N + 1):
        for j in range(M + 1):
            if i == 0 and j == 0:
                continue
            if i >= 1 and j >= 1 and S[i - 1] == T[j - 1]:
                dp[i][j] = sdp[i][j]
            sdp[i + 1][j + 1] = (sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j]
                                 + dp[i][j])
            sdp[i + 1][j + 1] %= MOD

    print(sdp[-1][-1])


if __name__ == "__main__":
    # main1()
    main2()
