fun main(args: Array<String>) {
    val (n, m) = readLine()!!.split(' ').map { it.toInt() }
    val s = readLine()!!.split(' ').map { it.toInt() }
    val t = readLine()!!.split(' ').map { it.toInt() }

    val dp = Array(s.size + 1) { LongArray(t.size + 1) }

    for (i in (0 until n))
        for (j in (0 until m)) {
            val x = dp[i + 1][j] + dp[i][j + 1] - dp[i][j] +
                    if (s[i] == t[j]) {
                        dp[i][j] + 1
                    } else {
                        0
                    }
            dp[i + 1][j + 1] = x % 1000000007
        }

    println(dp[n][m] + 1)
}