#pragma warning(disable:4996)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("-ffloat-store")

#include <bits/stdc++.h>
using namespace std;
#define fast()                 ios_base::sync_with_stdio(false); cin.tie(NULL)
#define tc()                 ll t; cin>>t; while(t--)
#define pr(n)             cout<<n<<"\n"
#define in(n)                 ll n; cin>>n
#define mem(a, b)             memset(a, (b), sizeof(a))
#define FOR(i, j, k, in)     for (ll i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in)     for (ll i=j ; i>=k ; i-=in)
#define fr(i, n)             FOR(i, 0, n, 1)
#define rfr(i, n)             RFOR(i, j, 0, 1)
#define all(cont)             cont.begin(), cont.end()
#define rall(cont)             cont.end(), cont.begin()
#define gcd(a,b)              __gcd(a,b)
#define mp                     make_pair
#define pb                     push_back
#define X                     first
#define Y                     second
#define sz                     size
#define clr                 clear
#define len                 length
#define inf                 (ll)1e9
#define eps                 1e-9
#define pi                     3.1415926535897932384626433832795
#define h                     1000000007
#define gc                     getchar//_unlocked
#define pc                     putchar//_unlocked
const double                 PI=acos(-1.0);
#define pl(a, n)             for (ll i = 0; i < n; i++) cout << a[i] << ' ' ; br
#define pv(v)                 for (auto itv = v.begin(); itv != v.end(); itv++) cout << *itv << ' ' ; br
#define ps(s)                 for (auto its = s.begin(); its != s.end(); its++) cout << *its << ' '; br
#define pmp(m)                 for (auto itm = m.begin(); itm != m.end(); itm++) cout << (*itm).ff << ' ' << (*itm).ss << endl;
#define pmm(m)                 for (auto itm = m.begin(); itm != m.end(); itm++) cout << (*itm).ff << ' ' << (*itm).ss << endl;
#define p2(a, b)             cout << a << ' ' << b; br
#define p3(a, b, c)         cout << a << ' ' << b << ' ' << c; br
#define p4(a, b, c, d)         cout << a << ' ' << b << ' ' << c << ' ' << d; br
#define sum(a, n, s)         ll s = 0; for (ll i = 0; i < n; i++) s += a[i];
#define br                     cout << endl;
#define il(arr,n)             vll arr(n,0); fr(i,n) {cin>>arr[i];}
typedef long long int         ll;
typedef long double         ld;
typedef pair<ll,ll>         pii;
typedef vector<pii>         vpii;
typedef vector<ll>             vll;


signed main()
{
    fast();
    //cout<<std::fixed<<std::setprecision(35);
    in(n); in(m);
    il(s,n);
    il(t,m);
     ll n1 = n;
    ll n2 =  m; 
    ll dp[n1+1][n2+1];
    fr(i,n1+1)
        fr(j,n2+1)
            dp[i][j]=0;
    
  
    for (ll i = 1; i <= n1; i++) { 
        for (ll j = 1; j <= n2; j++) { 
            if (s[i - 1] == t[j - 1])  
                dp[i][j] = (1 + dp[i][j - 1] + dp[i - 1][j])%h;             
            else 
                dp[i][j] = (dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1]+h)%h;             
        } 
    } 
    cout<<(dp[n1][n2]+1)%h<<endl;
    // amit();
    return 0;
}

Measured memory_bytes: 31616