#include <bits/stdc++.h>


const int MOD = INF + 7;

template<int mod>
class binary_indexed_tree_2d_by_mod {
private:
    int N;
    int M;
    std::vector<std::vector<int>> bit;

    void add(int &a, int b) {
        if (b < 0) b += mod;
        a += b;
        if (a + b >= mod) a -= mod;
    }

public:
    binary_indexed_tree_2d_by_mod() {}
    binary_indexed_tree_2d_by_mod(int n, int m) : N(n), M(m), bit(n, std::vector<int>(m)) {}

    void init(int n, int m) {
        N = n;
        M = m;
        bit = std::vector<std::vector<int>>(N + 1, std::vector<int>(M + 1));
    }

    void add(int x, int y, long long val) {
        val %= mod;
        for (int i = x; i <= N; i += i & (-i)) {
            for (int j = y; j <= M; j += j & (-j)) {
                this->add(this->bit[i][j], val);
            }
        }
    }

    int sum(int x, int y) {
        int res = 0;
        for (int i = x; i > 0; i -= i & (-i)) {
            for (int j = y; j > 0; j -= j & (-j)) {
                this->add(res, this->bit[i][j]);
            }
        }
        return res;
    }

    int sum(int x1, int y1, int x2, int y2) {

        int res = this->sum(x2, y2);
        add(res, - sum(x1 - 1, y2));
        add(res, - sum(x2, y1 - 1));
        add(res, sum(x1 - 1, y1 - 1));

        return res;
    }
};

const int MAX = 2e3 + 10;
int N, M;
binary_indexed_tree_2d_by_mod<MOD> bit;

int main(void) {
    std::cin >> N >> M;
    std::vector<int> S(N);
    for (int i = 0; i < N; i++) {
        std::cin >> S[i];
    }
    std::vector<int> T(M);
    for (int i = 0; i < M; i++) {
        std::cin >> T[i];
    }
    bit.init(N + 1, M + 1);

    bit.add(1, 1, 1);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (S[i] == T[j]) {
                bit.add(i + 2, j + 2, bit.sum(i + 1, j + 1));
            }
        }
    }

    std::cout << bit.sum(N + 1, M + 1) << std::endl;
}
