N, M = gets.split.map(&:to_i)
S = gets.split.map(&:to_i)
T = gets.split.map(&:to_i)

dp = Array.new(N+2){Array.new(M+2, 0)}
dp[0][0] = 1
MOD = 10**9+7
(N+1).times do |i|
    (M+1).times do |j|
        next if i+j == 0
        if S[i-1] == T[j-1]
            dp[i][j] = (dp[i-1][j] + dp[i][j-1]) % MOD
        else
            dp[i][j] = (dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1]) % MOD
        end
    end
end
puts dp[N][M]