#include <bits/stdc++.h>
#define REP(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define All(x) (x).begin(),(x).end()
#define ITR(i,x) for(auto i=(x).begin();i!=(x).end();++i)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const ll mod = 1000000007;

ll mpow(ll x, ll n){ //x^n(mod) ←普通にpow(x,n)では溢れてしまうため，随時mod計算 2分累乗法だから早い
    ll ans = 1;
    while(n != 0){
        if(n&1) ans = ans*x % mod;
        x = x*x % mod;
        n = n >> 1;
    }
    return ans;
}
ll inv_mod(ll a){return mpow(a,mod-2);}
template<ll mod> class modll {
 private:
  ll _num;
  modll set(ll num){ _num = num % mod; return *this; }
 public:
  modll(){ _num = 0; }
  modll(ll num){ _num = num % mod; }

  modll operator= (ll x){ return set(x % mod); }
  modll operator+ (ll x){ return modll<mod>(_num + (x % mod)); }
  modll operator- (ll x){ return modll<mod>(_num - (x % mod)); }
  modll operator* (ll x){ return modll<mod>(_num * (x % mod)); }
  modll operator+=(ll x){ return set(_num + (x % mod)); }
  modll operator-=(ll x){ return set(_num - (x % mod)); }
  modll operator*=(ll x){ return set(_num * (x % mod)); }

  explicit operator ll() { return _num; }
  friend ostream& operator<<(ostream &os, const modll<mod> &x){ os << x._num; return os; }
};
class UnionFind{//UnionFind木
 private:
    vector<int> Parent;
 public:
    UnionFind(int N){
        for(int i=0;i<N;i++) Parent.push_back(-1);
    }

    int root(int A){
        if(Parent[A]<0) return A;
        return Parent[A]=root(Parent[A]);
    }

    int size(int A){
        return -Parent[root(A)];
    }

    bool connect(int A,int B){
        A=root(A);
        B=root(B);
        if(A==B) return false;
        if(size(A)<size(B)) swap(A,B);
        Parent[A]-=Parent[B];
        Parent[B]=A;
    }
};
bool square(ll a){ll n=(ll)sqrt(a);return a==n*n;}
int digitsum(ll N,int a){
    if(N==0) return 0;
    int ret=0;
    ret+=digitsum(N/a,a)+N%a;
    return ret;
}
int gcd(int x,int y){return y ? gcd(y,x%y) : x;};//xとyの最大公約数
int lcm(int x,int y){return x*y/gcd(x,y);}//xとyの最小公倍数

class Factorial{//階乗とその逆元を求めて計算に利用するクラス
 private:
    vector<ll> fac;
 public:
    
    Factorial(ll N){
        fac.push_back(1);
        for(int i=0;i<N;i++) fac.push_back(fac[i]*(i+1)%mod);
    }

    ll fact(ll a){return fac[a];}
    ll ifact(ll a){return inv_mod(fac[a]);}

    ll cmb(ll a,ll b){
        if(a==0&&b==0) return 1;
        if(a<b||a<0) return 0;
        ll tmp =ifact(a-b)*ifact(b)%mod;
        return tmp*fac[a]%mod;
    }
};

int main(){
    int n, m;
    cin>>n>>m;
	vector<int> s(n+1), t(m+1);
	REP(i,n) cin>>s[i];
    REP(i,m) cin>>t[i];
	vector<vector<int>> dp0(n+2,vector<int>(m+2));
    vector<vector<int>> dp1(n+2,vector<int>(m+2));
	// dp[i][j] : sのi文字目、tのj文字目の部分列の数
	REP(i, n+1) REP(j, m+1){ 
        dp0[i][j] = 0;
        dp1[i][j] = 0;
    }
    dp0[0][0]=1;
	for(int i = 0; i <= n; i++){
		for(int j = 0; j <= m; j++){
            (dp0[i+1][j]+=dp0[i][j])%=mod;
            (dp1[i][j]+=dp0[i][j])%=mod;
            (dp1[i][j+1]+=dp1[i][j])%=mod;
			if(s[i] == t[j]){
				dp0[i+1][j+1] += dp1[i][j]%mod;
			}
		}
	}

	cout << dp1[n][m] << endl;
}

Measured memory_bytes: 31744