#include <iomanip>
#include <limits>
#include <thread>
#include <utility>
#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <numeric>
#include <cassert>
#include <random>
#include <chrono>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <list>
#include <functional>
#include <bitset>
#include <complex>
#include <tuple>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double D;
typedef complex<D> P;
const ll E = 1e18 + 7;
const ll MOD = 1000000007;
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fill(x, y) memset(x, y, sizeof(x))
#define pcnt __builtin_popcount
#define buli(x) __builtin_popcountll(x)
#define pb push_back
#define mp make_pair
#define F first
#define S second
template <class... A>
void prints()
{
    cout << endl;
}
template <class... A>
void prints_rest() { cout << endl; }
template <class T, class... A>
void prints_rest(const T &first, const A &... rest)
{
    cout << " " << first;
    prints_rest(rest...);
}
template <class T, class... A>
void prints(const T &first, const A &... rest)
{
    cout << first;
    prints_rest(rest...);
}

template <class T>
T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <unsigned P>
struct ModInt
{
    using M = ModInt;
    unsigned v;
    ModInt() : v(0) {}
    ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
    constexpr ModInt(unsigned v, int) : v(v) {}
    static constexpr unsigned p() { return P; }
    M operator+() const { return *this; }
    M operator-() const { return {v ? P - v : 0, 0}; }
    explicit operator bool() const noexcept { return v; }
    bool operator!() const noexcept { return !(bool)*this; }
    M operator*(M r) const { return M(*this) *= r; }
    M operator/(M r) const { return M(*this) /= r; }
    M operator+(M r) const { return M(*this) += r; }
    M operator-(M r) const { return M(*this) -= r; }
    bool operator==(M r) const { return v == r.v; }
    bool operator!=(M r) const { return !(*this == r); }
    M &operator*=(M r)
    {
        v = (uint64_t)v * r.v % P;
        return *this;
    }
    M &operator/=(M r) { return *this *= r.inv(); }
    M &operator+=(M r)
    {
        if ((v += r.v) >= P)
            v -= P;
        return *this;
    }
    M &operator-=(M r)
    {
        if ((v += P - r.v) >= P)
            v -= P;
        return *this;
    }
    M inv() const
    {
        int a = v, b = P, x = 1, u = 0;
        while (b)
        {
            int q = a / b;
            swap(a -= q * b, b);
            swap(x -= q * u, u);
        }
        assert(a == 1);
        return x;
    }
    M pow(auto n) const
    {
        if (n < 0)
            return pow(-n).inv();
        M res = 1;
        for (M a = *this; n; a *= a, n >>= 1)
            if (n & 1)
                res *= a;
        return res;
    }
    friend M operator*(auto l, M r) { return M(l) *= r; }
    friend M operator/(auto l, M r) { return M(l) /= r; }
    friend M operator+(auto l, M r) { return M(l) += r; }
    friend M operator-(auto l, M r) { return M(l) -= r; }
    friend ostream &operator<<(ostream &os, M r) { return os << r.v; }
    friend istream &operator>>(istream &is, M &r)
    {
        ll x;
        is >> x;
        r = x;
        return is;
    }
    friend bool operator==(auto l, M r) { return M(l) == r; }
    friend bool operator!=(auto l, M r) { return !(l == r); }
};

int main()
{
    using mint = ModInt<MOD>;

    cin.tie(0);
    ios::sync_with_stdio(false);

    int N, M;
    cin >> N >> M;
    int S[N], T[M];
    mint A[N+1][M+1], B[N+1][M+1];

    rep(i, N) { cin >> S[i]; A[i][0] = 0; B[i][0] = 1;}
    rep(j, M) { cin >> T[j]; A[0][j] = 0; B[0][j] = 1;}
    A[0][0] = 1;
    A[N][0] = 0; B[N][0] = 1;
    A[0][M] = 0; B[0][M] = 1;

    REP(i, 1, N+1) {
        REP(j, 1, M+1) {
            if (S[i-1] == T[j-1]) {
                A[i][j] = B[i-1][j-1];
            } else {
                A[i][j] = 0;
            }

            B[i][j] = B[i-1][j] + B[i][j-1] - B[i-1][j-1] + A[i][j];
        }
    }

    prints(B[N][M]);
}


Measured memory_bytes: 31616