Given an integer N and an array of N integers. Calculate the sum of products of all pairs of integers of the array.
Note: Since the answer can be large, return the answer modulo 10^{9}+7.
Example 1:
Input:
N=3
A=[1,2,3]
Output:
11
Explanation:
1x2+2x3+1x3=11
So, the answer is 11.
Example 2:
Input:
N=3
A=[2,2,3]
Output:
16
Explanation:
2x2+2x3+3x2=16.
So, the answer is 16.
Your Task:
You don't need to read input or print anything. Your task is to complete the function productOfPairs() which takes the integer N and array A as input parameters and returns the sum of the product of all pairs of integers from the array.
Expected Time Complexity:O(N)
Expected Auxillary Space:O(1)
 
Constraints:
1<=N,A[i]<=10^{6}

Source: geeksforgeeks | Difficulty: EASY

Starter code: #User function Template for python3

class Solution:
    def productOfPairs(self,N,A):
        #code here

Solutions:
class Solution:

	def productOfPairs(self, N, A):
		total = 0
		res = 0
		for i in range(N):
			res += total * A[i]
			res %= 1000000007
			total += A[i]
		return res


MOD = 1000000007

class Solution:

	def productOfPairs(self, N, A):
		total = sum(A) % MOD
		ans = 0
		acc = 0
		for i in range(N):
			I = A[i] % MOD
			acc += I
			acc = acc % MOD
			ans += I * ((total - acc) % MOD) % MOD
		return ans % MOD


class Solution:

	def productOfPairs(self, N, A):
		m = 10 ** 9 + 7
		array_sum = 0
		for i in range(0, N, 1):
			array_sum = array_sum + A[i]
		array_sum_square = array_sum * array_sum
		individual_square_sum = 0
		for i in range(0, N, 1):
			individual_square_sum += A[i] * A[i]
		res = (array_sum_square - individual_square_sum) // 2
		return res % m


class Solution:

	def productOfPairs(self, N, A):
		ans = 0
		Sum = 0
		Mod = 1000000007
		for i in range(N):
			ans += Sum * A[i] % Mod
			ans %= Mod
			Sum += A[i]
			Sum %= Mod
		return ans


class Solution:

	def productOfPairs(self, N, A):
		prefix_sum = [0] * N
		prefix_sum[0] = A[0]
		presum = A[0]
		for i in range(1, N):
			presum += A[i]
			prefix_sum[i] = presum
		prodsum = 0
		for i in reversed(range(1, N)):
			prodsum += A[i] * prefix_sum[i - 1]
		return prodsum % 1000000007


class Solution:

	def productOfPairs(self, n, a):
		sm = sum(a)
		sq = 0
		for i in a:
			sq += i * i
		res = (sm * sm - sq) // 2
		return res % 1000000007


class Solution:

	def productOfPairs(self, N, A):
		pre = [0] * N
		pre[0] = A[0]
		for i in range(1, N):
			pre[i] = pre[i - 1] + A[i]
		m = 0
		for i in range(0, N):
			m = m + A[i] * (pre[N - 1] - pre[i])
		return m % 1000000007


class Solution:

	def productOfPairs(self, N, A):
		s = 0
		prefix = [0] * N
		prefix[0] = A[0]
		for i in range(1, N):
			prefix[i] = prefix[i - 1] + A[i]
		for i in range(N):
			s = s + A[i] * (prefix[N - 1] - prefix[i])
		return s % 1000000007