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.

Solution in Common Lisp (SBCL 1.1.14) (judge verdict: Accepted)

;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam))
  #-swank (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (return (if minus (- result) result))))))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;; Body
(declaim (inline bitree-update!))
(defun bitree-update! (bitree index1 index2 delta)
  "destructively increments the array: array[index1][index2] += delta."
  (declare ((integer 0 #.most-positive-fixnum) index1 index2))
  (let ((length1 (array-dimension bitree 0))
        (length2 (array-dimension bitree 1)))
    (do ((i index1 (logior i (+ i 1))))
        ((>= i length1))
      (declare (fixnum i))
      (let ((base-i (array-row-major-index bitree i 0)))
        (declare (fixnum base-i))
        (do ((j index2 (logior j (+ j 1))))
            ((>= j length2))
          (declare (fixnum j))
          (setf (row-major-aref bitree (+ base-i j))
                (funcall (lambda (x y) (mod (+ x y) +mod+))
                         (row-major-aref bitree (the fixnum (+ base-i j)))
                         delta)))))
    bitree))
 
(declaim (inline bitree-sum))
(defun bitree-sum (bitree end1 end2)
  "returns the sum of the rectangle region: array[0][0] + ... +
array[end1-1][end2-1]."
  (declare ((integer 0 #.most-positive-fixnum) end1 end2))
  (let ((res 0))
    (declare (type uint62 res))
    (do ((i (- end1 1) (- (logand i (+ i 1)) 1)))
        ((< i 0))
      (declare (fixnum i))
      (let ((base-i (array-row-major-index bitree i 0)))
        (declare (fixnum base-i))
        (do ((j (- end2 1) (- (logand j (+ j 1)) 1)))
            ((< j 0))
          (declare (fixnum j))
          (incf res (row-major-aref bitree (the fixnum (+ base-i j)))))))
    (mod res +mod+)))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (m (read))
         (ss (make-array n :element-type 'uint32))
         (ts (make-array m :element-type 'uint32))
         (bitree (make-array '(2000 2000) :element-type 'uint32 :initial-element 0)))
    (declare (uint16 n m))
    (dotimes (i n) (setf (aref ss i) (read-fixnum)))
    (dotimes (i m) (setf (aref ts i) (read-fixnum)))
    (dotimes (i n)
      (dotimes (j m)
        (when (= (aref ss i) (aref ts j))
          (bitree-update! bitree i j (+ 1 (bitree-sum bitree i j))))))
    (println (mod (+ 1 (bitree-sum bitree n m)) +mod+))))

#-swank(main)