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)]

    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] + 1) % MOD
                ans = (ans + dp[i + 1][j + 1]) % MOD
            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

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)]

    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] + 1) % MOD
                ans = (ans + dp[i + 1][j + 1]) % MOD
            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 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)] 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] + 1) % MOD
                ans = (ans + dp[i + 1][j + 1]) % MOD
            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] + 1) % MOD
                ans = (ans + dp[i + 1][j + 1]) % MOD
            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] + 1) % MOD
                ans = (ans + dp[i + 1][j + 1]) % MOD
             S[i] == T[j] S[i] S i T[j] T j dp[i + 1][j + 1] = (total[i][j] + 1) % MOD dp[i + 1][j + 1] [i + 1] dp i + 1 i 1 j + 1 j 1 (total[i][j] + 1) % MOD total[i][j] + 1 total[i][j] [i] total i j 1 MOD ans = (ans + dp[i + 1][j + 1]) % MOD ans (ans + dp[i + 1][j + 1]) % MOD ans + dp[i + 1][j + 1] ans dp[i + 1][j + 1] [i + 1] dp i + 1 i 1 j + 1 j 1 MOD 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 ans 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 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)]

    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] + 1) % MOD
                ans = (ans + dp[i + 1][j + 1]) % MOD
            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 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)]

    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] + 1) % MOD
                ans = (ans + dp[i + 1][j + 1]) % MOD
            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 e_common_subsequence M = [int(i) for i in input().split()] [int(i) for i in input().split()] M T = [int(i) for i in input().split()] [int(i) for i in input().split()] T N, M = [int(i) for i in input().split()] [int(i) for i in input().split()] N 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)]

    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] + 1) % MOD
                ans = (ans + dp[i + 1][j + 1]) % MOD
            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

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))