import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    S=list(map(int,input().split()))
    T=list(map(int,input().split()))
    C=[[0]*(m+1) for _ in range(n+1)]
    from itertools import product
    for i,j in product(range(n),range(m)):
        dp=C[i][j]+1 if(S[i]==T[j]) else 0
        C[i+1][j+1]=dp+C[i][j+1]+C[i+1][j]-C[i][j]
        C[i+1][j+1]%=MOD
    print(C[n][m]+1)
resolve() import sys sys sys.setrecursionlimit(2147483647) sys.setrecursionlimit sys setrecursionlimit 2147483647 INF=float("inf") INF float("inf") float "inf" MOD=10**9+7 MOD 10**9+7 10**9 10 9 7 input=lambda :sys.stdin.readline().rstrip() input lambda :sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip def resolve():
    n,m=map(int,input().split())
    S=list(map(int,input().split()))
    T=list(map(int,input().split()))
    C=[[0]*(m+1) for _ in range(n+1)]
    from itertools import product
    for i,j in product(range(n),range(m)):
        dp=C[i][j]+1 if(S[i]==T[j]) else 0
        C[i+1][j+1]=dp+C[i][j+1]+C[i+1][j]-C[i][j]
        C[i+1][j+1]%=MOD
    print(C[n][m]+1) resolve 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 [0]*(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) [0]*(m+1) [0] 0 m+1 m 1 C=[[0]*(m+1) for _ in range(n+1)] C [[0]*(m+1) for _ in range(n+1)] from itertools import product for i,j in product(range(n),range(m)):
        dp=C[i][j]+1 if(S[i]==T[j]) else 0
        C[i+1][j+1]=dp+C[i][j+1]+C[i+1][j]-C[i][j]
        C[i+1][j+1]%=MOD
     i j product(range(n),range(m)) product range(n) range n range(m) range m dp=C[i][j]+1 if(S[i]==T[j]) else 0 dp C[i][j]+1 if(S[i]==T[j]) else 0 S[i]==T[j] S[i] S i T[j] T j C[i][j]+1 C[i][j] [i] C i j 1 0 C[i+1][j+1]=dp+C[i][j+1]+C[i+1][j]-C[i][j] C[i+1][j+1] [i+1] C i+1 i 1 j+1 j 1 dp+C[i][j+1]+C[i+1][j]-C[i][j] dp+C[i][j+1]+C[i+1][j] dp+C[i][j+1] dp C[i][j+1] [i] C i j+1 j 1 C[i+1][j] [i+1] C i+1 i 1 j C[i][j] [i] C i j C[i+1][j+1]%=MOD C[i+1][j+1] [i+1] C i+1 i 1 j+1 j 1 MOD print(C[n][m]+1) print C[n][m]+1 C[n][m] [n] C n m 1 resolve() resolve def resolve():
    n,m=map(int,input().split())
    S=list(map(int,input().split()))
    T=list(map(int,input().split()))
    C=[[0]*(m+1) for _ in range(n+1)]
    from itertools import product
    for i,j in product(range(n),range(m)):
        dp=C[i][j]+1 if(S[i]==T[j]) else 0
        C[i+1][j+1]=dp+C[i][j+1]+C[i+1][j]-C[i][j]
        C[i+1][j+1]%=MOD
    print(C[n][m]+1) def resolve():
    n,m=map(int,input().split())
    S=list(map(int,input().split()))
    T=list(map(int,input().split()))
    C=[[0]*(m+1) for _ in range(n+1)]
    from itertools import product
    for i,j in product(range(n),range(m)):
        dp=C[i][j]+1 if(S[i]==T[j]) else 0
        C[i+1][j+1]=dp+C[i][j+1]+C[i+1][j]-C[i][j]
        C[i+1][j+1]%=MOD
    print(C[n][m]+1) resolve INF=float("inf") float("inf") INF input=lambda :sys.stdin.readline().rstrip() lambda :sys.stdin.readline().rstrip() input MOD=10**9+7 10**9+7 MOD
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    S=list(map(int,input().split()))
    T=list(map(int,input().split()))
    C=[[0]*(m+1) for _ in range(n+1)]
    from itertools import product
    for i,j in product(range(n),range(m)):
        dp=C[i][j]+1 if(S[i]==T[j]) else 0
        C[i+1][j+1]=dp+C[i][j+1]+C[i+1][j]-C[i][j]
        C[i+1][j+1]%=MOD
    print(C[n][m]+1)
resolve()