Javascript - Sort + PreSum + Math
Code\n\n/**\n * @param {number[]} nums\n * @param {string} s\n * @param {number} d\n * @return {number}\n */\nvar sumDistance = function (nums, s, d) {\n  const
# Code\n```\n/**\n * @param {number[]} nums\n * @param {string} s\n * @param {number} d\n * @return {number}\n */\nvar sumDistance = function (nums, s, d) {\n  const n = nums.length;\n  const MOD = Math.pow(10, 9) + 7;\n  for (let i = 0; i < n; i++) {\n    nums[i] = s[i] === \'R\' ? nums[i] + d : nums[i] - d;\n  }\n\n  nums.sort((a, b) => a - b);\n  const preSum = nums.slice();\n\n  let ans = 0;\n  for (let i = 1; i < n; i++) {\n    ans = (ans + (i * nums[i] - preSum[i - 1])) % MOD;\n    preSum[i] = (nums[i] + preSum[i - 1]) % MOD;\n  }\n\n  return ans;\n};\n\n```
['Math', 'Sorting', 'Prefix Sum', 'JavaScript']