#include <bits/stdc++.h>
using namespace std;
struct Fast {
    Fast() {
        std::cin.tie(0);
        ios::sync_with_stdio(false);
        cout.precision(20);
    }
} fast;

/* define */
#define FOR(I, X, Y) for (long long(I) = (X); (I) < (Y); (I)++)
#define REP(I, X, Y) for (long long(I) = (Y)-1; (I) >= (X); (I)--)
#define ALL(X) (X).begin(), (X).end()
#define pb push_back
#define COUNT(V, X)                           \
    (upper_bound((V).begin(), (V).end(), X) - \
     lower_bound((V).begin(), (V).end(), X))
#define debug(x) cerr << #x << ':' << x << endl;
#define DEBUG(v)                             \
    {                                        \
        cerr << #v << ':';                   \
        for (auto xv : v) cerr << xv << ' '; \
        cerr << endl;                        \
    }
#define Yes(X) cout << (X ? "Yes" : "No") << endl;
#define YES(X) cout << (X ? "YES" : "NO") << endl;
#define ctoi(C) (C - '0')
#define pow2(x) ((long long)((long long)1 << x))

/* alias */
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vl = vector<long long>;
using vll = vector<vector<long long>>;
using pi = pair<int, int>;
using pl = pair<long long, long long>;
template <typename T>
using PQ = priority_queue<T>;
template <typename T>
using minPQ = priority_queue<T, vector<T>, greater<T>>;

/* const */
const long long dx[] = {1, 0, -1, 0};
const long long dy[] = {0, 1, 0, -1};
const long long dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};
const long long dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
const long long dx9[] = {1, 1, 0, -1, -1, -1, 0, 1, 0};
const long long dy9[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
const int INF = 1000000007;
const long long LINF = 1000000000000000007;

/* func */
template <typename T1, typename T2>
inline bool chmin(T1 &a, const T2 &b) {
    if (a > b) a = b;
    return a > b;
}
template <typename T1, typename T2>
inline bool chmax(T1 &a, const T2 &b) {
    if (a < b) a = b;
    return a < b;
}
long long max(long long x, int y) {
    return max(x, (long long)y);
}
long long max(int x, long long y) {
    return max((long long)x, y);
}
long long min(long long x, int y) {
    return min(x, (long long)y);
}
long long min(int x, long long y) {
    return min((long long)x, y);
}

const int MAXN = 2010;
const int MAXM = 2010;

/* liblary */

// http://h0rnet.hatenablog.com/entry/2016/03/12/JOI_10_%E6%9C%AC%E9%81%B8_Planetary_Exploration_%28_%E4%BA%8C%E6%AC%A1%E5%85%83BIT_%29

struct BIT {
    int n, m;
    vector<vl> dat;

    BIT(int N, int M) : n(N), m(M) {
        dat.resize(N + 1);
        fill(ALL(dat), vl(M + 1, 0));
    }

    void update(int x, int y, long long v) {
        v %= INF;
        for (int i = x; i <= n; i += i & -i) {
            for (int j = y; j <= m; j += j & -j) {
                dat[i][j] += v;
                if (dat[i][j] > INF) {
                    dat[i][j] %= INF;
                }
            }
        }
    }
    long long qsum(int x, int y) {
        long long res = 0;
        for (int i = x; i > 0; i -= i & -i) {
            for (int j = y; j > 0; j -= j & -j) {
                res += dat[i][j];
            }
            res %= INF;
        }
        return res;
    }
    int qsum(int x0, int y0, int x1, int y1) {
        return qsum(x1, y1) - qsum(x0 - 1, y1) - qsum(x1, y0 - 1) + qsum(x0 - 1, y0 - 1);
    }
};

/* main */

signed main() {
    int N, M;
    cin >> N >> M;
    vector<long long> S(N + 1, -1), T(M + 1, -1);
    FOR(i, 0, N) {
        cin >> S[i + 1];
    }
    FOR(i, 0, M) {
        cin >> T[i + 1];
    }
    BIT DP(N + 1, M + 1);
    DP.update(1, 1, 1);
    FOR(i, 1, N + 1) {
        FOR(j, 1, M + 1) {
            if (S[i] == T[j]) {
                long long tmp = DP.qsum(i, j);
                DP.update(i + 1, j + 1, tmp);
            }
        }
    }
    cout << DP.qsum(N + 1, M + 1) << endl;
}

Measured memory_bytes: 31744