#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long MOD = 1000000007LL;
vector<long long> S;
vector<long long> T;
vector<vector<long long>> dp;
long long solve(const int si, const int ti, const long long N,
                const long long M) {
  if (si == 0 || ti == 0) {
    return 1;
  }
  if (dp[si][ti] >= 0) {
    return dp[si][ti];
  }
  long long ans;
  if (S[si] == T[ti]) {
    ans = solve(si - 1, ti - 1, N, M) * 2 % MOD;
  } else {
    ans = solve(si - 1, ti - 1, N, M);
  }
  {
    int i = si - 1;
    for (; i >= 1; i--) {
      if (S[i] == T[ti]) {
        ans = (ans + solve(i - 1, ti - 1, N, M)) % MOD;
      }
    }
  }
  {
    int i = ti - 1;
    for (; i >= 1; i--) {
      if (T[i] == S[si]) {
        ans = (ans + solve(si - 1, i - 1, N, M)) % MOD;
      }
    }
  }
  dp[si][ti] = ans;
  return dp[si][ti];
}
long long solve(const long long N, const long long M) {
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      long long ans;
      if (S[i] == T[j]) {
        ans = dp[i - 1][j - 1] * 2 % MOD;
      } else {
        ans = dp[i - 1][j - 1];
      }
      for (int ii = i - 1; ii >= 1; ii--) {
        if (S[ii] == T[j]) {
          ans = (ans + dp[ii - 1][j - 1]) % MOD;
        }
      }
      for (int jj = j - 1; jj >= 1; jj--) {
        if (T[jj] == S[i]) {
          ans = (ans + dp[i - 1][jj - 1]) % MOD;
        }
      }
      dp[i][j] = ans;
    }
  }
  return dp[N][M];
}
int main(void) {
  long long N, M;
  cin >> N >> M;
  S.resize(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> S[i];
  }
  T.resize(M + 1, 0);
  for (int i = 1; i <= M; i++) {
    cin >> T[i];
  }
  dp.resize(N + 1, vector<long long>(M + 1, -1));
  for (int i = 0; i <= N; i++) {
    dp[i][0] = 1;
  }
  for (int i = 0; i <= M; i++) {
    dp[0][i] = 1;
  }
  cout << solve(N, M) << endl;
  return 0;
}
