#include <bits/stdc++.h>
using namespace std;
long long modpow(long long n) {
  if (n == 0) return 1ll;
  long long prev = modpow(n / 2);
  return (prev * prev * (n % 2 == 1 ? 2 : 1)) % (long long)(1e9 + 7);
}
int main() {
  long long n, m;
  cin >> n >> m;
  long long s[n], t[m];
  for (long long i = (long long)(0); i < (long long)(n); ++i) cin >> s[i];
  for (long long i = (long long)(0); i < (long long)(m); ++i) cin >> t[i];
  long long ans = 0;
  map<long long, bool> u;
  for (long long i = (long long)(0); i < (long long)(n); ++i) {
    if (u[i]) continue;
    long long last = -1;
    long long lp = 0;
    long long mlen = 0;
    map<long long, long long> mp;
    bool f = false;
    for (long long j = (long long)(0); j < (long long)(m); ++j) {
      if (s[i] == t[j]) {
        u[i] = true;
        f = true;
        if (s[i] != last) {
          mlen++;
          last = s[i];
        } else {
          mp[last]++;
        }
      }
    }
    long long nowans = modpow(mlen);
    if (i != 0) nowans--;
    for (auto itr = mp.begin(); itr != mp.end(); itr++) {
      long long key = itr->first;
      long long val = itr->second;
      nowans += (val + 1) * modpow(mlen - 1);
      nowans %= (long long)(1e9 + 7);
    }
    ans += nowans;
    ans %= (long long)(1e9 + 7);
  }
  cout << ans << endl;
  return 0;
}
