#include <bits/stdc++.h>
using namespace std;
map<vector<int>, int64_t> create_map(vector<int> &a, int idx, int size) {
  int n = a.size();
  if (idx < 0 || idx >= n) {
    map<vector<int>, int64_t> data;
    return data;
  }
  if (size == 0) {
    map<vector<int>, int64_t> data;
    vector<int> entry(0);
    data[entry] = 1;
    return data;
  }
  if (size == 1) {
    map<vector<int>, int64_t> data;
    for (int64_t i = 0; i < (int64_t)(idx + 1); i++) {
      vector<int> entry = {a.at(i)};
      data[entry]++;
    }
    return data;
  }
  map<vector<int>, int64_t> data = create_map(a, idx - 1, size);
  int a_last = a.at(idx);
  map<vector<int>, int64_t> sub_data = create_map(a, idx - 1, size - 1);
  for (auto p : sub_data) {
    vector<int> k;
    int64_t v;
    tie(k, v) = p;
    k.push_back(a_last);
    data[k] += v;
  }
  return data;
}
string to_vector_string(vector<int> &a) {
  int n = a.size();
  string s = "[";
  for (int64_t i = 0; i < (int64_t)(n); i++) {
    if (i > 0) {
      s += ",";
    }
    s += to_string(a.at(i));
  }
  s += "]";
  return s;
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> S(N), T(M);
  for (int64_t i = 0; i < (int64_t)(N); i++) {
    cin >> S.at(i);
  }
  for (int64_t i = 0; i < (int64_t)(M); i++) {
    cin >> T.at(i);
  }
  int64_t answer = 0;
  int D = min(N, M);
  for (int64_t j = 0; j < (int64_t)(D + 1); j++) {
    map<vector<int>, int64_t> mapS = create_map(S, N - 1, j);
    map<vector<int>, int64_t> mapT = create_map(T, M - 1, j);
    for (auto ps : mapS) {
      vector<int> ks;
      int64_t vs;
      tie(ks, vs) = ps;
      if (mapT.count(ks) == false) {
        continue;
      }
      int64_t vt = mapT[ks];
      answer += vs * vt;
      answer = answer % 1000000007;
    }
  }
  cout << answer << endl;
}
