#include <bits/stdc++.h>
using namespace std;
using vi = vector<long long>;
using pint = pair<long long, long long>;
struct io {
  io() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout.tie(0);
    cout << fixed << setprecision(20);
  }
} io;
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (T& e : v) is >> e;
  return is;
}
template <class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << "{";
  for (T& e : v)
    os << e << (v.size() - (long long)(&e - &v[0]) > 1 ? ", " : "");
  os << "}";
  return os;
}
void _DEBUG() {}
template <typename H, typename... T>
void _DEBUG(H a, T... b) {
  cerr << a << (sizeof...(b) ? "," : "\n");
  _DEBUG(b...);
}
inline void in() {}
template <typename H, typename... T>
void in(H& a, T&... b) {
  cin >> a;
  in(b...);
}
inline void out() {}
template <typename H, typename... T>
void out(H a, T... b) {
  cout << a << (sizeof...(b) ? " " : "\n");
  out(b...);
}
template <class T>
void resz(long long n, T& v) {
  v.resize(n);
}
template <class H, class... T>
void resz(long long n, H& a, T&... b) {
  a.resize(n);
  resz(n, b...);
}
template <typename V, typename H>
void resize(vector<V>& v, const H a) {
  v.resize(a);
}
template <typename V, typename H, typename... T>
void resize(vector<V>& v, const H& a, const T... b) {
  v.resize(a);
  for (auto& v : v) resize(v, b...);
}
const long long INF = 1LL << 55;
const long long MOD = 1000000007;
const double EPS = 1e-8;
long long N, M;
vi S, T;
vector<map<vi, long long>> subS, subT;
long long ans;
signed main() {
  in(N, M);
  S.resize(N);
  T.resize(M);
  in(S);
  in(T);
  vi tmp;
  subS.resize(N + 1);
  subT.resize(M + 1);
  subS[0][{}] = 1;
  for (long long i = (true) ? 0 : N - 1; (true) ? i < N : i >= 0;
       (true) ? ++i : --i) {
    for (auto e : subS[i]) {
      subS[i + 1][e.first] += e.second;
      auto v = e.first;
      v.push_back(S[i]);
      subS[i + 1][v] += e.second;
      subS[i + 1][v] %= MOD;
    }
  }
  subT[0][{}] = 1;
  for (long long i = (true) ? 0 : M - 1; (true) ? i < M : i >= 0;
       (true) ? ++i : --i) {
    for (auto e : subT[i]) {
      subT[i + 1][e.first] += e.second;
      auto v = e.first;
      v.push_back(T[i]);
      subT[i + 1][v] += e.second;
      subS[i + 1][v] %= MOD;
    }
  }
  auto itrS = subS[N].begin(), itrT = subT[M].begin();
  while (itrS != subS[N].end() && itrT != subT[M].end()) {
    if (itrS->first < itrT->first) {
      itrS++;
    } else if (itrS->first > itrT->first) {
      itrT++;
    } else {
      ans += itrS->second * itrT->second;
      ans %= MOD;
      itrS++;
      itrT++;
    }
  }
  out(ans);
  return 0;
}
