#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, M, ans = 0, MOD = 1e9 + 7;
  cin >> N >> M;
  vector<long long> S(N), T(M);
  for (int i = 0; i < N; i++) cin >> S[i];
  for (int i = 0; i < M; i++) cin >> T[i];
  vector<vector<long long>> s(N + 1, vector<long long>(M + 1, 0));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (S[i] == T[i]) {
        if (s[i][j] == 0)
          s[i + 1][j + 1] = 1;
        else
          s[i + 1][j + 1] = (s[i][j] * 2) % MOD;
      }
      ans += s[i + 1][j + 1];
      ans %= MOD;
    }
  }
  cout << ans << endl;
}
