This problem is same as the previous one, but has larger constraints.

It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.

At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates $(x_i, y_i)$. Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.

Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?


-----Input-----

The first line contains a single integer $n$ ($2 \le n \le 1000$) — the number of electric poles.

Each of the following $n$ lines contains two integers $x_i$, $y_i$ ($-10^4 \le x_i, y_i \le 10^4$) — the coordinates of the poles.

It is guaranteed that all of these $n$ points are distinct.


-----Output-----

Print a single integer — the number of pairs of wires that are intersecting.


-----Examples-----
Input
4
0 0
1 1
0 3
1 2

Output
14

Input
4
0 0
0 2
0 4
2 0

Output
6

Input
3
-1 -1
1 0
3 1

Output
0



-----Note-----

In the first example:

 [Image] 

In the second example:

 [Image] 

Note that the three poles $(0, 0)$, $(0, 2)$ and $(0, 4)$ are connected by a single wire.

In the third example:

 [Image]

Difficulty: HARD

Solutions:
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
pts = []
for _ in range(int(input()) if True else 1):
	(x, y) = map(int, input().split())
	pts += [[x, y]]
ps = []
pss = set()
from fractions import Fraction
for i in range(len(pts) - 1):
	for j in range(i + 1, len(pts)):
		(x1, y1, x2, y2) = (pts[i][0], pts[i][1], pts[j][0], pts[j][1])
		(a, b, c) = (y1 - y2, x2 - x1, x1 * y2 - x2 * y1)
		g = gcd(a, b)
		(a, b) = (a // g, b // g)
		if a < 0 or (a == 0 and b < 0):
			a *= -1
			b *= -1
			c *= -1
		c = round(c / g, 3)
		xx = str(a) + ',' + str(b) + ',' + str(c)
		if xx not in pss:
			pss.add(xx)
			ps += [[a, b, c]]
ans = len(ps) * (len(ps) - 1) // 2
di = {}
for i in ps:
	xx = str(i[0]) + ',' + str(i[1])
	if xx in di:
		di[xx] += 1
	else:
		di[xx] = 1
for i in di:
	ans -= di[i] * (di[i] - 1) // 2
print(ans)


n = int(input())
T = []
P = []
for i in range(n):
	(x, y) = map(int, input().split())
	T.append([x, y])
for i in range(n):
	for j in range(i + 1, n):
		(x1, y1) = T[i]
		(x2, y2) = T[j]
		a = y2 - y1
		b = x1 - x2
		c = -(a * x1 + b * y1)
		if a != 0:
			b /= a
			c /= a
			a = 1
		else:
			a = 0
			c /= b
			b = 1
		P.append([a, b, c])
P.sort()
newp = [P[0]]
for i in range(len(P) - 1):
	if P[i] != P[i + 1]:
		newp.append(P[i + 1])
P = newp
sos = dict()
for (a, b, c) in P:
	if b != 0 and a != 0:
		z = a / b
	elif a == 0:
		z = 'kek'
	else:
		z = 'lol'
	if z not in sos:
		sos[z] = 1
	else:
		sos[z] += 1
sus = 0
sussqua = 0
for i in sos:
	sus += sos[i]
	sussqua += sos[i] ** 2
print((sus ** 2 - sussqua) // 2)


n = int(input())
x = [0 for i in range(0, n + 1)]
y = [0 for i in range(0, n + 1)]
for i in range(1, n + 1):
	(x[i], y[i]) = map(int, input().split())
di = {}
for i in range(1, n + 1):
	a = {}
	for j in range(i + 1, n + 1):
		if x[j] - x[i] == 0:
			if '0' not in a:
				a['0'] = 1
			else:
				a['0'] = a['0'] + 1
		else:
			k = (y[j] - y[i]) / (x[j] - x[i])
			if k not in a:
				a[k] = 1
			else:
				a[k] = a[k] + 1
	for (key, value) in a.items():
		if value > 1:
			continue
		if key in di:
			di[key] = di[key] + 1
		else:
			di[key] = 1
b = []
for v in di.values():
	b.append(v)
b.sort()
sum = 0
for i in b:
	sum += i
ans = 0
for i in b:
	ans += i * (sum - i)
	sum -= i
print(ans)


def gcd(x, y):
	if y == 0:
		return x
	else:
		return gcd(y, x % y)
from collections import *
n = int(input())
ps = []
for i in range(n):
	ps.append([int(i) for i in input().split(' ')])
xs = 0
ys = 0
lines = defaultdict(int)
cache = set()
xcache = set()
ycache = set()
for i in range(n):
	for j in range(i + 1, n):
		if ps[i][0] == ps[j][0]:
			if ps[i][0] in xcache:
				continue
			xcache.add(ps[i][0])
			xs += 1
			continue
		if ps[i][1] == ps[j][1]:
			if ps[i][1] in ycache:
				continue
			ycache.add(ps[i][1])
			ys += 1
			continue
		tmpp = sorted([ps[i], ps[j]])
		tmppp = (tmpp[1][0] - tmpp[0][0], tmpp[1][1] - tmpp[0][1])
		gcdd = gcd(abs(tmpp[1][0] - tmpp[0][0]), abs(tmpp[1][1] - tmpp[0][1]))
		tmppp = (tmppp[0] // gcdd, tmppp[1] // gcdd)
		kkk = (tmppp[1], tmppp[0])
		if tmppp[1] < 0:
			kkk = (-tmppp[1], -tmppp[0])
		qqq = (tmppp[1] * -ps[i][0] + tmppp[0] * ps[i][1], tmppp[0])
		if qqq[0] < 0:
			qqq = (-qqq[0], -qqq[1])
		line = (kkk, qqq)
		if line in cache:
			continue
		cache.add(line)
		lines[kkk] += 1
tmp = xs + ys + sum(lines.values())
print((tmp * (tmp - 1) - sum((v * (v - 1) for v in lines.values())) - xs * (xs - 1) - ys * (ys - 1)) // 2)


from math import *
from itertools import *
n = int(input())
map = {}
l = []
r = []
now = 0
tot = 0
lth = []
for _ in range(n):
	s = input().split(' ')
	l.append(int(s[0]))
	r.append(int(s[1]))
for i in range(n):
	for j in range(i + 1, n):
		if l[i] == l[j]:
			y = 9999999999999999999
			x = l[i]
		else:
			y = (r[i] - r[j]) / (l[i] - l[j])
			x = (r[j] * l[i] - l[j] * r[i]) / (l[i] - l[j])
		if not y in map:
			map[y] = {x: 1}
		elif x in map[y]:
			map[y][x] += 1
		else:
			map[y][x] = 1
for i in map:
	lth.append(len(map[i]))
	now += lth[len(lth) - 1]
for i in lth:
	tot += (now - i) * i
	now -= i
print(tot)


from math import *

class slopeC:

	def __init__(self):
		self.chil = set()
n = int(input())
slopes = {}
L = []
for i in range(n):
	(x, y) = map(int, input().split())
	for l in L:
		if x != l[0]:
			slope = (y - l[1]) / (x - l[0])
		else:
			slope = inf
		s1 = str(l[0]) + '-' + str(l[1])
		s2 = str(x) + '-' + str(y)
		if slope not in slopes:
			slopes[slope] = [slopeC()]
			slopes[slope][0].chil.add(s1)
			slopes[slope][0].chil.add(s2)
		else:
			f = 0
			for child in slopes[slope]:
				if s1 in child.chil:
					f = 1
					child.chil.add(s2)
					break
			if f == 0:
				slopes[slope] += [slopeC()]
				slopes[slope][0].chil.add(s1)
				slopes[slope][0].chil.add(s2)
	L += [[x, y]]
A = []
P = [0]
for s in slopes:
	A += [len(slopes[s])]
	P += [P[-1] + A[-1]]
ans = 0
for (i, v) in enumerate(A):
	ans += A[i] * (P[-1] - P[i + 1])
print(ans)


import math
import itertools
from collections import Counter
n = int(input())
coords = [[int(t) for t in input().split(' ')] for _ in range(n)]
lines = set()
for pair in itertools.combinations(coords, 2):
	l = min(pair)
	r = max(pair)
	a = r[1] - l[1]
	b = r[0] - l[0]
	c = r[1] * l[0] - l[1] * r[0]
	gcd = math.gcd(math.gcd(a, b), c)
	a /= gcd
	b /= gcd
	c /= gcd
	lines.add((a, b, c))
num_lines = len(lines)
count = Counter(((a, b) for (a, b, c) in lines))
result = num_lines * (num_lines - 1) // 2
for (_, c) in count.items():
	result -= c * (c - 1) // 2
print(result)


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify, nlargest
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))
n = inp()
a = [tuple(map(int, sys.stdin.readline().split())) for _ in range(n)]
d = defaultdict(int)
z = set()
yp = set()
xp = set()
for i in range(n):
	for j in range(i + 1, n):
		(x1, y1) = a[i]
		(x2, y2) = a[j]
		Y = y2 - y1
		X = x2 - x1
		if X < 0:
			Y *= -1
			X *= -1
		if X == 0:
			xp.add(x1)
		elif Y == 0:
			yp.add(y1)
		else:
			g = math.gcd(X, abs(Y))
			X //= g
			Y //= g
			c = x1 * Y - y1 * X
			if (X, Y, c) in z:
				continue
			d[X, Y] += 1
			z.add((X, Y, c))
m = len(z) + len(yp) + len(xp)
res = 0
for (x, y, c) in list(z):
	res += m - d[x, y]
for YY in list(yp):
	res += m - len(yp)
for XX in list(xp):
	res += m - len(xp)
print(res // 2)


from sys import stdin, stdout
import math
from itertools import accumulate

def getabc(x1, y1, x2, y2):
	t1 = x2 - x1
	t2 = y2 - y1
	t3 = y1 * x2 - y2 * x1
	if t1 < 0:
		t1 = t1 * -1
		t2 = t2 * -1
		t3 = t3 * -1
	t4 = math.gcd(math.gcd(t1, t2), t3)
	return (t1 // t4, t2 // t4, t3 // t4)
n = int(stdin.readline())
x = []
y = []
for i in range(n):
	(xi, yi) = stdin.readline().strip().split(' ')
	x.append(int(xi))
	y.append(int(yi))
d = {}
there = {}
for i in range(len(x)):
	for j in range(i + 1, len(x)):
		num = y[i] - y[j]
		den = x[i] - x[j]
		if num == 0:
			key = 'yintercept' + str(y[i])
			if key not in d:
				d[key] = 1
		elif den == 0:
			key = 'xintercept' + str(x[i])
			if key not in d:
				d[key] = 1
		else:
			(a, b, c) = getabc(x[i], y[i], x[j], y[j])
			key = str(a) + ' ' + str(b) + ' ' + str(c)
			keys = str(a) + ' ' + str(b)
			if key not in there:
				if keys in d:
					d[keys] += 1
				else:
					d[keys] = 1
				there[key] = 1
ansarr = [0, 0]
for i in d:
	if i[0] == 'x':
		ansarr[0] += 1
	elif i[0] == 'y':
		ansarr[1] += 1
	else:
		ansarr.append(d[i])
ans = 0
tarr = sum(ansarr)
for i in range(len(ansarr)):
	ans += (tarr - ansarr[i]) * ansarr[i]
stdout.write(str(ans // 2) + '\n')


def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def frac(a, b):
	x = gcd(a, b)
	return (a // x, b // x)
n = int(input())
coords = []
for i in range(n):
	coords.append(tuple(map(int, input().split())))
slopes = {}
for i in range(n):
	for j in range(i + 1, n):
		(x1, y1) = (coords[i][0], coords[i][1])
		(x2, y2) = (coords[j][0], coords[j][1])
		if x1 == x2:
			slope = 'inf'
			mark = x1
		else:
			slope = frac(y2 - y1, x2 - x1)
			mark = frac(y1 * x2 - y2 * x1, x2 - x1)
		if slope in slopes:
			slopes[slope].add(mark)
		else:
			slopes[slope] = {mark}
tot = sum((len(slopes[guy]) for guy in slopes))
sumi = 0
for guy in slopes:
	a = len(slopes[guy])
	sumi += a * (tot - a)
print(sumi // 2)


from fractions import Fraction
VERT = object()
n = int(input())
points = [tuple(map(int, input().split())) for _ in range(n)]
d = {}
for (i, p) in enumerate(points):
	(x1, y1) = p
	for (x2, y2) in points[i + 1:]:
		if x1 < x2:
			f = Fraction(y2 - y1, x2 - x1)
		elif x1 > x2:
			f = Fraction(y1 - y2, x1 - x2)
		else:
			f = VERT
		if f == VERT:
			x_inter = x1
		else:
			x_inter = y1 - x1 * f
		if f == VERT:
			key = VERT
			value = x_inter
		else:
			key = f.numerator / f.denominator
			value = x_inter.numerator / x_inter.denominator
		if key in d:
			d[key].add(x_inter)
		else:
			d[key] = {x_inter}
total = sum((len(s) for s in d.values()))
total_inter = 0
for s in d.values():
	total_inter += len(s) * (total - len(s))
print(total_inter // 2)


import sys
import math
from pprint import pprint
n = int(input())
p = [list(map(int, input().split())) for i in range(n)]
p.sort()
s = {}
for i in range(n):
	(x1, y1) = p[i]
	for j in range(i + 1, n):
		(x2, y2) = p[j]
		slope = 0
		cons = 0
		if x1 - x2 == 0:
			slope = sys.maxsize
			cons = x1
		else:
			slope = (y2 - y1) / (x2 - x1)
			cons = (y1 * x2 - x1 * y2) / (x2 - x1)
		if slope in s:
			if cons in s[slope]:
				s[slope][cons] += 1
			else:
				s[slope][cons] = 1
		else:
			s[slope] = {cons: 1}
ans = 0
cnt = 0
for x in s:
	ans += cnt * len(s[x])
	cnt += len(s[x])
print(ans)


def pos_gcd(a, b):
	if a == 0 and b == 0:
		return 1
	if b == 0:
		return a
	return pos_gcd(b, a % b)

def gcd2(a, b):
	return pos_gcd(abs(a), abs(b))

def gcd(a, b, c):
	return gcd2(gcd2(a, b), c)

def make_line(p1, p2):
	a = p2[1] - p1[1]
	b = p1[0] - p2[0]
	c = -a * p1[0] - b * p1[1]
	d = gcd(a, b, c)
	a //= d
	b //= d
	c //= d
	if a < 0:
		a = -a
		b = -b
		c = -c
	elif a == 0 and b < 0:
		b = -b
		c = -c
	return (a, b, c)

def parallel(l1, l2):
	return l1[0] == l2[0] and l1[1] == l2[1]
n = int(input())
pointlist = []
lineset = set()
for i in range(n):
	p = tuple(map(int, input().split(' ')))
	for q in pointlist:
		lineset.add(make_line(p, q))
	pointlist.append(p)
linedict = {}
for l in lineset:
	l1 = (l[0], l[1])
	if l1 not in linedict:
		linedict[l1] = 1
	else:
		linedict[l1] += 1
linecount = 0
ans = 0
for l in linedict.keys():
	linecount += linedict[l]
for l in linedict.keys():
	linecount -= linedict[l]
	ans += linedict[l] * linecount
print(ans)