#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <random>
#include <iomanip>

using namespace std;
typedef long long unsigned int ll;
typedef pair<int, int> P;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define fillInt(xs, x)        \
  for (int i = 0; i < (x); i++) \
    scanf("%d", &xs[i]);
#define fillLong(xs, x)       \
  for (int i = 0; i < (x); i++) \
    scanf("%ld", &xs[i]);
#define fillString(xs, x)       \
  for (int i = 0; i < (x); i++) \
    cin >> xs[i];
#define sortv(xs) sort(xs.begin(), xs.end())
#define sortvdi(xs) sort(xs.begin(), xs.end(), std::greater<int>())
#define lbv(xs, x) (lower_bound(xs.begin(), xs.end(), x) - xs.begin())
#define ubv(xs, x) (upper_bound(xs.begin(), xs.end(), x) - xs.begin())
#define bs(xs, x) binary_search(xs.begin(), xs.end(), x)

#define rep(i,n) for(auto i=0; i<(n); i++)
#define isValidPoint(x, y, mx, my) x >= 0 && x  < mx && y >= 0 && y < my

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

const int MOD = 1000000007;

long exp_mod(long long a, long long b, long m) {
	long long res = 1;
	while (b > 0) {
		if (b&1)
			res = (res * a)%m;
		a = (a * a)%m;
		b >>= 1;
	}
	return (long)res;
}

long fact_mod(long n, long m) {
	long long res = 1;
	for (long i=n; i>0; --i)
		res = (res * i)%m;
	return (long)res;
}

long comb_mod(long n, long r) {
	long long res = 1;
	for (long i=0; i<r; ++i)
		res = (res * (n - i) )%MOD;
	return ( res * exp_mod(fact_mod(r, MOD), MOD-2, MOD) ) % MOD;
}

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

struct Pony
{
    int x,y;

    Pony(int x_, int y_)
    {
        x = x_;
        y = y_;
    }

    bool operator<(const Pony &another) const
    {
        return x < another.x;
    };
};

long modinv(long a, long m) {
    long b = m, u = 1, v = 0;
    while (b) {
        long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

vector<int> ss[100005];
vector<int> tt[100005];

int main () {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n,m;

  cin >> n >> m;

  vector<int> s(n);
  vector<int> t(m);

  fillint(s,n);
  fillint(t,m);

  int dp[n+1][m+1];

  dp[0][0] = 1;

  rep(i, n) {
    dp[i][0] = 1;
    ss[s[i]].push_back(i);
  }

  rep(i, m) {
    dp[0][i] = 1;
    tt[t[i]].push_back(i);
  }

  dp[n][0] = 1;
  dp[0][n] = 1;

  for(int i = 1; i <= n; i++) {
    int si = s[i-1];
    for(int j = 1; j <= m; j++) {
      int tj = t[j-1];
      dp[i][j] = dp[i][j-1];
      for(int k: ss[tj]) {
        if (k >= i) break;
        dp[i][j] = (dp[i][j] + dp[k][j-1]) % MOD;
      }
    }
  }

  cout << dp[n][m] << endl;
}

