<p>Given an integer array <code>nums</code>, return the sum of <code>floor(nums[i] / nums[j])</code> for all pairs of indices <code>0 &lt;= i, j &lt; nums.length</code> in the array. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>The <code>floor()</code> function returns the integer part of the division.</p>

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

<pre>
<strong>Input:</strong> nums = [2,5,9]
<strong>Output:</strong> 10
<strong>Explanation:</strong>
floor(2 / 5) = floor(2 / 9) = floor(5 / 9) = 0
floor(2 / 2) = floor(5 / 5) = floor(9 / 9) = 1
floor(5 / 2) = 2
floor(9 / 2) = 4
floor(9 / 5) = 1
We calculate the floor of the division for every pair of indices in the array then sum them up.
</pre>

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

<pre>
<strong>Input:</strong> nums = [7,7,7,7,7,7,7]
<strong>Output:</strong> 49
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

from typing import List

class Solution:
    def sumOfFlooredPairs(self, nums: List[int]) -> int:
        MOD = 10**9 + 7
        max_num = max(nums)
        
        # Step 1: Count occurrences of each number
        count = [0] * (max_num + 1)
        for num in nums:
            count[num] += 1
        
        # Step 2: Create a prefix sum array
        prefix_sum = [0] * (max_num + 1)
        for i in range(1, max_num + 1):
            prefix_sum[i] = prefix_sum[i - 1] + count[i]
        
        # Step 3: Calculate the sum of floored pairs
        result = 0
        for num in range(1, max_num + 1):
            if count[num] > 0:
                # For each multiple of num, calculate the contribution
                for k in range(1, (max_num // num) + 1):
                    start = k * num
                    end = min(max_num, (k + 1) * num - 1)
                    contribution = k * (prefix_sum[end] - prefix_sum[start - 1])
                    result = (result + count[num] * contribution) % MOD
        
        return result

# Example usage:
# solution = Solution()
# print(solution.sumOfFlooredPairs([2, 5, 9]))  # Output: 10
# print(solution.sumOfFlooredPairs([7, 7, 7, 7, 7, 7, 7]))  # Output: 49
30300