#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll mod = ll(1e9) + 7;
const int INF = int(1e9);
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> S(N), T(M);
  for (int i = 0; i < (int)(N); i++) cin >> S[i];
  for (int i = 0; i < (int)(M); i++) cin >> T[i];
  if (N > M) {
    S.swap(T);
    swap(M, N);
  }
  ll ans = 1;
  for (int i = 1; i < N; i++) {
    int dup = 0;
    for (int j = 0; j < (int)(i); j++) {
      if (S[S.size() - i + j] == T[j]) dup++;
    }
    ans += (1 << dup) - 1;
    ans %= int(1e9) + 7;
  }
  for (int i = 0; i < (int)(max(M, N) - min(M, N) + 1); i++) {
    int dup = 0;
    for (int j = 0; j < (int)(N); j++) {
      if (S[j] == T[i + j]) dup++;
    }
    ans += (1 << dup) - 1;
    ans %= int(1e9) + 7;
  }
  for (int i = N - 1; i > 0; i--) {
    int dup = 0;
    for (int j = 0; j < (int)(i); j++) {
      if (S[j] == T[T.size() - i + j]) dup++;
    }
    ans += (1 << dup) - 1;
    ans %= int(1e9) + 7;
  }
  cout << ans << endl;
  return 0;
}
