<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, num: str) -> int:
        
        mod = int(10**9) + 7
        def partitions(n):
            partition_table = [1] + [0] * n
            for i in range(1, n + 1):
                for j in range(i, n + 1):
                    partition_table[j] += partition_table[j - i]
                    partition_table[j] %= mod
            return partition_table[n]%mod


        if len(set(num)) == 1:
            return 0 if num[0]=='0' else partitions(len(num))
        @cache
        def dp(idx, prev):
            if idx == len(num):
                return 1
            if num[idx] == '0':
                return 0
            res, acc = 0, 0
            for i in range(idx, len(num)):
                acc = acc*10 + int(num[i])
                if acc >= prev:
                    res += dp(i+1, acc)
                    res%=mod
            return res
        return dp(0, float('-inf'))
        '''
        
        mod = int(10**9) + 7
        def partitions(n):
            partition_table = [1] + [0] * n
            for i in range(1, n + 1):
                for j in range(i, n + 1):
                    partition_table[j] += partition_table[j - i]
                    partition_table[j] %= mod
            return partition_table[n]%mod


        if len(set(num)) == 1:
            return 0 if num[0]=='0' else partitions(len(num))
            
        s = num
        def ranks(l):
            index = {v: i for i, v in enumerate(sorted(set(l)))}
            return [index[v] for v in l]

        def suffixArray(s):
            line = ranks(s)
            n, k, ans, sa = len(s), 1, [line], [0]*len(s)
            while k < n - 1:
                line = ranks(list(zip_longest(line, islice(line, k, None), fillvalue=-1)))
                ans, k = ans + [line], k << 1
            for i, k in enumerate(ans[-1]): sa[k] = i
            return ans, sa

        @lru_cache(None)
        def compare(i, j, l, k):
            a = (c[k][i], c[k][(i+l-(1<<k))%n])
            b = (c[k][j], c[k][(j+l-(1<<k))%n])
            return 0 if a == b else 1 if a < b else -1

        c, _ = suffixArray([int(i) for i in s])

        n, M = len(s), 10**9 + 7
        dp = [[0]*(n+1) for _ in range(n+1)]
        mp = [[0]*(n+1) for _ in range(n+1)]

        for k in range(n+1):
            dp[0][k] = 1
            mp[0][k] = 1

        for i in range(1, n+1):
            for k in range(1, i + 1):
                if s[i-k] == "0": continue
                dp[i][k] = mp[i-k][k-1]
                if i >= 2*k and compare(i-2*k, i-k, k, floor(log2(k))) >= 0:
                    dp[i][k] += dp[i-k][k]

            for k in range(n + 1):
                mp[i][k] = mp[i][k-1] + dp[i][k]

        return mp[-1][-1]
        '''
1638