def main():
  from collections import defaultdict
  import bisect
  n,m = map(int,input().split())
  s = list(map(int,input().split()))
  t = list(map(int,input().split()))
  mod = 10**9+7
  p = max(n,m)
  if n>m:
    t.extend((0,)*(n-m))
  else:
    s.extend((0,)*(m-n))
  dcs = defaultdict(list)
  dct = defaultdict(list)
  dp = [[1 for i in range(p+1)] for j in range(p+1)]
  for i in range(p):
    if s[i] != 0:
      dcs[s[i]].append(i)
    if t[i] != 0:
      dct[t[i]].append(i)
  def update(i,j):
    val = dp[i-1][j-1]
    x = s[i-1]
    y = t[j-1]
    if dct[x]:
      k = bisect.bisect_left(dct[x],j-1)
      if k != 0:
        val += dp[i][dct[x][k-1]+1]-dp[i-1][dct[x][k-1]+1]
    if dcs[y]:
      l = bisect.bisect_left(dcs[y],i-1)
      if l != 0:
        val += dp[dcs[y][l-1]+1][j]-dp[dcs[y][l-1]+1][j-1]
    if x == y:
      val += dp[i-1][j-1]
    dp[i][j] = val%mod

  for i in range(1,p+1):
    for j in range(1,p+1):
      update(i,j)
  print(dp[-1][-1])
main()