#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long infll = 1ll << 60ll;
const long long mod = 1e9 + 7;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> s(n), t(m);
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < m; i++) cin >> t[i];
  vector<vector<long long>> dp(n + 1, vector<long long>(m + 1, 0));
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = 1;
        dp[i + 1][j + 1] %= mod;
      }
    }
  }
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < m + 1; j++) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  long long ans = 0;
  for (int k = 0; k < n + 1; k++) {
    int i = k;
    int j = 0;
    int cnt = 0;
    while (i != n + 1 && j != m + 1) {
      if (dp[i++][j++] == 1) cnt++;
    }
    long long tmp;
    if (cnt == 0)
      tmp = 0;
    else {
      tmp = 1;
      for (int l = 0; l < cnt - 1; l++) tmp *= 2;
    }
    ans += tmp;
    ans %= mod;
  }
  for (int k = 1; k < m + 1; k++) {
    int i = 0;
    int j = k;
    int cnt = 0;
    while (i != n + 1 && j != m + 1) {
      if (dp[i++][j++] == 1) cnt++;
    }
    long long tmp;
    if (cnt == 0)
      tmp = 0;
    else {
      tmp = 1;
      for (int l = 0; l < cnt - 1; l++) tmp *= 2;
    }
    ans += tmp;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
