Python: explained in pictures and math
Approach\nSince there is no chance to simulate actual robots movement considering the problem constraints, let\'s try to find another way.\n\nFirst thing which 
# Approach\nSince there is no chance to simulate actual robots movement considering the problem constraints, let\'s try to find another way.\n\nFirst thing which is crusial for solving this problem is to understand what will be the final position of robots. The situation when there are no other robots on the way is illustrated on the figure below (in this example red robot moves to the left, white robot - to the right and `d = 5`) \n![2731_sol1.png](https://assets.leetcode.com/users/images/a63657b0-1d49-4a0a-b140-bebad967a6a3_1686600738.2467775.png)\n\nNow let\'s see if these robots stand on the same line and collide. Below the position of robots after each second is shown:\n![2731_sol2.png](https://assets.leetcode.com/users/images/9817b737-5d30-47de-bda7-543f8703dc4a_1686600756.8020415.png)\n\nIf we compare the robot\'s position at 5 s with the previous case we can see that the white robot stands at the same position the red robot would be if there were no collisions. The same is true for the red robot, it stands at the final coordinate the white robot would have without taking into account collisions. So we can assume the robots just pass through each other without colliding. That is the crucial hint necessary to solve the problem (I took it from the solutions of other users).\n\nNow we can easily calculate the final coordinates the robots will have after `d` seconds. If `s[i] == \'L\'` then final position is `nums[i] -= d`, and for `s[i] == \'R\'` the final position is `nums[i] += d`. Note that now there is no true correspondence between robots\' index and its calculated coordinate, but this is not important for this problem.\n\nNow we need to calculate the sum of distances between each pair of robots. For robot `i` the sum of distances will be equal to sum of distances to all robots on its left + sum of distances to all robots on the right. To find the distances without using `abs()` let\'s sort `nums` array of final distances. Below is a picture that helps us with evaluating the indices.\n\n![2731b.png](https://assets.leetcode.com/users/images/d3b2e105-dfc1-4ffa-9272-94717f9fe001_1686600803.8003035.png)\n\nNow let\'s do some math.\n\nIn sorted array of final distances the sum of distances from position `i` to every other position can be written as:<br>\n![2731_sol3.png](https://assets.leetcode.com/users/images/66d82c72-0f6b-4637-98bb-e98f28c2872a_1686600825.2975469.png)\n\nBasically that\'s all we need to solve the problem as we know how to calculate prefix sum and suffix sum in linear time. But let\'s go a little bit further and evaluate what the sum of all prefixes and suffixes looks like\n![2731_sol4.png](https://assets.leetcode.com/users/images/87d9aef0-3519-4beb-955e-340c2286aa8a_1686600863.530905.png)\n\n![2731_sol5.png](https://assets.leetcode.com/users/images/e7308c4c-c6c9-4cf6-a87a-70c353767379_1686600892.6719058.png)\n\nNow we can rewrite to sum of all distances between all pairs of robots as\n\n![2731_sol6.png](https://assets.leetcode.com/users/images/7214a9e0-83fd-4fe6-a75c-d3c7591d564e_1686600944.5564766.png)\n\n\nBut here the distance between each pair of robots is accounted twice, and since according to problem description the pairs (A,B) and (B,A) are considered as one pair, we can skip `2` multiplier in the resulting formula\n\n<ins>Time complexity:</ins> `O(n log n)`\n\n<ins>Space complexity:</ins> `O(1)`\n\n# Code\n```\nclass Solution:\n    def sumDistance(self, nums: List[int], s: str, d: int) -> int:\n        n = len(nums)\n        ans = 0\n        MOD = 1_000_000_007\n        \n        for i in range(n):\n            if s[i] == \'R\': nums[i] += d\n            else: nums[i] -= d\n        \n        nums.sort()\n        for i in range(n):\n            ans += nums[i] * ((i << 1) - n + 1)\n            ans %= MOD\n        return ans\n    # end sumDistance(...)\n```
['Python3']