#include <bits/stdc++.h>
using namespace std;
int a[100000];
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(n);
  vector<int> t(m);
  for (int i = (0); i <= (n - 1); i++) cin >> s[i];
  for (int i = (0); i <= (m - 1); i++) cin >> t[i];
  vector<vector<int> > dp(n);
  for (int i = (0); i <= (n - 1); i++) dp[i].resize(m);
  dp[0][0] = (s[0] == t[0] ? 1 : 0);
  for (int i = (1); i <= (n - 1); i++)
    dp[i][0] = dp[i - 1][0] + (s[i] == t[0] ? 1 : 0);
  for (int i = (1); i <= (m - 1); i++)
    dp[0][i] = dp[0][i - 1] + (s[0] == t[i] ? 1 : 0);
  unordered_map<int, list<int> > sh;
  for (int i = (1); i <= (n - 1); i++) sh[s[i]].push_back(i);
  for (int i = (1); i <= (n - 1); i++)
    for (int j = (1); j <= (m - 1); j++) {
      int x = dp[i][j - 1];
      for (int z : sh[t[j]])
        if (z <= i) x = (x + dp[z - 1][j - 1] + 1) % 1000000007;
      if (t[j] == s[0]) x += 1;
      dp[i][j] = x;
    }
  cout << (dp[n - 1][m - 1] + 1) % 1000000007 << endl;
}
