# 入力
N, M = map(int, input().split())
S = list(map(int, input().split()))
T = list(map(int, input().split()))


MOD = 10**9 + 7


class ModInt:
    def __init__(self, x):
        self.x = x % MOD

    def __str__(self):
        return str(self.x)

    __repr__ = __str__

    def __add__(self, other):
        return (
            ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other)
        )

    def __sub__(self, other):
        return (
            ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other)
        )

    def __mul__(self, other):
        return (
            ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other)
        )

    def __truediv__(self, other):
        return (
            ModInt(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(self.x * pow(other, MOD - 2, MOD))
        )

    def __pow__(self, other):
        return (
            ModInt(
                pow(self.x, other.x, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(pow(self.x, other, MOD))
        )

    def __radd__(self, other):
        return ModInt(other + self.x)

    def __rsub__(self, other):
        return ModInt(other - self.x)

    def __rmul__(self, other):
        return ModInt(other * self.x)

    def __rtruediv__(self, other):
        return ModInt(other * pow(self.x, MOD - 2, MOD))

    def __rpow__(self, other):
        return ModInt(pow(other, self.x, MOD))


# LCSと同様にDPで解を求める
dp = [[ModInt(1) for _ in range(M + 1)] for _ in range(N + 1)]
for i in range(1, N + 1):
    for j in range(1, M + 1):
        dp[i][j] = (
            dp[i - 1][j] + dp[i][j - 1] if S[i - 1] == T[j - 1] else
            dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
        )
ans = dp[N][M]

# 出力
print(ans) N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M S = list(map(int, input().split())) S list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split T = list(map(int, input().split())) T list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split MOD = 10**9 + 7 MOD 10**9 + 7 10**9 10 9 7 class ModInt:
    def __init__(self, x):
        self.x = x % MOD

    def __str__(self):
        return str(self.x)

    __repr__ = __str__

    def __add__(self, other):
        return (
            ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other)
        )

    def __sub__(self, other):
        return (
            ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other)
        )

    def __mul__(self, other):
        return (
            ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other)
        )

    def __truediv__(self, other):
        return (
            ModInt(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(self.x * pow(other, MOD - 2, MOD))
        )

    def __pow__(self, other):
        return (
            ModInt(
                pow(self.x, other.x, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(pow(self.x, other, MOD))
        )

    def __radd__(self, other):
        return ModInt(other + self.x)

    def __rsub__(self, other):
        return ModInt(other - self.x)

    def __rmul__(self, other):
        return ModInt(other * self.x)

    def __rtruediv__(self, other):
        return ModInt(other * pow(self.x, MOD - 2, MOD))

    def __rpow__(self, other):
        return ModInt(pow(other, self.x, MOD))


# LCSと同様にDPで解を求める ModInt def __init__(self, x):
        self.x = x % MOD

     __init__ self.x = x % MOD self.x self x x % MOD x MOD self self x x def __str__(self):
        return str(self.x)

     __str__ return str(self.x) str(self.x) str self.x self x self self __repr__ = __str__ __repr__ __str__ def __add__(self, other):
        return (
            ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other)
        )

     __add__ return (
            ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other)
        ) ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other) isinstance(other, ModInt) isinstance other ModInt ModInt(self.x + other.x) ModInt self.x + other.x self.x self x other.x other x ModInt(self.x + other) ModInt self.x + other self.x self x other self self other other def __sub__(self, other):
        return (
            ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other)
        )

     __sub__ return (
            ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other)
        ) ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other) isinstance(other, ModInt) isinstance other ModInt ModInt(self.x - other.x) ModInt self.x - other.x self.x self x other.x other x ModInt(self.x - other) ModInt self.x - other self.x self x other self self other other def __mul__(self, other):
        return (
            ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other)
        )

     __mul__ return (
            ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other)
        ) ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other) isinstance(other, ModInt) isinstance other ModInt ModInt(self.x * other.x) ModInt self.x * other.x self.x self x other.x other x ModInt(self.x * other) ModInt self.x * other self.x self x other self self other other def __truediv__(self, other):
        return (
            ModInt(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(self.x * pow(other, MOD - 2, MOD))
        )

     __truediv__ return (
            ModInt(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(self.x * pow(other, MOD - 2, MOD))
        ) ModInt(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(self.x * pow(other, MOD - 2, MOD)) isinstance(other, ModInt) isinstance other ModInt ModInt(
                self.x * pow(other.x, MOD - 2, MOD)
            ) ModInt self.x * pow(other.x, MOD - 2, MOD) self.x self x pow(other.x, MOD - 2, MOD) pow other.x other x MOD - 2 MOD 2 MOD ModInt(self.x * pow(other, MOD - 2, MOD)) ModInt self.x * pow(other, MOD - 2, MOD) self.x self x pow(other, MOD - 2, MOD) pow other MOD - 2 MOD 2 MOD self self other other def __pow__(self, other):
        return (
            ModInt(
                pow(self.x, other.x, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(pow(self.x, other, MOD))
        )

     __pow__ return (
            ModInt(
                pow(self.x, other.x, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(pow(self.x, other, MOD))
        ) ModInt(
                pow(self.x, other.x, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(pow(self.x, other, MOD)) isinstance(other, ModInt) isinstance other ModInt ModInt(
                pow(self.x, other.x, MOD)
            ) ModInt pow(self.x, other.x, MOD) pow self.x self x other.x other x MOD ModInt(pow(self.x, other, MOD)) ModInt pow(self.x, other, MOD) pow self.x self x other MOD self self other other def __radd__(self, other):
        return ModInt(other + self.x)

     __radd__ return ModInt(other + self.x) ModInt(other + self.x) ModInt other + self.x other self.x self x self self other other def __rsub__(self, other):
        return ModInt(other - self.x)

     __rsub__ return ModInt(other - self.x) ModInt(other - self.x) ModInt other - self.x other self.x self x self self other other def __rmul__(self, other):
        return ModInt(other * self.x)

     __rmul__ return ModInt(other * self.x) ModInt(other * self.x) ModInt other * self.x other self.x self x self self other other def __rtruediv__(self, other):
        return ModInt(other * pow(self.x, MOD - 2, MOD))

     __rtruediv__ return ModInt(other * pow(self.x, MOD - 2, MOD)) ModInt(other * pow(self.x, MOD - 2, MOD)) ModInt other * pow(self.x, MOD - 2, MOD) other pow(self.x, MOD - 2, MOD) pow self.x self x MOD - 2 MOD 2 MOD self self other other def __rpow__(self, other):
        return ModInt(pow(other, self.x, MOD))


# LCSと同様にDPで解を求める __rpow__ return ModInt(pow(other, self.x, MOD)) ModInt(pow(other, self.x, MOD)) ModInt pow(other, self.x, MOD) pow other self.x self x MOD self self other other [ModInt(1) for _ in range(M + 1)] for _ in range(N + 1) for _ in range(N + 1) _ range(N + 1) range N + 1 N 1 for _ in range(N + 1) [ModInt(1) for _ in range(M + 1)] dp = [[ModInt(1) for _ in range(M + 1)] for _ in range(N + 1)] dp [[ModInt(1) for _ in range(M + 1)] for _ in range(N + 1)] for i in range(1, N + 1):
    for j in range(1, M + 1):
        dp[i][j] = (
            dp[i - 1][j] + dp[i][j - 1] if S[i - 1] == T[j - 1] else
            dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
        ) i range(1, N + 1) range 1 N + 1 N 1 for j in range(1, M + 1):
        dp[i][j] = (
            dp[i - 1][j] + dp[i][j - 1] if S[i - 1] == T[j - 1] else
            dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
        ) j range(1, M + 1) range 1 M + 1 M 1 dp[i][j] = (
            dp[i - 1][j] + dp[i][j - 1] if S[i - 1] == T[j - 1] else
            dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
        ) dp[i][j] [i] dp i j dp[i - 1][j] + dp[i][j - 1] if S[i - 1] == T[j - 1] else
            dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] S[i - 1] == T[j - 1] S[i - 1] S i - 1 i 1 T[j - 1] T j - 1 j 1 dp[i - 1][j] + dp[i][j - 1] dp[i - 1][j] [i - 1] dp i - 1 i 1 j dp[i][j - 1] [i] dp i j - 1 j 1 dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] dp[i - 1][j] + dp[i][j - 1] dp[i - 1][j] [i - 1] dp i - 1 i 1 j dp[i][j - 1] [i] dp i j - 1 j 1 dp[i - 1][j - 1] [i - 1] dp i - 1 i 1 j - 1 j 1 ans = dp[N][M] ans dp[N][M] [N] dp N M print(ans) print ans M = map(int, input().split()) map(int, input().split()) M class ModInt:
    def __init__(self, x):
        self.x = x % MOD

    def __str__(self):
        return str(self.x)

    __repr__ = __str__

    def __add__(self, other):
        return (
            ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other)
        )

    def __sub__(self, other):
        return (
            ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other)
        )

    def __mul__(self, other):
        return (
            ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other)
        )

    def __truediv__(self, other):
        return (
            ModInt(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(self.x * pow(other, MOD - 2, MOD))
        )

    def __pow__(self, other):
        return (
            ModInt(
                pow(self.x, other.x, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(pow(self.x, other, MOD))
        )

    def __radd__(self, other):
        return ModInt(other + self.x)

    def __rsub__(self, other):
        return ModInt(other - self.x)

    def __rmul__(self, other):
        return ModInt(other * self.x)

    def __rtruediv__(self, other):
        return ModInt(other * pow(self.x, MOD - 2, MOD))

    def __rpow__(self, other):
        return ModInt(pow(other, self.x, MOD))


# LCSと同様にDPで解を求める class ModInt:
    def __init__(self, x):
        self.x = x % MOD

    def __str__(self):
        return str(self.x)

    __repr__ = __str__

    def __add__(self, other):
        return (
            ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other)
        )

    def __sub__(self, other):
        return (
            ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other)
        )

    def __mul__(self, other):
        return (
            ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other)
        )

    def __truediv__(self, other):
        return (
            ModInt(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(self.x * pow(other, MOD - 2, MOD))
        )

    def __pow__(self, other):
        return (
            ModInt(
                pow(self.x, other.x, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(pow(self.x, other, MOD))
        )

    def __radd__(self, other):
        return ModInt(other + self.x)

    def __rsub__(self, other):
        return ModInt(other - self.x)

    def __rmul__(self, other):
        return ModInt(other * self.x)

    def __rtruediv__(self, other):
        return ModInt(other * pow(self.x, MOD - 2, MOD))

    def __rpow__(self, other):
        return ModInt(pow(other, self.x, MOD))


# LCSと同様にDPで解を求める ModInt N, M = map(int, input().split()) map(int, input().split()) N T = list(map(int, input().split())) list(map(int, input().split())) T MOD = 10**9 + 7 10**9 + 7 MOD S = list(map(int, input().split())) list(map(int, input().split())) S ans = dp[N][M] dp[N][M] ans dp = [[ModInt(1) for _ in range(M + 1)] for _ in range(N + 1)] [[ModInt(1) for _ in range(M + 1)] for _ in range(N + 1)] dp
# 入力
N, M = map(int, input().split())
S = list(map(int, input().split()))
T = list(map(int, input().split()))


MOD = 10**9 + 7


class ModInt:
    def __init__(self, x):
        self.x = x % MOD

    def __str__(self):
        return str(self.x)

    __repr__ = __str__

    def __add__(self, other):
        return (
            ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other)
        )

    def __sub__(self, other):
        return (
            ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other)
        )

    def __mul__(self, other):
        return (
            ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other)
        )

    def __truediv__(self, other):
        return (
            ModInt(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(self.x * pow(other, MOD - 2, MOD))
        )

    def __pow__(self, other):
        return (
            ModInt(
                pow(self.x, other.x, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(pow(self.x, other, MOD))
        )

    def __radd__(self, other):
        return ModInt(other + self.x)

    def __rsub__(self, other):
        return ModInt(other - self.x)

    def __rmul__(self, other):
        return ModInt(other * self.x)

    def __rtruediv__(self, other):
        return ModInt(other * pow(self.x, MOD - 2, MOD))

    def __rpow__(self, other):
        return ModInt(pow(other, self.x, MOD))


# LCSと同様にDPで解を求める
dp = [[ModInt(1) for _ in range(M + 1)] for _ in range(N + 1)]
for i in range(1, N + 1):
    for j in range(1, M + 1):
        dp[i][j] = (
            dp[i - 1][j] + dp[i][j - 1] if S[i - 1] == T[j - 1] else
            dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
        )
ans = dp[N][M]

# 出力
print(ans)
