#include <bits/stdc++.h>
struct Mint {
  static const long long mod = (long long)(1e9 + 7);
  long long val;
  Mint() { val = 0; }
  Mint(long long a) {
    val = a;
    verify_value();
  }
  void verify_value() {
    if (val >= mod) val %= mod;
    if (val < 0) val %= mod, val += mod;
  }
  Mint pow(long long p) const {
    Mint cur = Mint(val), ret = 1;
    while (p > 0) {
      if (p & 1) ret *= cur;
      cur *= cur;
      p >>= 1LL;
    }
    return ret;
  }
  Mint inv() const {
    if (val == 0) std::cerr << "WARNING: inv() is called with 0." << std::endl;
    return pow(mod - 2);
  }
  Mint operator+() const { return *this; }
  Mint operator-() const { return Mint(mod - val); }
  Mint operator+=(const Mint &a) {
    val += a.val;
    if (val >= mod) val -= mod;
    return Mint(val);
  }
  Mint operator*=(const Mint &a) {
    val *= a.val;
    if (val >= mod) val %= mod;
    return Mint(val);
  }
  Mint operator-=(const Mint &a) { return *this += -a; }
  Mint operator/=(const Mint &a) { return *this *= a.inv(); }
  Mint operator++() { return *this += Mint(1); }
  Mint operator--() { return *this -= Mint(1); }
  Mint operator++(int) {
    Mint ret = *this;
    ++(*this);
    return ret;
  }
  Mint operator--(int) {
    Mint ret = *this;
    --(*this);
    return ret;
  }
  operator long long() const { return val; }
};
Mint operator+(const Mint &a, const Mint &b) {
  long long ret = a.val + b.val;
  if (ret >= Mint::mod) ret -= Mint::mod;
  return Mint(ret);
}
Mint operator*(const Mint &a, const Mint &b) {
  long long ret = a.val * b.val;
  if (ret >= Mint::mod) ret %= Mint::mod;
  return Mint(ret);
}
Mint operator-(const Mint &a, const Mint &b) { return a + (-b); }
Mint operator/(const Mint &a, const Mint &b) { return a * b.inv(); }
std::ostream &operator<<(std::ostream &out, const Mint &a) {
  return out << a.val;
}
std::istream &operator>>(std::istream &in, Mint &a) {
  in >> a.val;
  a.verify_value();
  return in;
}
Mint pow(Mint a, long long b) { return a.pow(b); }
class SegmentTree2D {
 private:
  const Mint INI = 0;
  int R, C;
  std::vector<std::vector<Mint>> dat;
  int op(Mint a, Mint b) { return a + b; }

 public:
  SegmentTree2D(int n_row = 0, int n_col = 0) {
    R = 1;
    C = 1;
    while (R < n_row) R <<= 1;
    while (C < n_col) C <<= 1;
    dat = std::vector<std::vector<Mint>>(2 * R - 1,
                                         std::vector<Mint>(2 * C - 1, INI));
  }
  SegmentTree2D &operator=(const SegmentTree2D &s) {
    R = s.getR();
    C = s.getC();
    dat = s.getdat();
    return *this;
  }
  int getR() const { return R; }
  int getC() const { return C; }
  std::vector<std::vector<Mint>> getdat() const { return dat; }

 public:
  void update(int row, int col, int val) {
    row += R - 1;
    col += C - 1;
    dat[row][col] = val;
    int pos = col;
    while (pos > 0) {
      pos = (pos - 1) / 2;
      dat[row][pos] = op(dat[row][2 * pos + 1], dat[row][2 * pos + 2]);
    }
    while (row > 0) {
      row = (row - 1) / 2;
      dat[row][col] = op(dat[2 * row + 1][col], dat[2 * row + 2][col]);
      int pos = col;
      while (pos > 0) {
        pos = (pos - 1) / 2;
        dat[row][pos] = op(dat[row][2 * pos + 1], dat[row][2 * pos + 2]);
      }
    }
  }
  int getval(int row0, int row1, int col0, int col1, int ind = 0, int left = 0,
             int right = -1) {
    if (row1 - row0 <= 0 || col1 - col0 <= 0)
      std::cerr << "INVALID INPUT" << std::endl;
    if (right < 0) right = R;
    if (right <= row0 || row1 <= left) return INI;
    if (row0 <= left && right <= row1) return getval_col(ind, col0, col1);
    int mid = left + (right - left) / 2;
    int leftval = getval(row0, row1, col0, col1, 2 * ind + 1, left, mid);
    int rightval = getval(row0, row1, col0, col1, 2 * ind + 2, mid, right);
    return op(leftval, rightval);
  }

 private:
  int getval_col(int ind, int col0, int col1, int ind_col = 0, int left = 0,
                 int right = -1) {
    if (right < 0) right = C;
    if (right <= col0 || col1 <= left) return INI;
    if (col0 <= left && right <= col1) return dat[ind][ind_col];
    int mid = left + (right - left) / 2;
    int leftval = getval_col(ind, col0, col1, 2 * ind_col + 1, left, mid);
    int rightval = getval_col(ind, col0, col1, 2 * ind_col + 2, mid, right);
    return op(leftval, rightval);
  }
};
int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<int> a(n + 1), b(m + 1);
  for (int i = 1; i <= n; i++) std::cin >> a[i];
  for (int i = 1; i <= m; i++) std::cin >> b[i];
  SegmentTree2D seg(n + 1, m + 1);
  seg.update(0, 0, Mint(1));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i] == b[j]) {
        seg.update(i, j, seg.getval(0, i, 0, j));
      }
    }
  }
  std::cout << seg.getval(0, n + 1, 0, m + 1) << std::endl;
  return 0;
}
