#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
ll cnts[2005][100005],cntt[2005][100005];
int main(){
  ll N,M;cin>>N>>M;
  ll S[N],T[M];
  ll i,j,k,l;
  rep(i,N) cin>>S[i];
  rep(i,M) cin>>T[i];
  repn(i,N-1){
    rep(j,100005){
      cnts[i][j]=cnts[i-1][j];
      if(S[i]==j) cnts[i][j]++;
    }
  }
  //cout<<1<<endl;
  repn(i,M-1){
    rep(j,100005){
      cntt[i][j]=cntt[i-1][j];
      if(T[i]==j) cntt[i][j]++;
    }
  }
  ll ans=1;
  rep(i,100005){
    ans*=cnts[N-1][i];
    ans%=mod;
  }
  rep(i,100005){
    ans*=cntt[N-1][i];
    ans%=mod;
  }
  cout<<ans<<endl;
}
