#include <cstdint>
 
template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;
 
public:
  u64 a;
 
  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};


#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<numeric>
#include<algorithm>
#include<complex>
#include<queue>
#include<stdlib.h>
#include<map>
#include<cmath>
#include<stdio.h>
#include<stdlib.h>
#include<set>

#define ll long long int
#define MOD 1000000007
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair

using namespace std;
using mint = modint<1000000007>;

mint dp0[2005][2005];
mint dp1[2005][2005];

int main(){
    int N,M;
    cin >> N >> M;
    vector<ll> S(N+1);
    vector<ll> T(M+1);
    for(int i=0;i<N;i++){
        cin >> S[i];
    }
    for(int i=0;i<M;i++){
        cin >> T[i];
    }
    dp0[0][0]=1;
    for(int i=0;i<N+1;i++){
        for(int j=0;j<M+1;j++){
            dp0[i+1][j]+=dp0[i][j];
            dp1[i][j]=dp0[i][j];
            dp1[i][j+1]+=dp1[i][j];
            if(S[i]==T[j]){
                dp0[i+1][j+1]+=dp1[i][j];
            }
            for(int k=0;k<N+1;k++){
                for(int l=0;l<M+1;l++){
                    cout << dp0[k][l].value() << " ";
                }
                cout << endl;
            }
            cout << endl;
            for(int k=0;k<N+1;k++){
                for(int l=0;l<M+1;l++){
                    cout << dp1[k][l].value() << " ";
                }
                cout << endl;
            }
            cout << endl;
        }
    }
    cout << dp1[N][M].value() << endl;
    return 0;
}
