SOURCE

function solution3(nums) {
    let N = nums.length;
    let mod = Math.pow(10, 9) + 7;
    let K = mypow(2, N, mod) - 1;
    let sum = 0;
    for (num of nums) {
        sum = (sum + num) % mod;
    }
    let [inverseN, ano] = modInverse(N, mod);
    let result = (K * sum % mod) * inverseN % mod;
    return result;
}

function mypow(a, n, mod) {
    let res = 1;
    while (n > 0) {
        if (n % 2 === 1) res = (res * a) % mod;
        a = (a * a) % mod;
        n = Math.floor(n / 2);
    }
    return res;
}

// function modInverse(a, m) {
//     let m0 = m;
//     let a0 = a;
//     let m1 = m;
//     let x0 = 0;
//     let x1 = 1;
//     while (a0 > 1) {
//         let q = Math.floor(a0 / m0);
//         let t = m0;
//         m0 = a0 % m0;
//         a0 = t;
//         t = x0;
//         x0 = x1 - q * x0;
//         x1 = t;
//         console.log(a0, m0, x1, x0);
//     }
//     if (x1 < 0) x1 += m1;
//     return x1;
// }

function modInverse(a, m) {
    if (m === 0) {
        return [1, 0];
    }
    let [x, y] = modInverse(m, a % m);
    let t = y;
    y = x - (Math.floor(a / m) * y);
    x = t;
    return [x, y];
}

console.log(solution3([1, 2, 3, 4, 6, 5]));
console 命令行工具 X clear

                    
>
console