### BUGGY CODE ###

#!/usr/bin/env python3
import sys
from pprint import pprint

MOD = 1000000007  # type: int

def solve(N: int, M: int, S: "List[int]", T: "List[int]"):
    dp = [[0] * (M + 1) for _ in range(N + 1)]
    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):
            dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1]
            if not S[i] == T[j]:
                dp[i + 1][j + 1] -= dp[i][j]
    #pprint(dp)
    ret = dp[N][M] % MOD
    print(ret)
    return

def __solve(n, m, s, t):
    #dp = [[1] * (m + 1)] + [[1] + [0] * m for _ in range(n)]

    dp = [[0] * (m + 1) for _ in range(n + 1)]
    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 + 1] + dp[i + 1][j]
            #else:
            #    dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j] - dp[i][j]
            dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1]
            if not s[i - 1] == t[j - 1]:
                dp[i + 1][j + 1] -= dp[i][j]

    #arr_s = [0] + s
    #arr_t = [0] + t
    #for c in range(1, n + 1):
    #    for r in range(1, m + 1):
    #        if arr_s[c] == arr_t[r]:
    #            dp[c][r] = dp[c-1][r] + dp[c][r-1]
    #        else:
    #            dp[c][r] = dp[c-1][r] + dp[c][r-1] - dp[c-1][r-1]
    #        dp[c][r] %= MOD
    pprint(dp)
    #ret = dp[m][n]
    ret = dp[n][m] % MOD
    print(ret)
    return
 




def _solve(N: int, M: int, S: "List[int]", T: "List[int]"):
    tmp = [0] * (N + M + 1)
    tmp[0] = 1
    for i in range(1, N + M + 1):
        tmp[i] = tmp[i - 1] * 2

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

    ret = 1
    for i in range(1, N + 1):
        for j in range(1, M + 1):
            mx0 = max(dp[i - 1][j][0], dp[i][j - 1][0])
            mx1 = max(dp[i - 1][j][1], dp[i][j - 1][1])
            if S[i - 1] == T[j - 1]:
                dp[i][j] = [dp[i - 1][j - 1][0] + 1, dp[i - 1][j - 1][1] + mx1]
                #dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1] * 1
                #dp[i][j] = 2 ** dp[i - 1][j - 1]
                #ret += 2 ** (dp[i][j] - 1)
                #print(i, j, ret)
            else:
                dp[i][j] = [mx0, mx1]
                #dp[i][j] = dp[i][j - 1]
    pprint(dp)
    ##ret = 1
    ##for i in range(1, N + 1):
    ##    for j in range(1, M + 1):
    ##        if S[i - 1] == T[j - 1] and dp[i][j] > 0:
    ##            #ret += tmp[dp[i][j] - 1]
    ##            ret += dp[i][j]
    ##            ret %= MOD
    ##            print(i, j)
    ##            print(tmp[dp[i][j] - 1])
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    S = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    T = [ int(next(tokens)) for _ in range(M) ]  # type: "List[int]"
    solve(N, M, S, T)

if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys
from pprint import pprint

MOD = 1000000007  # type: int

def solve(N: int, M: int, S: "List[int]", T: "List[int]"):
    dp = [[0] * (M + 1) for _ in range(N + 1)]
    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):
            dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1]
            if not S[i] == T[j]:
                dp[i + 1][j + 1] -= dp[i][j]
            dp[i + 1][j + 1] %= MOD
    #pprint(dp)
    ret = dp[N][M]
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    S = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    T = [ int(next(tokens)) for _ in range(M) ]  # type: "List[int]"
    solve(N, M, S, T)

if __name__ == '__main__':
    main()