// Ancient Prophecy (D), by Errichto
// AC, O(n^2 log(n)), hash and binary search
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
const int nax = 5005;
const int mod = 1e9 + 7;
int t[nax];
char sl[nax];
// int dp[nax][nax];
// pref[a][b] = sum(dp[1..a][b])
int pref[nax][nax];
int h[nax][nax];
int cmp(int i, int j) {
if(t[i] < t[j]) return -1;
if(t[i] > t[j]) return 1;
return 0;
}
int cmp(int a, int b, int c, int d) {
if(t[a] != t[c]) return cmp(a, c);
if(h[a][b] == h[c][d]) return 0;
int low = 0, high = b - a;
while(low < high) {
int x = (low + high) / 2;
if(h[a][a+x] == h[c][c+x]) low = x + 1;
else high = x;
}
assert(t[a+low] != t[c+low]);
return cmp(a+low, c+low);
}
int dp(int a, int b) {
int x = pref[a][b] - pref[a-1][b];
if(x < 0) x += mod;
return x;
}
int main() {
int n;
scanf("%d", &n);
scanf("%s", sl);
RI(i, n) t[i] = sl[i-1] - '0';
RI(i, n) {
h[i][i] = t[i];
FOR(j, i+1, n)
h[i][j] = (11LL * h[i][j-1] + t[j]) % mod;
}
RI(i, n) {
// dp[1][i] = 1;
RI(j, n) pref[j][i] = 1;
}
FOR(c, 2, n) FOR(d, c, n) {
int & x = pref[c][d];
int b = c - 1;
int a = b - (d - c);
x = pref[b][b] - pref[max(a,0)][b];
if(x < 0) x += mod;
if(a >= 1 && cmp(a,b,c,d) == -1) {
x += dp(a, b);
if(x >= mod) x -= mod;
}
if(t[c] == 0) x = 0;
pref[c][d] = x + pref[c-1][d];
if(pref[c][d] >= mod) pref[c][d] -= mod;
}
int ans = 0;
RI(i, n) ans = (ans + dp(i, n)) % mod;
printf("%d\n", ans);
return 0;
}
Ly8gQW5jaWVudCBQcm9waGVjeSAoRCksIGJ5IEVycmljaHRvCi8vIEFDLCBPKG5eMiBsb2cobikpLCBoYXNoIGFuZCBiaW5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