import scala.annotation.tailrec
import scala.collection.mutable
import scala.io.StdIn._
object Main extends App {

  val mod = 1000000007L

  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val sList = readLine.trim.split(' ').map(_.toInt)
  val tList = readLine.trim.split(' ').map(_.toInt)
  val memo = mutable.Map[(Int, Int), Long]()
  println(
    search(0, 0)
  )

  def search(s: Int, t: Int): Long = {
    memo.getOrElseUpdate((s, t), {
      if (s >= n || t >= m) 1L
      else if (sList(s) == tList(t)) (search(s + 1, t) + search(s, t + 1)) % mod
      else (search(s + 1, t) + search(s, t + 1) - search(s + 1, t + 1)) % mod
    })
  }



  implicit class Scope[T](val receiver: T) extends AnyVal {
    def let[A](@inline func: T ⇒ A): A = func(receiver)
    def tap(@inline func: T ⇒ Unit): T = {
      func(receiver)
      receiver
    }
  }
}