#

# 　　  ⋀_⋀　 

#　　  (･ω･)  

# .／ Ｕ ∽ Ｕ＼

#  │＊　合　＊│

#  │＊　格　＊│ 

#  │＊　祈　＊│ 

#  │＊　願　＊│ 

#  │＊　　　＊│ 

#      ￣

#

import sys

input=sys.stdin.readline

from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ

from heapq import heappop, heappush, heappushpop

from collections import Counter,defaultdict

from itertools import accumulate,permutations,combinations,product,combinations_with_replacement

from bisect import bisect_left, bisect_right

from copy import copy

inf=float('inf')

mod = 10**9+7

def INT_(n): return int(n)-1

def MI(): return list(map(int,input().split()))

def MF(): return list(map(float, input().split()))

def MI_(): return list(map(INT_,input().split()))

def LI(): return list(MI())

def LI_(): return [int(x) - 1 for x in input().split()]

def LF(): return list(MF())

def LIN(n:int): return [I() for _ in range(n)]

def LLIN(n: int): return [LI() for _ in range(n)]

def LLIN_(n: int): return [LI_() for _ in range(n)]

def LLI(): return [list(map(int, l.split() )) for l in eval(input())]

def I(): return int(eval(input()))

def F(): return float(eval(input()))

def ST(): return input().replace('\n', '')

#mint

class ModInt:

    def __init__(self, x):

        self.x = x % mod

    

    def __str__(self):

        return str(self.x)

    

    __repr__ = __str__



    def __add__(self, other):

        if isinstance(other, ModInt):

            return ModInt(self.x + other.x)

        else:

            return ModInt(self.x + other)



    __radd__ = __add__

        

    def __sub__(self, other):

        if isinstance(other, ModInt):

            return ModInt(self.x - other.x)

        else:

            return ModInt(self.x - other)



    def __rsub__(self, other):

        if isinstance(other, ModInt):

            return ModInt(other.x - self.x)

        else:

            return ModInt(other - self.x)



    def __mul__(self, other):

        if isinstance(other, ModInt):

            return ModInt(self.x * other.x)

        else:

            return ModInt(self.x * other)



    __rmul__ = __mul__



    def __truediv__(self, other):

        if isinstance(other, ModInt):

            return ModInt(self.x * pow(other.x, mod-2,mod))

        else:

            return ModInt(self.x * pow(other, mod - 2, mod))

            

    def __rtruediv(self, other):

        if isinstance(other, self):

            return ModInt(other * pow(self.x, mod - 2, mod))

        else:

            return ModInt(other.x * pow(self.x, mod - 2, mod))

            



    def __pow__(self, other):

        if isinstance(other, ModInt):

            return ModInt(pow(self.x, other.x, mod))

        else:

            return ModInt(pow(self.x, other, mod))

            



    def __rpow__(self, other):

        if isinstance(other, ModInt):

            return ModInt(pow(other.x, self.x, mod))

        else:

            return ModInt(pow(other, self.x, mod))



def main():

    N, M = MI()

    S = [i for i in ST().split()]

    T = [i for i in ST().split()]



    dp = [[ModInt(1)] * (M + 1) for _ in range(N + 1)] #dp[i][j]:=i,j文字目までの整数列組数



    for i,s in enumerate(S,start = 1):

        for j,t in enumerate(T,start = 1):

            if t == s:

                dp[i][j] = dp[i-1][j] + dp[i][j-1]

            else:

                dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1]

    print((dp[-1][-1]))



if __name__ == '__main__':

    main()
#

# 　　  ⋀_⋀　 

#　　  (･ω･)  

# .／ Ｕ ∽ Ｕ＼

#  │＊　合　＊│

#  │＊　格　＊│ 

#  │＊　祈　＊│ 

#  │＊　願　＊│ 

#  │＊　　　＊│ 

#      ￣

#

import sys

sys.setrecursionlimit(10**6)

input=sys.stdin.readline

from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ

from heapq import heappop, heappush, heappushpop

from collections import Counter,defaultdict,deque

from itertools import accumulate,permutations,combinations,product,combinations_with_replacement

from bisect import bisect_left,bisect_right

from copy import deepcopy

from fractions import gcd

from random import randint

def ceil(a,b): return (a+b-1)//b

inf=float('inf')

mod = 10**9+7

def pprint(*A): 

    for a in A:     print(*a,sep='\n')

def INT_(n): return int(n)-1

def MI(): return map(int,input().split())

def MF(): return map(float, input().split())

def MI_(): return map(INT_,input().split())

def LI(): return list(MI())

def LI_(): return [int(x) - 1 for x in input().split()]

def LF(): return list(MF())

def LIN(n:int): return [I() for _ in range(n)]

def LLIN(n: int): return [LI() for _ in range(n)]

def LLIN_(n: int): return [LI_() for _ in range(n)]

def LLI(): return [list(map(int, l.split() )) for l in input()]

def I(): return int(input())

def F(): return float(input())

def ST(): return input().replace('\n', '')



def main():

    N,M=MI()

    *S,=ST().split()

    *T,=ST().split()



    dp = [[1]*(M+1) for _ in range(N+1)]

    for i,s in enumerate(S,start=1):

        for j,t in enumerate(T,start=1):

            if s==t:

                dp[i][j] = (dp[i-1][j] + dp[i][j-1])%mod

            else:

                dp[i][j] = (dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1])%mod

    print(dp[-1][-1])

if __name__ == '__main__':

    main()

#
# 　　  ⋀_⋀
# 　　  (･ω･)
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│
#  │＊　祈　＊│
#  │＊　願　＊│
#  │＊　　　＊│
#      ￣
#
import sys

input = sys.stdin.readline
from math import floor, ceil, sqrt, factorial, log  # log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter, defaultdict
from itertools import (
    accumulate,
    permutations,
    combinations,
    product,
    combinations_with_replacement,
)
from bisect import bisect_left, bisect_right
from copy import copy

inf = float("inf")
mod = 10**9 + 7


def INT_(n):
    return int(n) - 1


def MI():
    return list(map(int, input().split()))


def MF():
    return list(map(float, input().split()))


def MI_():
    return list(map(INT_, input().split()))


def LI():
    return list(MI())


def LI_():
    return [int(x) - 1 for x in input().split()]


def LF():
    return list(MF())


def LIN(n: int):
    return [I() for _ in range(n)]


def LLIN(n: int):
    return [LI() for _ in range(n)]


def LLIN_(n: int):
    return [LI_() for _ in range(n)]


def LLI():
    return [list(map(int, l.split())) for l in eval(input())]


def I():
    return int(eval(input()))


def F():
    return float(eval(input()))


def ST():
    return input().replace("\n", "")


# mint
class ModInt:
    def __init__(self, x):
        self.x = x % mod

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

    __repr__ = __str__

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

    __radd__ = __add__

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

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

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

    __rmul__ = __mul__

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

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

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

    def __rpow__(self, other):
        if isinstance(other, ModInt):
            return ModInt(pow(other.x, self.x, mod))
        else:
            return ModInt(pow(other, self.x, mod))


def main():
    N, M = MI()
    S = [i for i in ST().split()]
    T = [i for i in ST().split()]
    dp = [[ModInt(1)] * (M + 1) for _ in range(N + 1)]  # dp[i][j]:=i,j文字目までの整数列組数
    for i, s in enumerate(S, start=1):
        for j, t in enumerate(T, start=1):
            if t == s:
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
            else:
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
    print((dp[-1][-1]))


if __name__ == "__main__":
    main()

#
# 　　  ⋀_⋀
# 　　  (･ω･)
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│
#  │＊　祈　＊│
#  │＊　願　＊│
#  │＊　　　＊│
#      ￣
#
import sys

sys.setrecursionlimit(10**6)
input = sys.stdin.readline
from math import floor, sqrt, factorial, hypot, log  # log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter, defaultdict, deque
from itertools import (
    accumulate,
    permutations,
    combinations,
    product,
    combinations_with_replacement,
)
from bisect import bisect_left, bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint


def ceil(a, b):
    return (a + b - 1) // b


inf = float("inf")
mod = 10**9 + 7


def pprint(*A):
    for a in A:
        print(*a, sep="\n")


def INT_(n):
    return int(n) - 1


def MI():
    return map(int, input().split())


def MF():
    return map(float, input().split())


def MI_():
    return map(INT_, input().split())


def LI():
    return list(MI())


def LI_():
    return [int(x) - 1 for x in input().split()]


def LF():
    return list(MF())


def LIN(n: int):
    return [I() for _ in range(n)]


def LLIN(n: int):
    return [LI() for _ in range(n)]


def LLIN_(n: int):
    return [LI_() for _ in range(n)]


def LLI():
    return [list(map(int, l.split())) for l in input()]


def I():
    return int(input())


def F():
    return float(input())


def ST():
    return input().replace("\n", "")


def main():
    N, M = MI()
    (*S,) = ST().split()
    (*T,) = ST().split()
    dp = [[1] * (M + 1) for _ in range(N + 1)]
    for i, s in enumerate(S, start=1):
        for j, t in enumerate(T, start=1):
            if s == t:
                dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % mod
            else:
                dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) % mod
    print(dp[-1][-1])


if __name__ == "__main__":
    main()

+sys.setrecursionlimit(10**6) -from math import floor, ceil, sqrt, factorial, log  # log2ないｙｐ +from math import floor, sqrt, factorial, hypot, log  # log2ないｙｐ -from collections import Counter, defaultdict +from collections import Counter, defaultdict, deque -from copy import copy +from copy import deepcopy +from fractions import gcd +from random import randint + + +def ceil(a, b): +    return (a + b - 1) // b + + + +def pprint(*A): +    for a in A: +        print(*a, sep="\n") -    return list(map(int, input().split())) +    return map(int, input().split()) -    return list(map(float, input().split())) +    return map(float, input().split()) -    return list(map(INT_, input().split())) +    return map(INT_, input().split()) -    return [list(map(int, l.split())) for l in eval(input())] +    return [list(map(int, l.split())) for l in input()] -    return int(eval(input())) +    return int(input()) -    return float(eval(input())) +    return float(input()) -# mint -class ModInt: -    def __init__(self, x): -        self.x = x % mod - -    def __str__(self): -        return str(self.x) - -    __repr__ = __str__ - -    def __add__(self, other): -        if isinstance(other, ModInt): -            return ModInt(self.x + other.x) -        else: -            return ModInt(self.x + other) - -    __radd__ = __add__ - -    def __sub__(self, other): -        if isinstance(other, ModInt): -            return ModInt(self.x - other.x) -        else: -            return ModInt(self.x - other) - -    def __rsub__(self, other): -        if isinstance(other, ModInt): -            return ModInt(other.x - self.x) -        else: -            return ModInt(other - self.x) - -    def __mul__(self, other): -        if isinstance(other, ModInt): -            return ModInt(self.x * other.x) -        else: -            return ModInt(self.x * other) - -    __rmul__ = __mul__ - -    def __truediv__(self, other): -        if isinstance(other, ModInt): -            return ModInt(self.x * pow(other.x, mod - 2, mod)) -        else: -            return ModInt(self.x * pow(other, mod - 2, mod)) - -    def __rtruediv(self, other): -        if isinstance(other, self): -            return ModInt(other * pow(self.x, mod - 2, mod)) -        else: -            return ModInt(other.x * pow(self.x, mod - 2, mod)) - -    def __pow__(self, other): -        if isinstance(other, ModInt): -            return ModInt(pow(self.x, other.x, mod)) -        else: -            return ModInt(pow(self.x, other, mod)) - -    def __rpow__(self, other): -        if isinstance(other, ModInt): -            return ModInt(pow(other.x, self.x, mod)) -        else: -            return ModInt(pow(other, self.x, mod)) - - -    S = [i for i in ST().split()] -    T = [i for i in ST().split()] -    dp = [[ModInt(1)] * (M + 1) for _ in range(N + 1)]  # dp[i][j]:=i,j文字目までの整数列組数 +    (*S,) = ST().split() +    (*T,) = ST().split() +    dp = [[1] * (M + 1) for _ in range(N + 1)] -            if t == s: -                dp[i][j] = dp[i - 1][j] + dp[i][j - 1] +            if s == t: +                dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % mod -                dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] -    print((dp[-1][-1])) +                dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) % mod +    print(dp[-1][-1])