// Ancient Prophecy (D), by Errichto
// AC, O(n^2)
#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;
const int inf = 1e9 + 120;
int t[nax];
char sl[nax];
// int dp[nax][nax];
// pref[a][b] = sum(dp[1..a][b])
int pref[nax][nax];
int nxt[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 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';
for(int a = n; a >= 1; --a)
for(int b = n; b > a; --b) {
int & x = nxt[a][b];
if(t[a] != t[b]) x = a;
else if(b == n) x = inf;
else x = nxt[a+1][b+1];
}
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) {
int where = nxt[a][c];
if(where < c && t[where] < t[c+where-a]) {
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;
}
Ly8gQW5jaWVudCBQcm9waGVjeSAoRCksIGJ5IEVycmljaHRvCi8vIEFDLCBPKG5eMikKI2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBGT1IoaSxhLGIpIGZvcihpbnQgaSA9IChhKTsgaSA8PSAoYik7ICsraSkKI2RlZmluZSBSSShpLG4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