import algorithm, future, hashes, macros, math, sequtils, sets, strutils, tables, times, unicode

macro unpack(rhs: seq, cnt: static[int]): auto =
  let t = genSym(); result = quote do:(let `t` = `rhs`;())
  if NimMinor <= 17:
    for i in 0..<cnt: result[0][1].add(quote do:`t`[`i`])
  else:
    for i in 0..<cnt: result[1].add(quote do:`t`[`i`])

template input(T: typedesc, cnt: Natural = 1): untyped =
  let line = stdin.readLine.split(" ")
  when T is int:         line.map(parseInt).unpack(cnt)
  elif T is float:       line.map(parseFloat).unpack(cnt)
  elif T is string:      line.unpack(cnt)
  elif T is char:        line.mapIt(it[0]).unpack(cnt)
  elif T is seq[int]:    line.map(parseint)
  elif T is seq[float]:  line.map(parseFloat)
  elif T is seq[string]: line
  elif T is seq[char]:   line.mapIt(it[0])

proc `%=`(n: var int, m: int)  = n = n mod m
proc `//=`(n: var int, m: int) = n = n div m
proc `<<=`(n: var int, m: int) = n = n shl m
proc `>>=`(n: var int, m: int) = n = n shr m
proc `<?=`(n: var int, m: int) = n = min(n, m)
proc `>?=`(n: var int, m: int) = n = max(n, m)
proc newSeq2[T](n1, n2: Natural): seq[seq[T]] = newSeqWith(n1, newSeq[T](n2))
proc newSeq3[T](n1, n2, n3: Natural): seq[seq[seq[T]]] = newSeqWith(n1, newSeqWith(n2, newSeq[T](n3)))

# -------------------------------------------------- #

const modulus = 10 ^ 9 + 7

var (N, M) = input(int, 2)
var S = @[0] & input(seq[int])
var T = @[0] & input(seq[int])
# dp[i][j]: S[i] = T[j] を末尾とする共通部分列の個数
var dp = newseq2[int](N + 1, M + 1)
var cus = newseq2[int](N + 1, M + 1)
dp[0][0] = 1
var res = 1 # () の分
for i in 1..N:
  for j in 1..M:
    if S[i] == T[j]:
      dp[i][j] = cus[i - 1][j - 1] + 1
      res += dp[i][j]
      res %= modulus
    cus[i][j] = (cus[i - 1][j] + cus[i][j - 1] - cus[i - 1][j - 1] + dp[i][j]) mod modulus
echo res