<p>You wrote down many <strong>positive</strong> integers in a string called <code>num</code>. However, you realized that you forgot to add commas to seperate the different numbers. You remember that the list of integers was <strong>non-decreasing</strong> and that <strong>no</strong> integer had leading zeros.</p>

<p>Return <em>the <strong>number of possible lists of integers</strong> that you could have written down to get the string </em><code>num</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> num = &quot;327&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> You could have written down the numbers:
3, 27
327
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> num = &quot;094&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> No numbers can have leading zeros and all numbers must be positive.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> num = &quot;0&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> No numbers can have leading zeros and all numbers must be positive.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= num.length &lt;= 3500</code></li>
	<li><code>num</code> consists of digits <code>&#39;0&#39;</code> through <code>&#39;9&#39;</code>.</li>
</ul>

class Solution:
    def numberOfCombinations(self, s: str) -> int:
        MOD = 1000000007
        
        if s[0] == '0':
            return 0
        
        n = len(s)
        
        # Longest Common Prefix
        lcp = [[0] * n for _ in range(n)]
        for i in range(n-1, -1, -1):
            for j in range(n-1, -1, -1):
                if i == n-1 or j == n-1:
                    lcp[i][j] = 1 if s[i] == s[j] else 0
                elif s[i] == s[j]:
                    lcp[i][j] = 1 + lcp[i+1][j+1]

        dp = [[0] * (n+1) for _ in range(n+1)]
        for st in range(n, -1, -1):
            for pst in range(st-1, -1, -1):
                if st == n: # New staring poiint is n
                    dp[pst][st] = 1 if s[pst] != '0' else 0
                else:
                    ans = dp[pst][st+1] # Continue the previous number
                    if s[st] != '0' and s[pst] != '0':
                        if s[st] > s[pst]: # If first digit of prev number is lesser
                            if 2*st - pst <= n:
                                ans = (ans + dp[st][2*st-pst]) % MOD
                        elif s[st] < s[pst]: # If first digit of prev number is greater
                            if 2*st - pst + 1 <= n:
                                ans = (ans + dp[st][2*st-pst+1]) % MOD
                        else: # If first digit of prev number is equal with current
                            if lcp[pst][st] >= st-pst: # then check till what index its having same digits, Case where whole number is equal
                                if 2*st - pst <= n:
                                    ans = (ans + dp[st][2*st-pst]) % MOD
                            else: # checking the kth digit which is differing
                                if (pst + lcp[pst][st] < n and st + lcp[pst][st] < n):
                                    if s[pst + lcp[pst][st]] > s[st + lcp[pst][st]] and 2*st - pst + 1 <= n:
                                        ans = (ans + dp[st][2*st-pst+1]) % MOD
                                    elif s[pst + lcp[pst][st]] < s[st + lcp[pst][st]] and 2*st - pst <= n:
                                        ans = (ans + dp[st][2*st-pst]) % MOD
                    dp[pst][st] = ans
        
        return dp[0][1]

609500