C++/Python, solution with explanation
\n\ntc is O(nlogn), sc is O(1).\n### python\npython\nmod = int(1e9+7)\nclass Solution:\n    def sumDistance(self, nums: List[int], s: str, d: int) -> int:\n    
![image](https://assets.leetcode.com/users/images/489bd57d-9f2c-44df-8005-839d83eda1fc_1692859787.7895982.png)\n\ntc is O(nlogn), sc is O(1).\n### python\n```python\nmod = int(1e9+7)\nclass Solution:\n    def sumDistance(self, nums: List[int], s: str, d: int) -> int:\n        for i, ch in enumerate(s):\n            nums[i] += d if ch == \'R\' else -d\n        nums.sort()\n        prefix = nums[0]\n        ans = 0\n        for i in range(1, len(nums)):\n            ans += i * nums[i] - prefix\n            if ans >= mod: ans %= mod\n            prefix += nums[i]\n        return ans\n```\n### c++\n```cpp\nconst int mod = 1e9+7;\nclass Solution {\npublic:\n    int sumDistance(vector<int>& nums, string s, int d) {\n        for(int i = 0; i < s.size(); i+=1) nums[i] += s[i] == \'R\'? d:-d;\n        sort(nums.begin(), nums.end());\n        long long ans = 0, prefix = nums[0];\n        for (int i = 1; i < nums.size(); i+=1) {\n            ans = (ans + (long long) i * nums[i] - prefix) % mod;\n            prefix += nums[i];\n        }\n        return ans;\n    }\n};\n```
['C', 'Sorting', 'Prefix Sum', 'Python']