Fast Python3 solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def sumImbalanceNumbers(self, nums: List[int]) -> int:\n        \n        n = len(nums)\n        \n        mp = collections.defaultdict(list)\n        for index in range(len(nums)):\n            mp[nums[index]].append(index)\n        \n        ans = int(( n * (n - 1) * (n + 1)) / 6)\n\n        for _, val in mp.items():\n            if len(val) == 1:\n                continue\n            for index in range(len(val) - 1):\n                i, j = val[index], val[index + 1]\n                cur = (i + 1) * (n - j)\n                ans -= cur\n                \n        num_unique = sorted(mp.keys())\n        for index in range(len(num_unique) - 1):\n            if num_unique[index + 1] - num_unique[index] != 1:\n                continue\n            \n            index_arr1 = mp[num_unique[index]]\n            index_arr2 = mp[num_unique[index + 1]]\n            for idx in range(len(index_arr2)):\n                j = index_arr2[idx]\n                \n                find_idx = bisect.bisect_right(index_arr1, j)\n                left_boundary = index_arr2[idx - 1] if idx > 0 else -1\n                \n                if find_idx > 0:\n                    find_idx -= 1\n                \n                while find_idx < len(index_arr1):\n                    i = index_arr1[find_idx]\n                    right_boundary = index_arr1[find_idx + 1] if find_idx + 1 < len(index_arr1) else n\n                    if min(i, j) >= left_boundary and max(i, j) <= right_boundary:\n                        ans -= (min(i, j) - left_boundary) * (right_boundary - max(i, j))\n                    find_idx += 1\n                    \n                       \n        return ans\n        \n```
['Python3']