when NimMajor == 0 and NimMinor <= 18: import future
else: import sugar

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from sequtils import map, mapIt, toSeq
from strutils import split, parseInt, parseFloat, parseBool, parseEnum
when NimMajor == 0 and NimMinor >= 14: from strutils import parseUint

import macros
# headaux macro -- https://stackoverflow.com/questions/31948131/unpack-multiple-variables-from-sequence
# unpack (toTuple) macro  -- https://chy72.hatenablog.com/entry/2017/12/16/214708
macro unpack*(input: array|seq|tuple; count: static[int]): untyped =
  var t = genSym()
  result = quote do: (let `t` = `input`; ())
  when NimMajor == 0 and NimMinor <= 17:
    for i in 0..<count: result[0][1].add quote do: `t`[`i`]
  else:
    for i in 0..<count: result[1].add quote do: `t`[`i`]

template input*(typ: static[typedesc]): seq =
  when typ is char:
    toSeq(stdin.readLine.items)
  elif typ is string:
    stdin.readLine.split
  else:
    stdin.readLine.split.map(`parse typ`)

# 2引数の input() は通常こちらが呼ばれる
template input*(typ: static[typedesc]; count: static[int]): untyped =
  input(typ).unpack(count)

# input(typ, [some integer].Positive) と指定しないとこちらは呼ばれない
template input*(typ: static[typedesc]; lineCount: Positive): untyped =
  (1..lineCount).mapIt(input(typ))

template input*(typ: static[typedesc]; count: static[int] = 1; lineCount: Positive): untyped =
  (1..lineCount).mapIt(input(typ, count))

macro inputs*(body: untyped): untyped =
  result = newNimNode(nnkStmtList)

  var letSection = newNimNode(nnkLetSection)

  var vars = newSeq[NimNode](body.len)
  for i, _ in vars:
    if body[i][0].len > 1: # (var1, var2, ...): int | float | string
      vars[i] = newNimNode(nnkVarTuple)
      for ident in body[i][0]:
        vars[i].add(ident)
      vars[i].add(newEmptyNode())
      vars[i].add(newNimNode(nnkCall)
                    .add(ident("input"))
                    .add(body[i][1][0])
                    .add(newLit(body[i][0].len))
                 )
    elif body[i][1][0].len == 0: # variable: int | float | string
      vars[i] = newIdentDefs(
        body[i][0],
        newEmptyNode(),
        newNimNode(nnkCall)
          .add(ident("input"))
          .add(body[i][1][0])
          .add(newLit(1))
      )
    elif body[i][1][0][0].len > 0: # variable: tuple[field1, field2, ...: T]
      var tupleIdents = newNimNode(nnkIdentDefs)
      for ident in body[i][1][0][0]:
        tupleIdents.add(ident)
      let tuplety = newNimNode(nnkTupleTy).add(tupleIdents)

      vars[i] = newIdentDefs(
        body[i][0],
        tuplety,
        newNimNode(nnkCall)
          .add(ident("input"))
          .add(body[i][1][0][0][^2])
          .add(newLit(body[i][1][0][0].len - 2))
      )
    elif body[i][1][0][0].len == 0: # seq
      if body[i][1][0].len == 2: # variable: seq[int | float | string | char]
        vars[i] = newIdentDefs(
          body[i][0],
          newEmptyNode(),
          newNimNode(nnkCall)
            .add(ident("input"))
            .add(body[i][1][0][1])
        )
      elif body[i][1][0].len == 3 and body[i][1][0][2].len == 0: # variable: seq[N, int | float | string | char]
        vars[i] = newIdentDefs(
          body[i][0],
          newEmptyNode(),
          newNimNode(nnkCall)
            .add(ident("input"))
            .add(body[i][1][0][2])
            .add(newLit(1))
            .add(body[i][1][0][1])
        )
      elif body[i][1][0][2].len == 2: # variable: seq[lineCount, seq[int | float | string | char]]
        vars[i] = newIdentDefs(
          body[i][0],
          newEmptyNode(),
          newNimNode(nnkCall)
            .add(ident("input"))
            .add(body[i][1][0][2][1])
            .add(newNimNode(nnkDotExpr)
              .add(body[i][1][0][1])
              .add(ident("Positive"))
            )
        )
      else: # variable: seq[lineCount, tuple[field1, field2, ...: T]]
        var tupleIdents = newNimNode(nnkIdentDefs)
        for ident in body[i][1][0][2][0]:
          tupleIdents.add(ident)
        let tuplety = newNimNode(nnkTupleTy).add(tupleIdents)

        vars[i] = newIdentDefs(
          body[i][0],
          newNimNode(nnkBracketExpr).add(body[i][1][0][0]).add(tuplety),
          newNimNode(nnkCall)
            .add(ident("input"))
            .add(body[i][1][0][2][0][^2])
            .add(newLit(body[i][1][0][2][0].len - 2))
            .add(body[i][1][0][1])
        )
    else:
      discard

  for `var` in vars:
    letSection.add(`var`)

  result.add(letSection)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
proc `//=`*(x: var SomeInteger; y: SomeInteger) = x = x div y
proc `%=`*(x: var SomeInteger; y: SomeInteger) = x = x mod y
proc `<?=`*[T](x: var T; y: T) = x = min(x, y)
proc `>?=`*[T](x: var T; y: T) = x = max(x, y)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from sequtils import concat

template flatten*[T](s: seq[T]): untyped =
  when T is seq: flatten(s.concat)
  else: s
proc flatten*[T](s: seq[T]; recLevel: static[Natural]): auto =
  when recLevel == 0: s
  elif T is seq: flatten(s.concat, recLevel - 1)
  else: s

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
proc indexed*[T](s: openArray[T]): seq[tuple[i: int, v: T]] =
  (0..s.high).mapIt((it, s[it]))

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
template countIt*[T](a: openArray[T]; pred: untyped): int =
  var result = 0
  for it {.inject.} in items(a):
    if pred: result.inc
  result

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
template stderrEcho*(x: varargs[string, `$`]) =
  for v in x:
    stderr.write(v)
  stderr.writeLine ""

template stderrEcho*[T](x: seq[seq[T]]) =
  for v in x: stderrEcho v

template stderrEcho*(x: seq[string]) =
  for v in x: stderrEcho v

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from sequtils import newSeqWith
from math import `^`

const MOD = 10^9 + 7

inputs:
  (N, M): int
  S: seq[int]
  T: seq[int]

var dp = newSeqWith(N + 1, newSeq[int](M + 1))
dp[0][0] = 1
for i, s in S: dp[i + 1][0] = dp[i][0]
for j, t in T: dp[0][j + 1] = dp[0][j]

for i, s in S:
  for j, t in T:
    if s == t:
      dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1]
    else:
      dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1] - dp[i][j]
    dp[i + 1][j + 1] %= MOD

echo dp[N][M]
