Problem:


Score : 500 points

Problem Statement

You are given two integer sequences S and T of length N and M, respectively, both consisting of integers between 1 and 10^5 (inclusive).

In how many pairs of a subsequence of S and a subsequence of T do the two subsequences are the same in content?

Here the subsequence of A is a sequence obtained by removing zero or more elements from A and concatenating the remaining elements without changing the order.

For both S and T, we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Since the answer can be tremendous, print the number modulo 10^9+7.

Constraints

1 \leq N, M \leq 2 \times 10^3

The length of S is N.

The length of T is M.     

1 \leq S_i, T_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M}

Output

Print the number of pairs of a subsequence of S and a subsequence of T such that the subsequences are the same in content, modulo 10^9+7.

Sample Input 1

2 2
1 3
3 1

Sample Output 1

3

S has four subsequences: (), (1), (3), (1, 3).

T has four subsequences: (), (3), (1), (3, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 1 \times 1 pair of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (3), for a total of three pairs.

Sample Input 2

2 2
1 1
1 1

Sample Output 2

6

S has four subsequences: (), (1), (1), (1, 1).

T has four subsequences: (), (1), (1), (1, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 2 \times 2 pairs of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (1,1), for a total of six pairs.
Note again that we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Sample Input 3

4 4
3 4 5 6
3 4 5 6

Sample Output 3

16

Sample Input 4

10 9
9 6 5 7 5 9 8 5 6 7
8 6 8 5 5 7 9 9 7

Sample Output 4

191

Sample Input 5

20 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 5

846527861

Be sure to print the number modulo 10^9+7.



Sample input:
2 2
1 3
3 1


Sample output:
3

Submission status: Accepted

Solution code:
# compiled by pythran
"""
#pythran export main2()
import numpy as np

def calc(S, T, a):
    MOD = 10 ** 9 + 7
    for i in range(1, a.shape[0]):
        for j in range(1, a.shape[1]):
            a[i, j] = (a[i - 1, j] + a[i, j - 1] -
                       (a[i - 1, j - 1] if S[i - 1] != T[j - 1] else 0)) % MOD


def main2():
    stdin = open('/dev/stdin')
    stdin.readline()
    S = np.fromstring(stdin.readline(), np.uint32, -1, ' ')
    T = np.fromstring(stdin.readline(), np.uint32, -1, ' ')
    a = np.ones((len(S) + 1, len(T) + 1), dtype=np.uint32)
    calc(S, T, a)
    print(a[len(S), len(T)])
"""
import os, sys

p = os.path.dirname(__file__)
pymain = os.path.join(p, "a.out")
if len(sys.argv) == 1:
    import subprocess
    if p == "":
        pymain = "./" + pymain
    subprocess.call(pymain)
else:
    import gzip, base64, stat
    gz = b'H4sIAAx5Nl8C/+19D3xUxbX/3c2/5U+yCyKGP8KiCyYoIQkBkoC4CVm4KxuMJPBQwctmsyGrm93t7t0AttbYBMt2iaWtz8fray319fVhn1Ze/VPrayGIJNTX9iFV5I8oWIENIRCxILbK/s6Zmbt7781eCOj7/T6/z8dVMnu+d+bMmTNnzpyZvffOIzbHAr1Ox0kfPTePI9RYK6GtDLe2JbIAVsoNgb8TuQlcJtDp8nycVZFeZKyl1MDypbFypXpKl+qtinQ8yyelOlmawck/VkX617s5Rcpx5kQ5lDVvBUXzVqxSpK2M6bZMZTk9K9e/kqL9K1cpUk7PKVKpfensXx3D61i7pLSK5auS5cdPzXGxgejTwVrlsCrSJ1i+J1Tl7oZymdzgPyaWLmH1aenlQBqnSKV+mO711M8qme5tmOb1+MJrp60tnTVtVklByF9QTGQysbwLFy9N9LdeJvMohuH1Y33fte1a8NKDRy58/9bpT36v5v67T/4IrxXK+q1mQWDVpPxMHWIl8G82/Gv47Hfbf/G9ua/9yPW73cMe3STc9pFw/4b1rtEP/uOL054aH3hl1Obh89faH3w53/a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lO1X6U9tPp6q8dF/MZjbwVuguX/8+jp5Lmqa6bzNvRer2GlTpexw9vzRNdV9n6YrU8qrLn2LyS+Wl37etrPw2lfxqf/aJbGzJ72fsX6kcqJmqek0yhyCvX7rPqV8YXPszVOUTv/+zZLPu8uVzVOWl3wE3Oy9fv/TB504NsvLS75FbWfncK+hvDKu/UG3XTqX/vtz9w6meKbzIylvZobd4gONdKfzPEE55PnDid9EATY+NuLz/HqFRfsy32O/qusuX/z/JXyJKAIsAAA=='
    bin = gzip.decompress(base64.b64decode(gz))

    with open(pymain, "wb") as f:
        f.write(bin)
    os.chmod(pymain, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)

Submission status: Accepted

Solution code:

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

def main():
    mod=10**9+7
    N,M=MI()
    S=LI()
    T=LI()

    dp=[[0]*(M+1) for _ in range(N+1)]
    # dp[i][j]はSをi文字，Tをj文字見た時の通り数
    
    for i in range(N+1):
        dp[i][0]=1
        
    for j in range(M+1):
        dp[0][j]=1
    
    for i in range(N):
        for j in range(M):
            dp[i+1][j+1]=dp[i+1][j]+dp[i][j+1]
            if S[i]!=T[j]:
                dp[i+1][j+1]-=dp[i][j]
            dp[i+1][j+1]%=mod
            
            
    print(dp[-1][-1])
            
                
            
    

    

main()