#include <bits/stdc++.h>
const int inf = 1e9 + 5;
const long long linf = 5e18 + 5;
const long double eps = 1e-9;
const long long mod = 1e9 + 7;
const int dd = 5e3 + 7;
const int maxn = 2e5 + 10;
using namespace std;
int dp[dd][dd];
int pref[dd][dd];
int past[dd];
long long step[dd];
long long hs[dd];
int find(int l1, int r1, int l2, int r2) {
  int l = 0;
  int r = r2 - l2 + 2;
  long long dopstep = step[r1 - l1 + 1];
  while (l != r - 1) {
    int m = (l + r) / 2;
    long long h1 = hs[l1 + m - 1];
    if (l1) h1 -= hs[l1 - 1];
    h1 *= dopstep;
    long long h2 = hs[l2 + m - 1];
    if (l2) h2 -= hs[l2 - 1];
    if (h2 == h1)
      l = m;
    else
      r = m;
  }
  return l;
}
vector<char> str;
int main() {
  ios_base::sync_with_stdio(0);
  if (0) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  } else {
  }
  long long x = 1;
  for (long long i = 0; i < dd; ++i) {
    step[i] = x;
    x *= (long long)17;
  }
  int n;
  cin >> n;
  str.resize(n);
  for (long long i = 0; i < n; ++i) cin >> str[i];
  x = 1;
  for (long long i = 0; i < str.size(); ++i) {
    hs[i] = ((long long)str[i]) * x;
    if (i) hs[i] += hs[i - 1];
    x *= (long long)17;
  }
  dp[0][0] = 1;
  for (long long l = 1; l < n + 1; ++l) {
    for (long long i = 0; i < n - l + 1; ++i) {
      if (str[i] == '0') {
        continue;
      }
      long long sum = 0;
      if (past[i]) sum += pref[i][past[i] - 1], sum %= mod;
      for (long long j = past[i]; j < l; ++j)
        sum += dp[i][j], sum %= mod, pref[i][j] = sum;
      past[i] = l;
      if (sum != pref[i][l - 1]) pref[i][l - 1] = sum;
      if (i >= l) {
        int p = find(i - l, i - 1, i, i + l - 1);
        if (p < l && str[i - l + p] < str[i + p]) sum += dp[i][l], sum %= mod;
      }
      dp[i + l][l] = sum;
    }
  }
  long long sum = 0;
  for (long long i = 0; i < n + 1; ++i) sum += dp[n][i], sum %= mod;
  printf("%d", sum);
  return 0;
}
