/* What we know is a drop , but what we don't know is an ocean !*/

#include <bits/stdc++.h>
#define ll long long int
#define F(i,j,k,in) for(int i=j;i<k;i+=in)
#define DF(i,j,k,in) for(int i=j;i>=k;i-=in)
#define feach(it,l) for (auto it = l.begin(); it != l.end(); ++it)
#define fall(a) a.begin(),a.end()
#define sz(x) (int)x.size()
#define szs(x) (int)x.length()
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fs first
#define ss second
#define mkp make_pair
#define endl "\n"
using namespace std;

typedef vector<ll> vll;
typedef vector<int> vin;
typedef vector<char> vch;
typedef vector<string> vst;
typedef set<ll> sll;
typedef set<int> sint;
typedef set<char> sch;
typedef set<string> sst;
typedef queue<ll> qll;
typedef queue<int> qin;
typedef queue<char> qch;
typedef deque<ll> dqll;
typedef deque<int> dqint;
typedef deque<char> dqch;
typedef priority_queue<ll> pqll;
typedef priority_queue<int> pqin;
typedef priority_queue<char> pqch;
typedef map<ll,ll> mpll;
typedef map<int,int> mpin;
typedef pair<ll,ll> pll;
typedef pair<int,int> pin;

const ll MOD=1000000007;
const long double PI=3.1415926535897932384626433832795;

ll pwr(ll b,ll p){ll res=1;while(p){if(p&1) {res*=b; p--;}else{b*=b; p>>=1;}}return res;}
ll mpwr(ll b,ll p){ll res=1;while(p){if(p&1) {res=(res*b)%MOD; p--;}else{b=(b*b)%MOD; p>>=1;}}return res;}

int msb (int n) {return 31-__builtin_clz(n);}
ll msbl (ll n) {return 63-__builtin_clzll(n);}
int lsb (int n) {return __builtin_ctz(n);}
ll  lsbl (ll n) {return __builtin_ctzll(n);}
int setbit (int n) {return __builtin_popcount(n);}
ll setbitl (ll n) {return __builtin_popcountll(n);}
const int maxn = 3005;
ll dp [maxn][maxn];

int main()
{
ios_base::sync_with_stdio(false) , cin.tie(NULL) , cout.tie(NULL);
ll n; cin>>n;
vll a (n);
F (i,0,n,1) cin>>a[i];

F (i,0,n,1) {
	F (j,0,n,1) dp[i][j]=0;
}

F (i,0,n,1) dp[i][1]=1;

F (j,2,n+1,1) {
	ll s=0;
	vll sum (n,0);
	F (i,0,n,1) {
		s=(s+a[i])%j;
		dp[i][j]=sum[s];
		sum[s]=(sum[s]+dp[i][j-1])%MOD;
	}
}
ll ans=0;
F (i,1,n+1,1) ans=(ans+dp[n-1][i])%MOD;
cout<<ans;
return 0;
}
/*Maybe you are searching among the branches for what only appears in the roots.*/
#include<bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define MOD 1000000007
//#define MOD 998244353
#define INF 1000000010000
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long
#define endl "\n"
#define N 500010

typedef pair<int,int> PII;
typedef pair<int,string> PIS;
typedef pair<string,int> PSI;

template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b; 
    return true;
  }
  return false;
}

template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

struct Node{
  vector<PII> edges;//first=cost,second=index

  bool visit;
  int cost;
};

vector<Node> mp(N);

void dijkstra(int st,int n){
  rep(i,n){
    mp[i].visit=false;
    mp[i].cost= INF;
  }
  priority_queue<PII>  q;
  mp[st].cost=0;
  rep(i,mp[st].edges.size() ){
    mp[mp[st].edges[i].S].cost=mp[st].edges[i].F;
    q.push({-mp[st].edges[i].F,mp[st].edges[i].S});
  }
  mp[st].visit=true;
  while(!q.empty() ){
    PII p=q.top();
    int u=p.S;
    q.pop();
    
    if(mp[u].cost < -p.F) continue;
    mp[u].visit=true;
    rep(i,mp[u].edges.size() ){
      int v=mp[u].edges[i].S;
      if(!mp[v].visit){
	if(mp[v].cost> mp[u].cost+mp[u].edges[i].F){
	  mp[v].cost=mp[u].cost+mp[u].edges[i].F;
	  q.push({-mp[v].cost,v});
	}
      }
    }
  }
}

signed main(){
  cin.tie(0);	
  ios::sync_with_stdio(false);
  int r,c;
  cin>>r>>c;
  int A[r][c-1],B[r-1][c];
  rep(i,r) rep(j,c-1) cin>>A[i][j];
  rep(i,r-1) rep(j,c) cin>>B[i][j];
  
  rep(i,r){
    rep(j,c){
      int mn=(i*c)+j,mx=250000+mn;
      if(j<c-1) mp[mn].edges.push_back({A[i][j],mn+1});
      if(j>0) mp[mn].edges.push_back({A[i][j-1],mn-1});
      if(i<r-1) mp[mn].edges.push_back({B[i][j],mn+c});
      mp[mn].edges.push_back({1,mx});
      if(mx-c >= 250000) mp[mx].edges.push_back({1,mx-c});
      mp[mx].edges.push_back({0,mn});
    }
  }
      
  dijkstra(0,250000+(r*c) );
  
  /*rep(i,r){
    rep(j,c){
      cout<<mp[(i*c)+j].cost<<" ";
    }
    cout<<endl;
    }*/
  cout<<mp[r*c-1].cost<<endl;
  return 0;
}
