#include <bits/stdc++.h>
using namespace std;
const long long maxm = 2e3 + 5;
long long d[maxm][maxm];
long long a[maxm];
long long b[maxm];
long long n, m;
signed main() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 1; i <= m; i++) cin >> b[i];
  for (long long i = 0; i <= n; i++) d[i][0] = 1;
  for (long long i = 0; i <= m; i++) d[0][i] = 1;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      if (a[i] == b[j]) {
        d[i][j] = d[i - 1][j - 1] * 2;
      } else {
        d[i][j] = d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1];
      }
    }
  }
  cout << d[n][m] << endl;
  return 0;
}
