#include <bits/stdc++.h>
using namespace std;
const long long INF = 1ll << 60;
const long double EPS = 1e-10;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const long long mods = 1000000007;
template <class T>
inline void amax(T &a, const T &b) {
  if (a < b) a = b;
}
template <class T>
inline void amin(T &a, const T &b) {
  if (a > b) a = b;
}
long long binary(long long key, vector<long long> &a) {
  long long ok = a.size();
  long long dame = -1;
  while (abs(ok - dame) > 1) {
    long long mid = (ok + dame) / 2;
    if (a[mid] <= key)
      ok = mid;
    else
      dame = mid;
  }
  return ok;
}
long long n;
long long m;
long long k;
long long l;
long long temp;
long long x, y;
long long H, W;
char c;
long long ddy, ddx;
signed main() {
  scanf("%lld", &n);
  scanf("%lld", &m);
  long long S[n];
  long long T[n];
  vector<vector<long long> > second(n + 1, vector<long long>(10, 0));
  vector<vector<long long> > Tsum(n + 1, vector<long long>(10, 0));
  for (long long i = (long long)1; i < (long long)n + 1; i++) {
    scanf("%lld", &temp);
    S[i - 1] = temp;
    second[i][temp] = (second[i - 1][temp] + 1) % mods;
  }
  for (long long i = (long long)1; i < (long long)n + 1; i++) {
    scanf("%lld", &temp);
    T[i - 1] = temp;
    Tsum[i][temp] = (Tsum[i - 1][temp] + 1) % mods;
  }
  long long ans = 1;
  for (long long i = (long long)1; i < (long long)n + 1; i++) {
    temp = S[i - 1];
    for (long long j = (long long)1; j < (long long)n + 1; j++) {
      if (temp == T[j - 1]) {
        for (long long k = (long long)1; k < (long long)10; k++) {
          ans = (ans + min(second[i][k], Tsum[j][k])) % mods;
        }
      }
    }
  }
  printf("%lld\n", ans);
}
