<p>Given an integer array <code>arr</code>, and an integer <code>target</code>, return the number of tuples <code>i, j, k</code> such that <code>i &lt; j &lt; k</code> and <code>arr[i] + arr[j] + arr[k] == target</code>.</p>

<p>As the answer can be very 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> arr = [1,1,2,2,3,3,4,4,5,5], target = 8
<strong>Output:</strong> 20
<strong>Explanation: </strong>
Enumerating by the values (arr[i], arr[j], arr[k]):
(1, 2, 5) occurs 8 times;
(1, 3, 4) occurs 8 times;
(2, 2, 4) occurs 2 times;
(2, 3, 3) occurs 2 times.
</pre>

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

<pre>
<strong>Input:</strong> arr = [1,1,2,2,2,2], target = 5
<strong>Output:</strong> 12
<strong>Explanation: </strong>
arr[i] = 1, arr[j] = arr[k] = 2 occurs 12 times:
We choose one 1 from [1,1] in 2 ways,
and two 2s from [2,2,2,2] in 6 ways.
</pre>

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

<pre>
<strong>Input:</strong> arr = [2,1,3], target = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> (1, 2, 3) occured one time in the array so we return 1.
</pre>

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

<ul>
	<li><code>3 &lt;= arr.length &lt;= 3000</code></li>
	<li><code>0 &lt;= arr[i] &lt;= 100</code></li>
	<li><code>0 &lt;= target &lt;= 300</code></li>
</ul>

class Solution {
public:
    int threeSumMulti(std::vector<int>& arr, int target) {
        int n = arr.size();
        std::sort(arr.begin(), arr.end());
        int mod = 1e9+7;

        std::unordered_map<long long, long long> freq;
        for (int num : arr) {
            freq[num]++;
        }

        std::vector<int> nums = arr;
        std::vector<std::vector<int>> ans;

        for (int i = 0; i < n - 2; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;

            for (int j = i + 1; j < n - 1; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;

                int target1 = target - nums[i] - nums[j];
                int st = j + 1, end = n - 1;

                while (st <= end) {
                    int mid = st + (end - st) / 2;
                    if (nums[mid] == target1) {
                        ans.push_back({nums[i], nums[j], nums[mid]});
                        break;
                    } else if (nums[mid] > target1) {
                        end = mid - 1;
                    } else {
                        st = mid + 1;
                    }
                }
            }
        }

        long long final_ans = 0;
        for (const auto& triplet : ans) {
            int a = triplet[0], b = triplet[1], c = triplet[2];
            if (a == b && b == c) {
                final_ans = (final_ans + (((freq[a] ) * (freq[a] - 1)) * (freq[a] - 2)) / 6) % mod;
            } else if (a != b && b != c && c != a) {
                final_ans = (final_ans + (freq[a] % mod * freq[b] % mod * freq[c] % mod) % mod) % mod;
            } else if (a == b && b != c) {
                final_ans = (final_ans + (((freq[a] % mod) * (freq[a] - 1) % mod / 2) % mod * freq[c] % mod) % mod) % mod;
            } else if (a != b && b == c) {
                final_ans = (final_ans + (freq[a] % mod * (freq[b] % mod * (freq[b] - 1) % mod / 2) % mod) % mod) % mod;
            }
        }

        return static_cast<int>(final_ans);
    }
};
17100