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 C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

//#define _GLIBCXX_DEBUG

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>
#include<complex>

using namespace std;

#define ll long long
#define ld long double
#define EPS 1e-9
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;
typedef vector<vi> vvi;

int gcd(int a, int b){if(b==0) return a;return gcd(b,a%b);}
int lcm(int a, int b){return a/gcd(a,b)*b;}

typedef int SegT;
const SegT defvalue=0;
SegT combine(SegT a,SegT b){return a+b%MOD;}
class SegTree{
	public:
		vector<SegT>val;
		int n;
		SegTree(int size){
			n=1;
			while(n<size)n<<=1;
			val=vector<SegT>(2*n,defvalue);
		}
		SegTree(const vector<SegT> &in){
			n=1;
			while(n<in.size())n<<=1;
			val=vector<SegT>(2*n,defvalue);
			for(int i=n-1+in.size()-1;i>=0;i--){
				if(n-1<=i)val[i]=in[i-(n-1)];
				else val[i]=combine(val[i*2+1],val[i*2+2]);
			}
		}
		void update(int i,SegT a){
			i+=n-1;
			val[i]+=a;
			while(i>0){
				i=(i-1)/2;
				val[i]=combine(val[i*2+1],val[i*2+2]);
			}
		}
		SegT query(int a,int b,int k=0,int l=0,int r=-1){//[a,b)
			if(r==-1)r=n;
			if(r<=a||b<=l)return defvalue;
			if(a<=l&&r<=b)return val[k];
			else return combine(query(a,b,k*2+1,l,(l+r)/2),query(a,b,k*2+2,(l+r)/2,r));
		}
		SegT get(int x){
			return val[n-1+x];
		}
		void tmp(){
			rep(i,val.size())cout<<" "<<val[i];cout<<endl;
		}
};
class SegTree_2d{
	public:
		vector<SegTree>val;
		int n;
		SegTree_2d(int h,int w){
			n=1;
			while(n<h)n<<=1;
			val=vector<SegTree>(2*n,SegTree(w));
		}
		SegTree_2d(const vector<vector<SegT> > &in){
			n=1;
			while(n<in.size())n<<=1;
			val=vector<SegTree>(2*n,SegTree(in[0].size()));
			for(int i=n-1+in.size()-1;i>=0;i--){
				if(i-(n-1)<0)
					rep(j,val[i].val.size())
						val[i].val[j]=combine(val[i*2+1].val[j],val[i*2+2].val[j]);
				else val[i]=SegTree(in[i-(n-1)]);
			}
		}
		void update(int x,int y,SegT a){
			x+=n-1;
			val[x].update(y,a);
			while(x>0){
				x=(x-1)/2;
				val[x].update(y,a);
//				val[x].update(y,combine(val[x*2+1].val[y+n-1],val[x*2+2].val[y+n-1]));
			}
		}
		SegT query(int x1,int y1,int x2,int y2,int k=0,int l=0,int r=-1){//[a,b)
			if(r==-1)r=n;
			if(r<=x1||x2<=l)return defvalue;
			if(x1<=l&&r<=x2)return val[k].query(y1,y2);
			else return combine(query(x1,y1,x2,y2,k*2+1,l,(l+r)/2),query(x1,y1,x2,y2,k*2+2,(l+r)/2,r));
		}
		SegT get(int x,int y){
			return val[n-1+x].get(y);
		}
		void tmp(){
			rep(i,val.size())val[i].tmp();
		}
};


signed main(void) {
    int n,m;
    cin >> n >> m;
    vi a(n),b(m);
    rep(i,n)cin >> a[i];
    rep(i,m)cin >> b[i];
    SegTree_2d st(n,m);
    int ans = 1;
    rep(i,n)rep(j,m){
        if(a[i] == b[j]){
            int t = 0;
            if(i&&j)t = st.query(0,0,i,j);
            st.update(i,j,t+1);
            //cout << i << " " << j << " " << t+1 << endl;
            (ans += t+1) %= MOD;
        }
    }
    /*
    dp[0][0] = 0;
    int ans = 1;
    rep(i,n)rep(j,m){
        if(i&&j)dp[i][j] += dp[i-1][j-1];
        if(a[i] == b[j]){
            (ans += dp[i][j]+1) %= MOD;
            dp[i][j] *= 2;
            dp[i][j]++;
            dp[i][j] %= MOD;
            cout << i << " " << j << " " << dp[i][j] << " " << ans << endl;
        }
        //if(i)dp[i][j] += dp[i-1][j];
        //if(j)dp[i][j] += dp[i][j-1];
    }
    //rep(i,n)rep(j,m)cout << " " << i << " " << j << " " << dp[i][j] << endl;
*/
    cout << ans << endl;
}