/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author alireza_kaviani
 */

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef pair<ld, ld> pld;
typedef pair<string, string> pss;

#define all(x)                      (x).begin(),(x).end()
#define Sort(x)                     sort(all((x)))
#define X                           first
#define Y                           second
#define Mp                          make_pair
#define sep                         ' '
#define endl                        '\n'
#define debug(x)                    cerr << #x << " = " << x << endl
#define SZ(x)                       ll(x.size())
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define set_random                  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

inline ll input() {
    ll n;
    cin >> n;
    return n;
}

ll poww(ll a, ll b, ll md) {
    return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}

set_random;

const ll MAXN = 1e4 + 10;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;

template<ll Mod>
struct ModInt {
    ll val;

    ModInt(ll v = 0) { val = (v % Mod + Mod) % Mod; }
    template<ll MOD> ModInt(ModInt<MOD> x) { (*this).val = (x.val % Mod + Mod) % Mod;}

    friend ostream &operator<<(ostream &os, ModInt x) { return os << x.val;}
    friend istream &operator>>(istream &is, ModInt &x) { return is >> x.val;}

    friend ModInt operator+(ModInt x, ModInt y) { return ModInt(x.val + y.val);}
    friend ModInt operator-(ModInt x, ModInt y) { return ModInt(x.val - y.val);}
    friend ModInt operator*(ModInt x, ModInt y) { if (Mod > ll(2e9)) return x.LogMul(x, y); return ModInt(x.val * y.val);}
    friend ModInt operator/(ModInt x, ModInt y) { return x * x.inv(y); }
    friend ModInt operator^(ModInt x, ll y) {
        ModInt ans = 1;
        for(; y ; y /= 2 , x *= x)  if(y & 1)   ans *= x;
        return ans;
    }

    template<class T> ModInt operator+=(T x) { return (*this) = (*this) + x;}
    template<class T> ModInt operator-=(T x) { return (*this) = (*this) - x;}
    template<class T> ModInt operator*=(T x) { return (*this) = (*this) * x;}
    template<class T> ModInt operator/=(T x) { return (*this) = (*this) / x;}
    template<class T> ModInt operator^=(T x) { return (*this) = (*this) ^ x;}

    ModInt inv(ModInt x) { return ModInt(x.val) ^ (Mod - 2); }

    ModInt LogMul(ModInt x, ModInt y) {
        if (y.val == 1) return x;
        return LogMul(ModInt(2 * x.val), ModInt(y.val / 2)) + (y.val % 2 == 1 ? x.val : 0);
    }
};

template <ll T = MOD> using mll = ModInt<T>;

ll n , m , A[MAXN] , B[MAXN];
mll<> dp[MAXN][MAXN];

int main() {
    fast_io;

    cin >> n >> m;
    for(ll i = 1 ; i <= n ; i++){
        cin >> A[i];
    }
    for(ll i = 1 ; i <= m ; i++){
        cin >> B[i];
    }

    for(ll i = 0 ; i < MAXN ; i++)  dp[i][0] = dp[0][i] = 1;
    for(ll i = 1 ; i <= n ; i++){
        for(ll j = 1 ; j <= m ; j++){
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            if(A[i] != B[j]){
                dp[i][j] -= dp[i - 1][j - 1];
            }
        }
    }
    cout << dp[n][m] << endl;

    return 0;
}
/*
todo :
    1- set constants
    2- check TimeLimit and MemoryLimit
    3- check special test cases
    4- don't forget BS can help you that is good idea(use for loop for floats)
    5- don't forget printf and scanf can help you in your code speed
*/


Measured memory_bytes: 783232