#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
#include<cmath>
#include<bitset>
using namespace std;

int main(){
  int N,M;
  cin>>N>>M;
  int S[N+1],T[M+1];
  for(int i=0; i<N; ++i) cin>>S[i];
  for(int i=0; i<M; ++i) cin>>T[i];
  
  long long int dp[N+1][M+1];
  long long int MOD=1000000007;
  
  for(int i=0; i<N; ++i){
    for(int j=0; j<M; ++j){
      if(S.at(i)==T.at(j)) dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+1)%MOD;
      dp[i+1][j+1]=max(dp[i+1][j+1],max(dp[i+1][j],dp[i][j+1]))%MOD;
    }
  }
  
  cout<<dp[N][M]%MOD<<endl;
}
