Trick question: let robots walk past each other.
Computing final positions\n\n First note that the final solution is only a function of the final robot positions, and robot indices are not important\n When rob
**Computing final positions**\n\n* First note that the final solution is only a function of the final robot positions, and robot indices are not important\n* When robots collide, they can "walk past each other" instead of colliding:\n * Collision type 1:\n   *  R | _ | L\n   *  _ | RL| _ = _ | LR | _\n   *  L | _ | R\n *  Collision type 2:\n    *  R | _ | _ | L\n    *  _ | R | L | _ = _ | L | R | _\n    *  L | _ | R\n *  Note that in both types of collision, we cannot tell wether the robots collided or simply walked past each other\n   *  Therefore, given a a robot at position `n`, we can easily compute its final position: `n+d` if moving right, or `n-d` if moving left\n\n**Computing sum of pairwise distances**\n\n   *  Now we need to find the distance between every pair of final positions, i.e. `sum(n_i - n_j), n_i>=n_j`. There are `N*(N-1)` such pairs.\n   *  If `n_i` is the smallest element, then it will always appear as the negative term when computing the difference with each of the other `N-1` other pairs. So it will contribute `- n_i * (N-1)` to the final result.\n   *  If `n_i` is the second smallest element, it will appear 1 time as the positive term and `N-2` times as the negative term when computing the difference with other pairs. So it will contribute `n_i * 1 - n_i * (N-2)` to the final result.\n   *  In general, the `k_th` smallest element will contribute `n_k * (left) - n_k*(right) = n_k * (left-right)`, where `left` and `right` are the number of elements smaller and larger than `n_k`.\n\n```\nclass Solution {\npublic:\n    int sumDistance(vector<int>& nums, string s, int d) {\n        long long sm = 0;\n        int MOD = 1e9+7;\n        int N = s.size();\n        for(int i=0;i<N;++i){\n            nums[i] += d*(s[i]==\'L\'? -1: 1);\n        }\n        sort(nums.begin(), nums.end());\n        for(int i=0;i<N;++i){\n            int left = i, right = N-(i+1);\n            sm += (long long)nums[i]*(left-right);\n            sm %= MOD;\n        }\n        return sm;\n    }\n};\n```
[]