Problem:
Score : 300 points

Problem Statement

The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.

He has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i.

To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.

How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9(1\leq i\leq N)

1 \leq B_i \leq 10^9(1\leq i\leq N)

1 \leq C_i \leq 10^9(1\leq i\leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N
B_1 ... B_N
C_1 ... C_N

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

Upper: 1-st part, Middle: 1-st part, Lower: 1-st part

Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part

Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

87

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
public class CodeForces
{
#if TEST
// To set this go to Project -> Properties -> Build -> General -> Conditional compilation symbols: -> enter 'TEST' into text box.
const bool testing = true;
#else
    const bool testing = false;
#endif


    struct Node : IComparable
    {
        public long T { get; set; }
        public long Q { get; set; }
        public int CompareTo(object obj)
        {
            return this.T.CompareTo(((Node)obj).T);
        }
    }
    public static long nPr(int n, int r)
    {
        // naive: return Factorial(n) / Factorial(n - r);
        return FactorialDivision(n, n - r);
    }
    private static long FactorialDivision(int topFactorial, int divisorFactorial)
    {
        long result = 1;
        for (int i = topFactorial; i > divisorFactorial; i--)
            result *= i;
        return result;
    }
    private static long Factorial(int i)
    {
        if (i <= 1)
            return 1;
        return i * Factorial(i - 1);
    }
    public static long nCr(int n, int r)
    {
        // naive: return Factorial(n) / Factorial(r) * Factorial(n - r);
        return nPr(n, r) / Factorial(r);
    }

    private static int LowerBound<T>(IList<T> list, T value)
    {
        if (list == null)
            throw new ArgumentNullException("list");
        var comp = Comparer<T>.Default;
        int lo = 0, hi = list.Count - 1;
        while (lo < hi)
        {
            int m = (hi + lo) / 2;  // this might overflow; be careful.
            if (comp.Compare(list[m], value) < 0) lo = m + 1;
            else hi = m - 1;
        }
        if (comp.Compare(list[lo], value) < 0) lo++;
        return Math.Min(lo, list.Count);
    }
    public class SegmentTree<T>
    {
        private int Mid;
        private T[] t;
        private Func<T, T, T> f;
        private T defaultValue = default(T);

        public SegmentTree(T[] a, T defaultValue, Func<T, T, T> f)
        {
            this.defaultValue = defaultValue;
            this.Mid = 1;
            while (Mid < a.Length) Mid *= 2;
            this.t = new T[Mid + Mid];
            this.f = f;

            Array.Copy(a, 0, this.t, Mid, a.Length);
            for (int i = Mid + a.Length; i < this.t.Length; i++) this.t[i] = this.defaultValue;
            for (int i = Mid - 1; i > 0; i--) this.UpdateValueFor(i);
        }
        public SegmentTree(T defaultValue, int capacity, Func<T, T, T> f)
        {
            this.defaultValue = defaultValue;
            this.Mid = 1;
            while (Mid < capacity) Mid *= 2;
            this.t = new T[Mid + Mid];
            this.f = f;

            for (int i = 0; i < Mid; i++)
                this.t[Mid + i] = defaultValue;

            for (int i = (Mid + Mid - 1) / 2; i > 0; i--) this.UpdateValueFor(i);
        }

        public T GetRoot()
        {
            return t[1];
        }

        public T Get(int l, int r)
        {
            l += Mid;
            r += Mid;

            T ans = this.defaultValue;
            while (l <= r)
            {
                if ((l & 1) == 1) ans = this.f(ans, t[l]);
                if ((r & 1) == 0) ans = this.f(ans, t[r]);

                l = (l + 1) >> 1;
                r = (r - 1) >> 1;
            }

            return ans;
        }

        public void Update(int i, T value)
        {
            t[i + Mid] = value;
            for (i = (i + Mid) >> 1; i >= 1; i = (i >> 1)) this.UpdateValueFor(i);
        }

        private void UpdateValueFor(int i)
        {
            t[i] = this.f(t[i + i], t[i + i + 1]);
        }
    }
    static void program(TextReader input)
    {
        var n = int.Parse(input.ReadLine());
        var all = new List<long>();
        var a = input.ReadLine().TrimEnd().Split(' ').Select(long.Parse).ToList();
        var b = input.ReadLine().TrimEnd().Split(' ').Select(long.Parse).ToList();
        var c = input.ReadLine().TrimEnd().Split(' ').Select(long.Parse).ToList();
        a.Sort();
        b.Sort();
        c.Sort();
        var cNumber = new int[n];
        var aNumber = new int[n];
        var ci = 0;
        for(var i = 0; i < n; i++)
        {
            var bn = b[i];
            while(ci < n && c[ci] <= bn)
            {
                ci++;
            }
            cNumber[i] = n - ci;
        }

        var ai = 0;
        for (var i = 0; i < n; i++)
        {
            var bn = b[i];
            while (ai < n && a[ai] < bn)
            {
                ai++;
            }
            aNumber[i] = ai;
        }

        long result = 0;
        for (var i = 0; i < n; i++)
        {
            result += aNumber[i] * cNumber[i];
        }

            Console.WriteLine(result);
    }

    public static void Main(string[] args)
    {
        if (!testing)
        { // set testing to false when submiting to codeforces
            program(Console.In); // write your program in 'program' function (its your new main !)
            return;
        }

        Console.WriteLine("Test Case(1) => expected :");
        Console.WriteLine("6");
        Console.WriteLine("Test Case(1) => found    :");
        program(new StringReader("5 1 1 1 4 0 5\n1 1\n2 1\n3 1\n4 0\n5 0\n"));
        Console.WriteLine();

        Console.WriteLine("Test Case(2) => expected :");
        Console.WriteLine("-1");
        Console.WriteLine("Test Case(2) => found    :");
        program(new StringReader("1 1 2 1 2 1 2\n1 0\n"));
        Console.WriteLine();

    }
}