<p>You are given an array <code>nums</code> of <code>n</code> positive integers and an integer <code>k</code>.</p>

<p>Initially, you start with a score of <code>1</code>. You have to maximize your score by applying the following operation at most <code>k</code> times:</p>

<ul>
	<li>Choose any <strong>non-empty</strong> subarray <code>nums[l, ..., r]</code> that you haven&#39;t chosen previously.</li>
	<li>Choose an element <code>x</code> of <code>nums[l, ..., r]</code> with the highest <strong>prime score</strong>. If multiple such elements exist, choose the one with the smallest index.</li>
	<li>Multiply your score by <code>x</code>.</li>
</ul>

<p>Here, <code>nums[l, ..., r]</code> denotes the subarray of <code>nums</code> starting at index <code>l</code> and ending at the index <code>r</code>, both ends being inclusive.</p>

<p>The <strong>prime score</strong> of an integer <code>x</code> is equal to the number of distinct prime factors of <code>x</code>. For example, the prime score of <code>300</code> is <code>3</code> since <code>300 = 2 * 2 * 3 * 5 * 5</code>.</p>

<p>Return <em>the <strong>maximum possible score</strong> after applying at most </em><code>k</code><em> operations</em>.</p>

<p>Since the answer may be large, return it modulo <code>10<sup>9 </sup>+ 7</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [8,3,9,3,8], k = 2
<strong>Output:</strong> 81
<strong>Explanation:</strong> To get a score of 81, we can apply the following operations:
- Choose subarray nums[2, ..., 2]. nums[2] is the only element in this subarray. Hence, we multiply the score by nums[2]. The score becomes 1 * 9 = 9.
- Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 1, but nums[2] has the smaller index. Hence, we multiply the score by nums[2]. The score becomes 9 * 9 = 81.
It can be proven that 81 is the highest score one can obtain.</pre>

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

<pre>
<strong>Input:</strong> nums = [19,12,14,6,10,18], k = 3
<strong>Output:</strong> 4788
<strong>Explanation:</strong> To get a score of 4788, we can apply the following operations: 
- Choose subarray nums[0, ..., 0]. nums[0] is the only element in this subarray. Hence, we multiply the score by nums[0]. The score becomes 1 * 19 = 19.
- Choose subarray nums[5, ..., 5]. nums[5] is the only element in this subarray. Hence, we multiply the score by nums[5]. The score becomes 19 * 18 = 342.
- Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 2, but nums[2] has the smaller index. Hence, we multipy the score by nums[2]. The score becomes 342 * 14 = 4788.
It can be proven that 4788 is the highest score one can obtain.
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length == n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= k &lt;= min(n * (n + 1) / 2, 10<sup>9</sup>)</code></li>
</ul>

import math

def prime_score(x: int) -> int:
    num_primes = 0
    while x > 1:
        sq_root = int(math.sqrt(x))
        is_prime = True
        for f in range(2, sq_root + 1):
            if x % f == 0:
                num_primes += 1
                while x % f == 0:
                    x //= f
                is_prime = False
                break
        if is_prime:
            num_primes += 1
            break
    return num_primes

def pow_modulo(base: int, power: int, modulo: int) -> int:
    result = 1
    if power == 0:
        return 1
    if power == 1:
        return base % modulo
    if power % 2 == 0:
        return (pow_modulo(base, power // 2, modulo) ** 2) % modulo
    return ((pow_modulo(base, (power - 1) // 2, modulo) ** 2) * base) % modulo

    
class Solution:
    def maximumScore(self, nums: List[int], k: int) -> int:
        prime_scores = [prime_score(x) for x in nums]
        answer = 1
        # print(prime_scores)
        modulo = 10 ** 9 + 7
        num_to_pow = {}
        
        left_bound = [0]
        left_stack = [0]
        for idx in range(1, len(nums)):
            while left_stack and prime_scores[left_stack[-1]] < prime_scores[idx]:
                left_stack.pop()
            if left_stack:
                left_bound.append(left_stack[-1] + 1)
            else:
                left_bound.append(0)
            left_stack.append(idx)

        right_stack = [len(nums) - 1]
        right_bound = [0] * len(nums)
        right_bound[-1] = len(nums) - 1
        for idx in range(len(nums) - 1, -1, -1):
            # print("idx=%d right_stack=%s right_bound=%s" % (idx, right_stack, right_bound))
            while right_stack and prime_scores[right_stack[-1]] <= prime_scores[idx]:
                right_stack.pop()
            if right_stack:
                right_bound[idx] = right_stack[-1] - 1
            else:
                right_bound[idx] = len(nums) - 1
            right_stack.append(idx)
        

        #print(prime_scores)
        #print(left_bound)
        #print(right_bound)
        for x, idx in sorted([(x, i) for i, x in enumerate(nums)], reverse=True):
            if x == 1:
                continue
            # print("x=%d" % x)
            #i = idx
            #j = idx
            # while i > 0 and prime_scores[i - 1] < prime_scores[idx]:
            #    i -= 1
            #while j < len(nums) - 1 and prime_scores[j + 1] <= prime_scores[idx]:
            #    j += 1
            i = left_bound[idx]
            j = right_bound[idx]
            num_pairs = (idx - i + 1) * (j - idx + 1)
            num_to_use = min(k, num_pairs)
            if x not in num_to_pow:
                num_to_pow[x] = 0
            num_to_pow[x] += num_to_use
            # print("x=%d i=%d j=%d num_pairs=%d num_to_use=%d answer=%d" % (x, i, j, num_pairs, num_to_use, answer))
            k -= num_to_use
            if k == 0:
                break
        answer = 1
        for num, degree in num_to_pow.items():
            answer = (answer * pow_modulo(num, degree, modulo)) % modulo
        return answer

            

 
41700