#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(ll i=0;i<(n);++i)
#define reps(i,n) for(ll i=1;i<=(n);++i)
using ll = long long;
using str = string;
constexpr long long INF = (1LL<<60);
constexpr long long MOD = (1e9+7);
template<class T>inline T gcd(T a,T b){if(b==0)return a; return(gcd(b,a%b));}
template<class T>inline T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T>inline bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>inline bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
inline void dump(){cout<<endl;}
template<class Head,class... Tail>inline void dump(Head&& head, Tail&&... tail){cout<<head<<", ";dump(forward<Tail>(tail)...);}
template<typename T>inline istream &operator>>(istream&input,vector<T>&v){for(auto &elemnt:v)input>>elemnt;return input;}
template<class T>vector<T> make_vector(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto make_vector(size_t a, Ts... ts){return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));}

main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout<<fixed<<setprecision(10);
  
  ll n,m;
  cin>>n>>m;
  assert(n!=2);
  vector<ll> s(n+1),t(m+1);
  for(ll i=1;i<=n;++i)cin>>s[i];
  for(ll j=1;j<=m;++j)cin>>t[j];

  auto dp = make_vector<ll>(n+1,m+1);
  dp[0][0]=1;
  for(ll i=0;i<=n;++i)for(ll j=0;j<=m;++j){
    if(i>0)dp[i][j]+=dp[i-1][j];
    if(j>0)dp[i][j]+=dp[i][j-1];
    if(i>0&&j>0)dp[i][j]-=dp[i-1][j-1];
    if(i>0&&j>0&&s[i]==t[j])dp[i][j]+=dp[i-1][j-1];
    dp[i][j]+=MOD;
    dp[i][j]%=MOD;
  }
  
  cout<<dp[n][m]<<endl;

}
