#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <array>
#include <queue>
#include <stack>
#include <algorithm>
#include <tuple>
#include <numeric>
#include <type_traits>

template<unsigned int MOD>
struct modcalc
{
	static_assert(MOD < std::numeric_limits<unsigned int>::max() / 4, "");

	static unsigned int add(unsigned int a, unsigned int b)
	{
		return (a + b) % MOD;
	}

	static unsigned int sub(unsigned int a, unsigned int b)
	{
		return (MOD + a - b) % MOD;
	}

	static unsigned int neg(unsigned int a)
	{
		return (MOD - a) % MOD;
	}

	static unsigned int mul(unsigned int a, unsigned int b)
	{
		return (static_cast<unsigned long long>(a) * static_cast<unsigned long long>(b)) % MOD;
	}

	static unsigned int pow(unsigned int a, unsigned int b)
	{
		int ret = 1;
		while (b) {
			if (b & 1)
				ret = mul(ret, a);
			a = mul(a, a);
			b >>= 1;
		}
		return ret;
	}

	// 階乗

	static std::vector<unsigned int> fac_table; // n!

	static void init_fac(unsigned int last)
	{
		fac_table.resize(last);
		fac_table[0] = 1;
		for (unsigned int i = 1; i < last; ++i) {
			fac_table[i] = mul(fac_table[i - 1], i);
		}
	}

	unsigned int fac(unsigned int x)
	{
		return fac_table[x];
	}


	// 逆数

	static std::vector<unsigned int> inv_table; // n^-1

	static void init_inv(unsigned int last)
	{
		inv_table.resize(last);
		inv_table[0] = 0; // XXX
		inv_table[1] = 1;
		for (unsigned int i = 1; i < last; ++i) {
			inv_table[i] = MOD - mul(inv_table[MOD % i], MOD / i);
		}
	}

	static unsigned int inv(unsigned int x)
	{
		return inv_table[x];
	}

	static unsigned int div(unsigned int a, unsigned int b)
	{
		return mul(a, inv(b));
	}


	// 階乗の逆数

	static std::vector<unsigned int> invfac_table; // (n!)^-1

	static unsigned int init_invfac(unsigned int last)
	{
		invfac_table.resize(last);
		invfac_table[0] = 0; // XXX
		invfac_table[1] = 1;
		for (unsigned int i = 1; i < last; ++i) {
			invfac_table[i] = mul(invfac_table[i - 1], inv_table[i]);
		}
	}

	static unsigned int invfac(unsigned int x)
	{
		return invfac_table[x];
	}

	// 二項係数

	static unsigned int combination(unsigned int n, unsigned int k)
	{
		if (n < k)
			return 0;
		return mul(fac(n), mul(invfac(k), invfac(n - k)));
	}
};

template<unsigned int MOD> std::vector<unsigned int> modcalc<MOD>::fac_table;
template<unsigned int MOD> std::vector<unsigned int> modcalc<MOD>::inv_table;
template<unsigned int MOD> std::vector<unsigned int> modcalc<MOD>::invfac_table;

using mod = modcalc<1000000007U>;

int main()
{
	int N, M;
	std::vector<int> S, T;
	
	std::cin >> N >> M;
	S.resize(N);
	T.resize(M);
	for (auto& s : S)
		std::cin >> s;
	for (auto& t : T)
		std::cin >> t;
	
	int d[N+1][M+1];
	std::unordered_map<int, int> dsum; // T の値 -> それの d の合計
	for (int i = 0; i <= N; ++i)
		d[i][0] = 1;
	for (int j = 1; j <= M; ++j) {
		d[0][j] = 1;
		dsum[T[j-1]] = mod::add(dsum[T[j-1]], d[0][j]);
	}

	for (int n = 1; n <= N; ++n) {
		for (int m = 1; m <= M; ++m) {
			d[n][m] = d[n-1][m];
			for (int i = 0; i < m; ++i) {
				if (T[i] == S[n-1]) {
					d[n][m] = mod::add(d[n][m], d[n-1][i]);
				}
			}
		}
		dsum.clear();
		for (int j = 1; j <= M; ++j) {
			dsum[T[j-1]] = mod::add(dsum[T[j-1]], d[n][j]);
		}
	}

	std::cout << d[N][M] << "\n";
}
