-- E - "Common Subsequence"
import Data.Array

main :: IO()
main = do
  [n, m] <- map read . words <$> getLine
  ns <- map read . words <$> getLine
  ms <- map read . words <$> getLine
  print $ solve n m ns ms

solve :: Int -> Int -> [Int] -> [Int] -> Int
solve n m ns ms = (sums!(n, m) + 1) `mod` (10^9 + 7)
  where
    na = listArray (1, n) ns
    ma = listArray (1, m) ms
    dp = array ((1, 1), (n, m)) $ map (\p->(p, f p)) [(x, y)| x <- [1..n], y <- [1..m]]
    sums = array ((1, 1), (n, m)) $ map (\p -> (p, g p)) [(x, y)| x <- [1..n], y <- [1..m]]
    f (x, y)
      | na!x /= ma!y = 0
      | x == 1 || y == 1 = 1
      | otherwise = (1 + sums!(x-1, y-1)) `mod` (10^9 + 7)
    g (x, y)
      | x == 1 && y == 1 = dp!(1, 1)
      | x == 1 = (dp!(1, y) + sums!(1, y-1)) `mod` (10^9 + 7)
      | y == 1 = (dp!(x, 1) + sums!(x-1, 1)) `mod` (10^9 + 7)
      | otherwise = (dp!(x, y) + sums!(x-1, y) + sums!(x, y-1) - sums!(x-1, y-1)) `mod` (10^9 + 7)