You are given an array a consisting of n integers. We denote the subarray a[l..r] as the array [a_l, a_{l + 1}, ..., a_r] (1 ≤ l ≤ r ≤ n).

A subarray is considered good if every integer that occurs in this subarray occurs there exactly thrice. For example, the array [1, 2, 2, 2, 1, 1, 2, 2, 2] has three good subarrays:

  * a[1..6] = [1, 2, 2, 2, 1, 1]; 
  * a[2..4] = [2, 2, 2]; 
  * a[7..9] = [2, 2, 2]. 



Calculate the number of good subarrays of the given array a.

Input

The first line contains one integer n (1 ≤ n ≤ 5 ⋅ 10^5).

The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n).

Output

Print one integer — the number of good subarrays of the array a.

Examples

Input


9
1 2 2 2 1 1 2 2 2


Output


3


Input


10
1 2 3 4 1 2 3 1 2 3


Output


0


Input


12
1 2 3 4 3 4 2 1 3 4 2 1


Output


1
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.io.UncheckedIOException;
import java.util.List;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * @author khokharnikunj8
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "khokharnikunj8", 1 << 29);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            GThreeOccurrences solver = new GThreeOccurrences();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class GThreeOccurrences {
        int[][] tree;
        int[] lazy;

        public void merge(int[] x, int[] y, int[] z) {
            if (x[0] == y[0]) {
                z[0] = x[0];
                z[1] = x[1] + y[1];
            } else if (x[0] < y[0]) {
                z[0] = x[0];
                z[1] = x[1];
            } else {
                z[0] = y[0];
                z[1] = y[1];
            }
        }

        public void build(int[] ar, int node, int start, int end) {
            if (start == end) {
                tree[node] = new int[]{0, 1};
            } else {
                int mid = (start + end) >> 1;
                build(ar, node << 1, start, mid);
                build(ar, node << 1 | 1, mid + 1, end);
                merge(tree[node << 1], tree[node << 1 | 1], tree[node]);
            }
        }

        public void push(int node, int start, int end) {
            if (lazy[node] != 0) {
                if (start != end) {
                    lazy[node << 1] += lazy[node];
                    lazy[node << 1 | 1] += lazy[node];
                }
                tree[node][0] += lazy[node];
                lazy[node] = 0;
            }
        }

        public void update(int node, int start, int end, int l, int r, int val) {
            push(node, start, end);
            if (l > end || r < start) return;
            if (l <= start && end <= r) {
                lazy[node] += val;
                push(node, start, end);
                return;
            }
            int mid = (start + end) >> 1;
            update(node << 1, start, mid, l, r, val);
            update(node << 1 | 1, mid + 1, end, l, r, val);
            merge(tree[node << 1], tree[node << 1 | 1], tree[node]);
        }

        public int[] query(int node, int start, int end, int l, int r) {
            push(node, start, end);
            if (l > end || r < start) return new int[]{0, 0};
            if (l <= start && end <= r) {
                return tree[node];
            }
            int mid = (start + end) >> 1;
            int[] left = query(node << 1, start, mid, l, r);
            int[] right = query(node << 1 | 1, mid + 1, end, l, r);
            int[] result = new int[2];
            merge(left, right, result);
            return result;
        }

        public void solve(int testNumber, FastInput in, FastOutput out) {
            int n = in.readInt();
            int[] ar = new int[n];
            for (int i = 0; i < n; i++) ar[i] = in.readInt();
            tree = new int[(n + 5) * 4][2];
            lazy = new int[(n + 5) * 4];
            build(ar, 1, 0, n - 1);
            List<Integer>[] lastIndex = new ArrayList[n + 1];
            for (int i = 0; i <= n; i++) lastIndex[i] = new ArrayList<>();
            long ans = 0;
            for (int i = 0; i < n; i++) {
                int val = ar[i];
                lastIndex[val].add(i);
                int size = lastIndex[val].size();
                if (size == 1) {
                    update(1, 0, n - 1, 0, i, 1);
                } else if (size == 2) {
                    update(1, 0, n - 1, lastIndex[val].get(size - 2) + 1, i, 1);
                } else if (size == 3) {
                    update(1, 0, n - 1, lastIndex[val].get(size - 2) + 1, i, 1);
                    update(1, 0, n - 1, 0, lastIndex[val].get(size - 3), -1);
                } else if (size == 4) {
                    update(1, 0, n - 1, lastIndex[val].get(size - 2) + 1, i, 1);
                    update(1, 0, n - 1, 0, lastIndex[val].get(size - 4), 1);
                    update(1, 0, n - 1, lastIndex[val].get(size - 4) + 1, lastIndex[val].get(size - 3), -1);
                } else {
                    update(1, 0, n - 1, lastIndex[val].get(size - 2) + 1, i, 1);
                    update(1, 0, n - 1, lastIndex[val].get(size - 5) + 1, lastIndex[val].get(size - 4), 1);
                    update(1, 0, n - 1, lastIndex[val].get(size - 4) + 1, lastIndex[val].get(size - 3), -1);
                }
                int[] res = query(1, 0, n - 1, 0, i);
                if (res[0] == 0) ans += res[1];
            }
            out.println(ans);


        }

    }

    static class FastOutput implements AutoCloseable, Closeable, Appendable {
        private final Writer os;
        private final StringBuilder cache = new StringBuilder(5 << 20);

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput append(CharSequence csq) {
            cache.append(csq);
            return this;
        }

        public FastOutput append(CharSequence csq, int start, int end) {
            cache.append(csq, start, end);
            return this;
        }

        public FastOutput append(char c) {
            cache.append(c);
            return this;
        }

        public FastOutput append(long c) {
            cache.append(c);
            return this;
        }

        public FastOutput println(long c) {
            return append(c).println();
        }

        public FastOutput println() {
            cache.append(System.lineSeparator());
            return this;
        }

        public FastOutput flush() {
            try {
                os.append(cache);
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }

    static class FastInput {
        private final InputStream is;
        private final byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

    }
}

