MOD = 10 ** 9 + 7

class Array
  def lcs(other)
    n = size
    m = other.size
    dp0 = Array.new(n + 3) { Array.new(m + 3, 0) }
    dp1 = Array.new(n + 3) { Array.new(m + 3, 0) }
    dp0[0][0] = 1

    (0..n+1).each do |i|
      s = self[i]

      (0..m+1).each do |j|
        t = other[j]

        dp0[i + 1][j] += dp0[i][j]
        dp1[i][j] += dp0[i][j]
        dp1[i][j + 1] += dp1[i][j]

        if s == t
          dp0[i + 1][j + 1] += dp1[i][j]
        end

        dp0[i][j] %= MOD
        dp1[i][j] %= MOD
      end
    end

    dp1[n][m] % MOD
  end
end

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

p S.lcs(T)

