Buggy python submission (verdict: Runtime Error):
# coding:utf-8

n = input()
top = map(int, raw_input().split())
middle = map(int, raw_input().split())
under = map(int, raw_input().split())
top.sort()
middle.sort()
under.sort()
total = 0
for i in xrange(n):
    high = n - 1
    low = 0
    t = (high+low)/2
    d = middle[i] - 1
    while(low <= high):
        if top[t] == d: break
        if top[t] > d:
            high = t - 1
        else:
            low = t + 1
        t = (high+low)/2
    while(t+1 < len(top)):
        if top[t] != top[t+1]: break
        t += 1
    tt = t + 1 #topの候補数

    high = n -1
    low = 0
    t = (high+low)/2
    d = middle[i] + 1
    while(low <= high):
        if under[t] == d: break
        if under[t] > d:
            high = t - 1
        else:
            low = t + 1
        t = (high+low)/2
    if(under[t] != d): t += 1
    while(t-1 >= 0):
        if under[t] != under[t-1]: break
        t -= 1
    tu = n - t #underの候補数

    total += tt*tu
print total



Fixed submission:
# coding:utf-8

INF = float('inf')


def inpl(): return list(map(int, input().split()))


N = int(input())
A = sorted(inpl())
B = sorted(inpl())
C = sorted(inpl())

ans = 0
i_a = 0
i_c = 0
for i_b in range(N):
    while i_a < N and A[i_a] < B[i_b]:
        i_a += 1
    while i_c < N and C[i_c] <= B[i_b]:
        i_c += 1

    ans += i_a * (N - i_c)

print(ans)

Buggy python submission (verdict: Runtime Error):
# coding:utf-8

n = input()
top = map(int, raw_input().split())
middle = map(int, raw_input().split())
under = map(int, raw_input().split())
top.sort()
middle.sort()
under.sort()
total = 0
for i in xrange(n):
    high = n - 1
    low = 0
    t = (high+low)/2
    d = middle[i] - 1
    while(low <= high):
        if top[t] == d: break
        if top[t] > d:
            high = t - 1
        else:
            low = t + 1
        t = (high+low)/2
    while(t+1 < len(top)):
        if top[t] != top[t+1]: break
        t += 1
    tt = t + 1 #topの候補数

    high = n -1
    low = 0
    t = (high+low)/2
    d = middle[i] + 1
    while(low <= high):
        if under[t] == d: break
        if under[t] > d:
            high = t - 1
        else:
            low = t + 1
        t = (high+low)/2
    if(under[t] != d): t += 1
    while(t-1 >= 0):
        if under[t] != under[t+1]: break
        t -= 1
    tu = n - t #underの候補数

    total += tt*tu
print total


Fixed submission:
# coding:utf-8

INF = float('inf')


def inpl(): return list(map(int, input().split()))


N = int(input())
A = sorted(inpl())
B = sorted(inpl())
C = sorted(inpl())

ans = 0
i_a = 0
i_c = 0
for i_b in range(N):
    while i_a < N and A[i_a] < B[i_b]:
        i_a += 1
    while i_c < N and C[i_c] <= B[i_b]:
        i_c += 1

    ans += i_a * (N - i_c)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = input()
top = map(int, raw_input().split())
top.sort()
middle = map(int, raw_input().split())
middle.sort()
under = map(int, raw_input().split())
under.sort()
cnt = 0
for i in top[::-1]:
    for j in middle[::-1]:
        if j <= i: break
        for k in under[::-1]:
            if k <= j: break
            cnt += 1
            # print '{} {} {}'.format(i,j,k)

print cnt

Fixed submission:
# coding:utf-8

INF = float('inf')


def inpl(): return list(map(int, input().split()))


N = int(input())
A = sorted(inpl())
B = sorted(inpl())
C = sorted(inpl())

ans = 0
i_a = 0
i_c = 0
for i_b in range(N):
    while i_a < N and A[i_a] < B[i_b]:
        i_a += 1
    while i_c < N and C[i_c] <= B[i_b]:
        i_c += 1

    ans += i_a * (N - i_c)

print(ans)