#include <bits/stdc++.h>
using namespace std;
const long INF = 1000000000000000;
const long MOD = 1000000007;
bool check = false;
long solve1(long N, long M, vector<long> S, vector<long> T) {
  vector<vector<long>> dp(N + 1, vector<long>(M + 1, 1));
  for (int i = 1; i < N + 1; i++) {
    for (int j = 1; j < M + 1; j++) {
      dp.at(i).at(j) = dp.at(i).at(j - 1) + dp.at(i - 1).at(j) -
                       (S.at(i - 1) != T.at(j - 1)) * dp.at(i - 1).at(j - 1);
      dp.at(i).at(j) %= MOD;
    }
  }
  return dp.at(N).at(M);
}
long solve2(long N, long M, vector<long> S, vector<long> T) {
  long ans = 0;
  for (long i = 0; i < (long)pow(2, N); i++) {
    vector<long> s(0);
    for (int k = 0; k < N; k++)
      if (i / ((long)pow(2, k)) % 2) s.push_back(S.at(k));
    for (long j = 0; j < pow(2, M); j++) {
      vector<long> t(0);
      for (int k = 0; k < M; k++)
        if ((j / ((long)pow(2, k))) % 2) t.push_back(T.at(k));
      if (s.size() == t.size()) {
        for (int l = 0; l <= s.size(); l++) {
          if (l == s.size()) {
            ans++;
            ans %= MOD;
          } else if (s.at(l) != t.at(l))
            break;
        }
      }
    }
  }
  return ans;
}
int main(void) {
  long N, M;
  cin >> N >> M;
  vector<long> S(N), T(M);
  for (int i = 0; i < N; i++) cin >> S.at(i);
  for (int i = 0; i < M; i++) cin >> T.at(i);
  cout << solve2(N, M, S, T) << endl;
}
