<p>Given an integer array <code>arr</code> and an integer <code>k</code>, modify the array by repeating it <code>k</code> times.</p>

<p>For example, if <code>arr = [1, 2]</code> and <code>k = 3 </code>then the modified array will be <code>[1, 2, 1, 2, 1, 2]</code>.</p>

<p>Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be <code>0</code> and its sum in that case is <code>0</code>.</p>

<p>As the answer can be very large, return the answer <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> arr = [1,2], k = 3
<strong>Output:</strong> 9
</pre>

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

<pre>
<strong>Input:</strong> arr = [1,-2,1], k = 5
<strong>Output:</strong> 2
</pre>

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

<pre>
<strong>Input:</strong> arr = [-1,-2], k = 7
<strong>Output:</strong> 0
</pre>

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

<ul>
	<li><code>1 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= arr[i] &lt;= 10<sup>4</sup></code></li>
</ul>

from typing import List


class Solution:
    def kConcatenationMaxSum(self, arr, k):
        MOD = 10**9 + 7

        def kadane(arr):
            max_ending_here = max_so_far = arr[0]
            for x in arr[1:]:
                max_ending_here = max(x, max_ending_here + x)
                max_so_far = max(max_so_far, max_ending_here)
            return max_so_far

        max_subarray_sum = kadane(arr)
        total_sum = sum(arr)
        max_prefix_sum = float('-inf')
        max_suffix_sum = float('-inf')
        current_prefix_sum = 0
        current_suffix_sum = 0

        # Calculate maximum prefix sum
        for i in range(len(arr)):
            j = len(arr) - i - 1
            current_prefix_sum += arr[i]
            current_suffix_sum += arr[j]

            max_prefix_sum = max(max_prefix_sum, current_prefix_sum)
            max_suffix_sum = max(max_suffix_sum, current_suffix_sum)

        if k == 1:
            return max(0, max_subarray_sum) % MOD

        # Calculate the maximum sum considering k > 1
        max_sum_k = max(0, max_subarray_sum, max_suffix_sum + max_prefix_sum + (k - 2) * max(0, total_sum))

        return max_sum_k % MOD
29700