def e_common_subsequence(N, M, S, T, MOD=10**9 + 7):
    # dp[i][j]: S[i], T[j] まで考慮したとき、この2つをペアにしたときの場合の数
    dp = [[0] * (M + 1) for _ in range(N + 1)]

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

    ans = 1  # 空の列は常に等しい列である
    for i in range(N):
        for j in range(M):
            if S[i] == T[j]:
                dp[i + 1][j + 1] = (total[i][j] % MOD)
                ans += dp[i + 1][j + 1]
            total[i + 1][j + 1] = (total[i][j + 1] + total[i + 1][j]
                                   - total[i][j] + dp[i + 1][j + 1] + MOD) % MOD
    return ans % MOD

N, M = [int(i) for i in input().split()]
S = [int(i) for i in input().split()]
T = [int(i) for i in input().split()]
print(e_common_subsequence(N, M, S, T)) def e_common_subsequence(N, M, S, T, MOD=10**9 + 7):
    # dp[i][j]: S[i], T[j] まで考慮したとき、この2つをペアにしたときの場合の数
    dp = [[0] * (M + 1) for _ in range(N + 1)]

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

    ans = 1  # 空の列は常に等しい列である
    for i in range(N):
        for j in range(M):
            if S[i] == T[j]:
                dp[i + 1][j + 1] = (total[i][j] % MOD)
                ans += dp[i + 1][j + 1]
            total[i + 1][j + 1] = (total[i][j + 1] + total[i + 1][j]
                                   - total[i][j] + dp[i + 1][j + 1] + MOD) % MOD
    return ans % MOD e_common_subsequence [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 + 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 total = [[0] * (M + 1) for _ in range(N + 1)] total [[0] * (M + 1) for _ in range(N + 1)] for i in range(N + 1):
        total[i][0] = 1
     i range(N + 1) range N + 1 N 1 total[i][0] = 1 total[i][0] [i] total i 0 1 for j in range(M + 1):
        total[0][j] = 1

     j range(M + 1) range M + 1 M 1 total[0][j] = 1 total[0][j] [0] total 0 j 1 ans = 1 ans 1 for i in range(N):
        for j in range(M):
            if S[i] == T[j]:
                dp[i + 1][j + 1] = (total[i][j] % MOD)
                ans += dp[i + 1][j + 1]
            total[i + 1][j + 1] = (total[i][j + 1] + total[i + 1][j]
                                   - total[i][j] + dp[i + 1][j + 1] + MOD) % MOD
     i range(N) range N for j in range(M):
            if S[i] == T[j]:
                dp[i + 1][j + 1] = (total[i][j] % MOD)
                ans += dp[i + 1][j + 1]
            total[i + 1][j + 1] = (total[i][j + 1] + total[i + 1][j]
                                   - total[i][j] + dp[i + 1][j + 1] + MOD) % MOD
     j range(M) range M if S[i] == T[j]:
                dp[i + 1][j + 1] = (total[i][j] % MOD)
                ans += dp[i + 1][j + 1]
             S[i] == T[j] S[i] S i T[j] T j dp[i + 1][j + 1] = (total[i][j] % MOD) dp[i + 1][j + 1] [i + 1] dp i + 1 i 1 j + 1 j 1 total[i][j] % MOD total[i][j] [i] total i j MOD ans += dp[i + 1][j + 1] ans dp[i + 1][j + 1] [i + 1] dp i + 1 i 1 j + 1 j 1 total[i + 1][j + 1] = (total[i][j + 1] + total[i + 1][j]
                                   - total[i][j] + dp[i + 1][j + 1] + MOD) % MOD total[i + 1][j + 1] [i + 1] total i + 1 i 1 j + 1 j 1 (total[i][j + 1] + total[i + 1][j]
                                   - total[i][j] + dp[i + 1][j + 1] + MOD) % MOD total[i][j + 1] + total[i + 1][j]
                                   - total[i][j] + dp[i + 1][j + 1] + MOD total[i][j + 1] + total[i + 1][j]
                                   - total[i][j] + dp[i + 1][j + 1] total[i][j + 1] + total[i + 1][j]
                                   - total[i][j] total[i][j + 1] + total[i + 1][j] total[i][j + 1] [i] total i j + 1 j 1 total[i + 1][j] [i + 1] total i + 1 i 1 j total[i][j] [i] total i j dp[i + 1][j + 1] [i + 1] dp i + 1 i 1 j + 1 j 1 MOD MOD return ans % MOD ans % MOD ans MOD N N M M S S T T MOD=10**9 + 7 MOD 10**9 + 7 10**9 10 9 7 int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i N, M = [int(i) for i in input().split()] N [int(i) for i in input().split()] M int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i S = [int(i) for i in input().split()] S [int(i) for i in input().split()] int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i T = [int(i) for i in input().split()] T [int(i) for i in input().split()] print(e_common_subsequence(N, M, S, T)) print e_common_subsequence(N, M, S, T) e_common_subsequence N M S T T = [int(i) for i in input().split()] [int(i) for i in input().split()] T S = [int(i) for i in input().split()] [int(i) for i in input().split()] S def e_common_subsequence(N, M, S, T, MOD=10**9 + 7):
    # dp[i][j]: S[i], T[j] まで考慮したとき、この2つをペアにしたときの場合の数
    dp = [[0] * (M + 1) for _ in range(N + 1)]

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

    ans = 1  # 空の列は常に等しい列である
    for i in range(N):
        for j in range(M):
            if S[i] == T[j]:
                dp[i + 1][j + 1] = (total[i][j] % MOD)
                ans += dp[i + 1][j + 1]
            total[i + 1][j + 1] = (total[i][j + 1] + total[i + 1][j]
                                   - total[i][j] + dp[i + 1][j + 1] + MOD) % MOD
    return ans % MOD def e_common_subsequence(N, M, S, T, MOD=10**9 + 7):
    # dp[i][j]: S[i], T[j] まで考慮したとき、この2つをペアにしたときの場合の数
    dp = [[0] * (M + 1) for _ in range(N + 1)]

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

    ans = 1  # 空の列は常に等しい列である
    for i in range(N):
        for j in range(M):
            if S[i] == T[j]:
                dp[i + 1][j + 1] = (total[i][j] % MOD)
                ans += dp[i + 1][j + 1]
            total[i + 1][j + 1] = (total[i][j + 1] + total[i + 1][j]
                                   - total[i][j] + dp[i + 1][j + 1] + MOD) % MOD
    return ans % MOD e_common_subsequence N, M = [int(i) for i in input().split()] [int(i) for i in input().split()] N M = [int(i) for i in input().split()] [int(i) for i in input().split()] M
def e_common_subsequence(N, M, S, T, MOD=10**9 + 7):
    # dp[i][j]: S[i], T[j] まで考慮したとき、この2つをペアにしたときの場合の数
    dp = [[0] * (M + 1) for _ in range(N + 1)]

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

    ans = 1  # 空の列は常に等しい列である
    for i in range(N):
        for j in range(M):
            if S[i] == T[j]:
                dp[i + 1][j + 1] = (total[i][j] % MOD)
                ans += dp[i + 1][j + 1]
            total[i + 1][j + 1] = (total[i][j + 1] + total[i + 1][j]
                                   - total[i][j] + dp[i + 1][j + 1] + MOD) % MOD
    return ans % MOD

N, M = [int(i) for i in input().split()]
S = [int(i) for i in input().split()]
T = [int(i) for i in input().split()]
print(e_common_subsequence(N, M, S, T))