class Solution: def threeSumMulti(self, arr: List[int], target: int) -> int: MOD = 10**9 + 7 # 0 <= arr[i] <= 100 count = [0] * 101 # num of occurrency every number in arr for x in arr: count[x] += 1 res = 0 # all same if target % 3 == 0: x = target // 3 if 0<=x<=100: res += count[x] * (count[x]-1) * (count[x]-2) / 6 res %= MOD # all different for x in range(101): for y in range(x+1, 101): z = target - x - y if y < z <= 100: res += count[x] * count[y] * count[z] res %= MOD # x == y != z for x in range(101): z = target - 2 * x if x < z <= 100: res += (count[x] * (count[x] - 1) / 2) * count[z] res %= MOD # x != y == z for x in range(101): if (target - x) % 2 == 0: y = (target - x) // 2 if x < y <= 100: res += (count[y] * (count[y] - 1) / 2) * count[x] res %= MOD return int(res)