program common_subsequence
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer :: n, m, s(2000), t(2000), i, j
  integer(8) :: dp(0:2000,0:2000)
  s = 0
  t = 0
  dp = 0_8
  read(*,*) n, m
  read(*,*) s(1:n)
  read(*,*) t(1:m)
  do i = 1, n
    do j = 1, m
      if (s(i).eq.t(j)) then
        dp(i,j) = mod(1_8+dp(i,j-1)+dp(i-1,j),md)
      else
        dp(i,j) = mod(mod(dp(i,j-1)+dp(i-1,j),md)+mod(md-dp(i-1,j-1),md),md)
      end if
    end do
  end do
  write(*,'(i0)') dp(n,m)+1_8
  stop
end program common_subsequence