Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.

There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).

Help Pashmak with the test.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print a single integer — the answer to the problem.

Examples

Input

7
1 2 1 1 2 2 1


Output

8


Input

3
1 1 1


Output

1


Input

5
1 2 3 4 5


Output

0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Main {
  static class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    static void init(InputStream input) {
      reader = new BufferedReader(new InputStreamReader(input));
      tokenizer = new StringTokenizer("");
    }

    static String next() throws IOException {
      while (!tokenizer.hasMoreTokens()) {
        tokenizer = new StringTokenizer(reader.readLine());
      }
      return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
      return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
      return Long.parseLong(next());
    }

    static double nextDouble() throws IOException {
      return Double.parseDouble(next());
    }

    static String nextLine() {
      String str = "";
      try {
        str = reader.readLine();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return str;
    }
  }
  
  public static void main(String[] args) throws Exception {
    Reader.init(System.in);
    int size = Reader.nextInt();
    int[] a = new int[size + 1];
    for (int i = 1; i <= size; i++) {
      a[i] = Reader.nextInt();
    }
    int[] left = cmpLeft(a);
    int[] right = cmpRight(a);
    long numOfPairs = cmpInversion(left, right);
    System.out.println(numOfPairs);
  }

  public static int[] cmpLeft(int[] a) {
    Map<Integer, Integer> cnt = new HashMap<>();
    int[] left = new int[a.length];
    for (int i = 1; i < a.length; i++) {
      int num = a[i];
      if (cnt.containsKey(num)) {
        cnt.put(num, cnt.get(num) + 1);
      } else {
        cnt.put(num, 1);
      }
      left[i] = cnt.get(num);
    }
    return left;
  }

  public static int[] cmpRight(int[] a) {
    Map<Integer, Integer> cnt = new HashMap<>();
    int[] right = new int[a.length];
    for (int i = a.length - 1; i >= 1; i--) {
      int num = a[i];
      if (cnt.containsKey(num)) {
        cnt.put(num, cnt.get(num) + 1);
      } else {
        cnt.put(num, 1);
      }
      right[i] = cnt.get(num);
    }
    return right;
  }

  public static long cmpInversion(int[] left, int[] right) {
    long inversionCnt = 0;
    Fenwick fenwick = new Fenwick(left.length - 1);
    for (int i = left.length - 2; i >= 0; i--) {
      fenwick.update(right[i + 1], 1);
      inversionCnt += fenwick.query(left[i] - 1);
    }
    return inversionCnt;
  }

  // 1 based, always inclusive
  public static class Fenwick {
    int[] intervals;
    
    public Fenwick(int size) {
      intervals = new int[size + 1];
    }

    public void update(int i, int val) {
      while (i < intervals.length) {
        intervals[i] += val;
        i += i & (-i);
      }
    }

    public int query(int i) {
      int sum = 0;
      while (i >= 1) {
        sum += intervals[i];
        i -= i & (-i);
      }
      return sum;
    }
  }
}
