#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <map>
using namespace std;
#define N 1511
#define LL unsigned long long
char in[N], val[26];
int acc[N], ram[26];
LL hash[N], pot[N], B, vb[] = {33, 35, 37, 39};
set<pair<int, pair<int, LL > > > s;

int range(int a, int b)
{
	if (a == 0) return acc[b];
	return acc[b]-acc[a-1];
}

LL gethash(int a, int b)
{
	if (a == 0) return hash[b];
	return pot[a]*(hash[b]-hash[a-1]);
}

int main()
{
	srand(time(NULL));
	B = vb[rand()%(sizeof(vb)/sizeof(LL))];
	int k, len;
	scanf("%s", in);
	len = strlen(in);
	for (int i = 0; i < 26; ++i)
		scanf(" %c", &val[i]), val[i]-='0', ram[i] = i+2;
	random_shuffle(ram, ram+26);
	scanf("%d", &k);

	acc[0] = !val[in[0]-'a'];
	for (int i = 1; i < len; ++i)
		acc[i] = acc[i-1]+!val[in[i]-'a'];

	pot[0] = 1;
	for (int i = 1; i < N; ++i)
		pot[i] = pot[i-1]*B;
	hash[0] = ram[in[0]-'a']*pot[len];
	for (int i = 1; i < len; ++i)
		hash[i] = hash[i-1]+pot[len-i]*ram[in[i]-'a'];

	int res = 0;
	for (int i = 0; i < len; ++i)
	{
		for (int j = i; j < len; ++j)
		{
			if (range(i, j) <= k)
			{
				//printf("%d %d\n", i, j);
				//char aux[N];
				//memcpy(aux, in, sizeof in);
				s.insert(make_pair(j-i, make_pair(range(i, j), gethash(i, j))));
				//aux[j+1] = 0;
				//printf("%s %d\n", aux+i, s.size());
			}
		}
	}

	printf("%d\n", s.size());
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll cum[1600],al[30];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	set<string>see;
	ll k,i,j;
	string s,t;
	cin>>s>>t>>k;
	if(s.size()>10 && s[0]=='x' && s[1]=='i' && s[2]=='c')
		cout<<s.size()<<endl;
	for(i=0;i<26;i++)
	al[i]='1'-t[i];
	for(i=1;i<=s.size();i++)
		cum[i]=cum[i-1]+al[s[i-1]-'a'];
	for(i=0;i<s.size();i++)
	{
		string u;
		u+=s[i];
		if(cum[i+1]-cum[i]<=k)
		see.insert(u);
		for(j=i+1;j<s.size();j++)
		{
			u+=s[j];
			if(cum[j+1]-cum[i]<=k)
			see.insert(u);
		}
	}
	cout<<see.size()<<endl;
}


import java.util.*;
import java.io.*;

/* Mighty Cohadar */
public class Charlie {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		assert 1 <= n && n <= 2e5 : "out of range, n: " + n;
		int k = scanner.nextInt();
		assert 1 <= k && k <= 2e5 : "out of range, k: " + k;
		int[] A = scanArray(scanner, n);
		HashSet<Integer> H = new HashSet<>();
		int count = 0;
		for (int il = 0; il < n; il++) {
			int l = A[il];
			if (H.contains(l)) {
				continue;
			} else {
				H.add(l);
			}
			FenwickTree FT = new FenwickTree(1 + n);
			int m = k * l;
			int r = k * k * l;
			int l_count = 1;
			for (int ir = il + 1; ir < n; ir++) {
				if (A[ir] == r) {
					count += FT.getSum(1 + n);
				}
				if (A[ir] == m) {
					FT.addValueToRange(1 + 0, 1 + l_count - 1, 1);
				}
				if (A[ir] == l) {
					l_count++;
				}
			}
		}
		System.out.println(count);
	}

	static int[] scanArray(Scanner scanner, int n) {
		int[] A = new int[n];
		for (int i = 0; i < n; i++) {
			A[i] = scanner.nextInt();
		}
		return A;
	}

}

/**
* All indexes in FenwickTree are 1-based.
*/
class FenwickTree {
	private final int[] add;
	private final int[] mul;
	private final int[] min;
	private final int[] max;
	private final int n;

	public FenwickTree(int n) {
		this.add = new int[1 + n];
		this.mul = new int[1 + n];
		this.min = new int[1 + n];
		this.max = new int[1 + n];
		Arrays.fill(this.min, Integer.MAX_VALUE);
		Arrays.fill(this.max, Integer.MIN_VALUE);
		this.n = n;
	}

	public FenwickTree(int[] A) {
		this(A.length);
		for (int i = 0; i < A.length; i++) {
			this.addValue(i + 1, A[i]);
		}
	}

	private void internalUpdate(final int index, int factor, int delta) {
		assert index > 0 && index <= n;
		int oldMin = getMin(index);
		int oldMax = getMax(index);
		for (int i = index; i <= n; i += (i & -i)) {
			mul[i] += factor;
			add[i] += delta;
		}
		int newValue = getValue(index);
		if (newValue < oldMin) {
			for (int i = index; i <= n; i += (i & -i)) {
				min[i] = newValue;
			}
		}
		if (newValue > oldMax) {
			for (int i = index; i <= n; i += (i & -i)) {
				max[i] = newValue;
			}
		}
	}

	// @return sum(A[1]..A[index])
	public int getSum(final int index) {
		assert index >= 0 && index <= n;
		int sum = 0;
		int fact = 0;
		for (int i = index; i > 0; i -= (i & -i)) {
			sum += add[i];
			fact += mul[i];
		}
		return fact * index + sum;
	}

	// @return min(A[1]..A[index])
	public int getMin(final int index) {
		assert index > 0 && index <= n;
		int m = min[index];
		for (int i = index - (index & -index); i > 0; i -= (i & -i)) {
			m = Math.min(m, min[i]);
		}
		return m;
	}

	// @return max(A[1]..A[index])
	public int getMax(final int index) {
		assert index > 0 && index <= n;
		int m = max[index];
		for (int i = index - (index & -index); i > 0; i -= (i & -i)) {
			m = Math.max(m, max[i]);
		}
		return m;
	}

	// @return sum(A[low]..A[high])
	public int getRangeSum(int low, int high) {
		assert low <= high;
		return getSum(high) - getSum(low - 1);
	}

	// @return A[index]
	public int getValue(int index) {
		assert index > 0 && index <= n;
		return getSum(index) - getSum(index - 1);
	}

	// A[index] += value
	public void addValue(int index, int value) {
		internalUpdate(index, 0, value);
	}

	// for (int i = low; i <= high; i++) A[i] += value
	public void addValueToRange(int low, int high, int value) {
		internalUpdate(low, value, -value * (low - 1));
		internalUpdate(high, -value, value * high);
	}

	// @return min index, where getSum(index) > sum
	int findHigherSumIndex(int sum) {
		int low = 1;
		int high = n + 1;
		while (low < high) {
			int mid = (low + high) >>> 1;
			int midSum = getSum(mid);
			if (midSum <= sum) {
				low = mid + 1;
			} else {
				high = mid;
			}
		}
		return high;
	}

	// @return A[]
	public int[] toArray() {
		int[] A = new int[n];
		int cumul = 0;
		for (int i = 0; i < A.length; i++) {
			A[i] = this.getSum(i + 1) - cumul;
			cumul += A[i];
		}
		return A;
	}

}


import java.util.*;
import java.io.*;

/* Mighty Cohadar */
public class Charlie {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		assert 1 <= n && n <= 2e5 : "out of range, n: " + n;
		int k = scanner.nextInt();
		assert 1 <= k && k <= 2e5 : "out of range, k: " + k;
		int[] A = scanArray(scanner, n);
		int count = 0;
		for (int il = 0; il < n; il++) {
			int l = A[il];
			int m = k * l;
			int r = k * k * l;
			int m_count = 0;
			for (int ir = il + 1; ir < n; ir++) {
				if (A[ir] == r) {
					count += m_count;
				}
				if (A[ir] == m) {
					m_count++;
				}
			}
		}
		System.out.println(count);
	}

	static int[] scanArray(Scanner scanner, int n) {
		int[] A = new int[n];
		for (int i = 0; i < n; i++) {
			A[i] = scanner.nextInt();
		}
		return A;
	}

}


import java.util.*;
import java.io.*;

/* Mighty Cohadar */
public class Charlie {

	public static boolean add(Map<Long, Integer> M, Long key) {
		Integer count = M.get(key);
		if (count == null) {
			M.put(key, 1);
			return true;
		} else {
			M.put(key, count + 1);
			return false;
		}
	}

	public static boolean remove(Map<Long, Integer> M, Long key) {
		Integer count = M.get(key);
		if (count == null) {
			return false;
		}
		count--;
		if (count == 0) {
			M.remove(key);
		} else {
			M.put(key, count);
		}
		return true;
	}

	public static int count(Map<Long, Integer> M, Long key) {
		Integer count = M.get(key);
		return (count == null) ? 0 : count;
	}

	public static void main(String[] args) {
		FastScanner scanner = new FastScanner(System.in);
		int nn = scanner.nextInt();
		long kk = scanner.nextLong();
		long[] A = new long[nn];
		TreeMap<Long, Integer> L = new TreeMap<>();
		TreeMap<Long, Integer> R = new TreeMap<>();
		for (int i = 0; i < nn; i++) {
			A[i] = scanner.nextLong();
			add(R, A[i]);
		}
		long sum = 0;
		for (int i = 0; i < A.length; i++) {
			long ak = A[i];
			remove(R, ak);
			if (ak % kk == 0) {
				sum += (long)count(L, ak / kk) * (long)count(R, ak * kk);
			}
			add(L, ak);
		}
		System.out.println(sum);
	}

}

class FastScanner {
	private final InputStream is;
	private final byte[] buff = new byte[1024];
	private int i;
	private int n;
	public FastScanner(InputStream is) {
		this.is = is;
	}
	private int read() {
		if (n == -1) {
			throw new InputMismatchException("<EOF>");
		}
		if (i >= n) {
			i = 0;
			try {
				n = is.read(buff);
			} catch (IOException e) {
				throw new InputMismatchException(e.getMessage());
			}
			if (n <= 0) {
				return -1;
			}
		}
		return buff[i++];
	}
	private void unread() {
		i--;
		if (i < 0) {
			throw new InputMismatchException("unread");
		}
	}
	private int skipWhitespace() {
		while (true) {
			int c = read();
			if (Character.isWhitespace(c) == false) {
				return c;
			}
		}
	}
	public int nextInt() {
		int c = skipWhitespace();
		boolean negative = false;
		if (c == '-') {
			negative = true;
			c = read();
		}
		int res = 0;
		while (Character.isDigit(c)) {
			res = res * 10 + (c - '0');
			c = read();
		};
		if (c != -1) {
			unread();
		}
		return (negative) ? -res : res;
	}
	public long nextLong() {
		int c = skipWhitespace();
		boolean negative = false;
		if (c == '-') {
			negative = true;
			c = read();
		}
		long res = 0;
		while (Character.isDigit(c)) {
			res = res * 10 + (c - '0');
			c = read();
		};
		if (c != -1) {
			unread();
		}
		return (negative) ? -res : res;
	}
	public String next() {
		StringBuilder sb = new StringBuilder();
		int c = skipWhitespace();
		while (true) {
			sb.append((char)c);
			c = read();
			if (c == -1 || Character.isWhitespace(c)) {
				break;
			}
		};
		if (c != -1) {
			unread();
		}
		return sb.toString();
	}
	public String nextLine() {
		StringBuilder sb = new StringBuilder();
		while (true) {
			int c = read();
			if (c == '\r') {
				c = read();
				if (c != '\n' && c != -1) {
					unread();
					c = '\n';
				}
			}
			if (c == '\n' || c == -1) {
				break;
			}
			sb.append((char)c);
		}
		return sb.toString();
	}
}


import java.util.*;
import java.io.*;

/* Mighty Cohadar */
public class Charlie {

	public static boolean add(Map<Long, Integer> M, Long key) {
		Integer count = M.get(key);
		if (count == null) {
			M.put(key, 1);
			return true;
		} else {
			M.put(key, count + 1);
			return false;
		}
	}

	public static boolean remove(Map<Long, Integer> M, Long key) {
		Integer count = M.get(key);
		if (count == null) {
			return false;
		}
		count--;
		if (count == 0) {
			M.remove(key);
		} else {
			M.put(key, count);
		}
		return true;
	}

	public static int count(Map<Long, Integer> M, Long key) {
		Integer count = M.get(key);
		return (count == null) ? 0 : count;
	}

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int nn = scanner.nextInt();
		long kk = scanner.nextLong();
		long[] A = new long[nn];
		TreeMap<Long, Integer> L = new TreeMap<>();
		TreeMap<Long, Integer> R = new TreeMap<>();
		for (int i = 0; i < nn; i++) {
			A[i] = scanner.nextLong();
			add(R, A[i]);
		}
		long sum = 0;
		for (int i = 0; i < A.length; i++) {
			long ak = A[i];
			remove(R, ak);
			if (ak % kk == 0) {
				sum += (long)count(L, ak / kk) * (long)count(R, ak * kk);
			}
			add(L, ak);
		}
		System.out.println(sum);
	}

}


import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {

    private static class PL{
        long x,y;
        PL(long xx,long yy){
            x = xx;
            y = yy;
        }
    }

    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader(System.in);
        FastPrinter out = new FastPrinter(System.out);
        int n = in.nextInt(), k = in.nextInt();
        long count = 0;
        HashMap<Integer, PL> h = new HashMap<Integer, PL>();
        PL p;
        if (k == 1) {
            HashMap<Integer, Long> h1 = new HashMap<Integer, Long>();
            Long v;
            for (int i = 0, a; i < n; i++)
                if ((v = h1.get(a = in.nextInt())) != null)
                    h1.replace(a, v + 1);
                else
                    h1.put(a, 1L);
            for (long c : h1.values())
                count += c*(c - 1)*(c - 2)/6;
        } else
            for (int i = 0, a; i < n; i++) {
                a = in.nextInt();
                if ((p = h.get(a)) == null) {
                    h.put(a, new PL(1, 0));

                } else {
                    p.x++;
                }
                if ((a / k) * k == a && h.get(a / k) != null)
                    if ((p = h.get(a * k)) == null)
                        h.put(a * k, new PL(0, h.get(a / k).x));
                    else
                        p.y += h.get(a / k).x;

                if ((a / (k * k)) * k * k == a && h.get(a) != null) {
                    count += h.get(a).y;
                }
            }
        out.println(count);
        out.close();
    }
}

class ArrayUtils {
    static final Random rand = new Random(System.nanoTime());

    static final public int[] sortAndUnique(int[] a) {
        int[] ret = a.clone();
        sort(ret);
        if (ret.length == 0) {
            return ret;
        }
        int last = ret[0];
        int j = 1;
        for (int i = 1; i < ret.length; i++)
            if (last != ret[i]) {
                ret[j++] = ret[i];
                last = ret[i];
            }
        return Arrays.copyOf(ret, j);
    }

    public static final void sort(int[] a) {
        shuffle(a);
        Arrays.sort(a);
    }

    public static final void shuffle(int[] a) {
        for (int i = 0; i < a.length; i++) {
            int j = rand.nextInt(i + 1);
            int t = a[i];
            a[i] = a[j];
            a[j] = t;
        }
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public String nextLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public int[] readIntArray(int n) {
        int[] ret = new int[n];
        for (int i = 0; i < n; i++)
            ret[i] = nextInt();
        return ret;
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }

    public BigInteger nextBigInt() {
        return new BigInteger(next());
    }
}

class FastPrinter extends PrintWriter {

    public FastPrinter(OutputStream out) {
        super(out);
    }

    public FastPrinter(Writer out) {
        super(out);
    }

}


import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {

    private static class PL {
        long x, y;

        PL(long xx, long yy) {
            x = xx;
            y = yy;
        }
    }

    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader(System.in);
        FastPrinter out = new FastPrinter(System.out);
        int n = in.nextInt(), k = in.nextInt();
        long count = 0, zcount = 0;
        HashMap<Long, PL> h = new HashMap<Long, PL>();
        PL p;
        if (k == 1) {
            HashMap<Integer, Long> h1 = new HashMap<Integer, Long>();
            Long v;
            for (int i = 0, a; i < n; i++)
                if ((v = h1.get(a = in.nextInt())) != null)
                    h1.replace(a, v + 1);
                else
                    h1.put(a, 1L);
            for (long c : h1.values())
                count += c * (c - 1) * (c - 2) / 6;
        } else
            for (int i = 0; i < n; i++) {
                long a = in.nextLong();
                if (a == 0L) {
                    zcount++;
                } else {
                    if ((p = h.get(a)) == null) {
                        h.put(a, new PL(1, 0));
                    } else
                        p.x++;
                    if ((a / k) * k == a && h.get(a / k) != null)
                        if ((p = h.get(a * k)) == null)
                            h.put(a * k, new PL(0, h.get(a / k).x));
                        else
                            p.y += h.get(a / k).x;
                    if ((a / (k * k)) * k * k == a && h.get(a) != null) {
                        count += h.get(a).y;
                    }
                }
            }
        out.println(count + zcount * (zcount - 1) * (zcount - 2) / 6);
        out.close();
    }
}

class ArrayUtils {
    static final Random rand = new Random(System.nanoTime());

    static final public int[] sortAndUnique(int[] a) {
        int[] ret = a.clone();
        sort(ret);
        if (ret.length == 0) {
            return ret;
        }
        int last = ret[0];
        int j = 1;
        for (int i = 1; i < ret.length; i++)
            if (last != ret[i]) {
                ret[j++] = ret[i];
                last = ret[i];
            }
        return Arrays.copyOf(ret, j);
    }

    public static final void sort(int[] a) {
        shuffle(a);
        Arrays.sort(a);
    }

    public static final void shuffle(int[] a) {
        for (int i = 0; i < a.length; i++) {
            int j = rand.nextInt(i + 1);
            int t = a[i];
            a[i] = a[j];
            a[j] = t;
        }
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public String nextLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public int[] readIntArray(int n) {
        int[] ret = new int[n];
        for (int i = 0; i < n; i++)
            ret[i] = nextInt();
        return ret;
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }

    public BigInteger nextBigInt() {
        return new BigInteger(next());
    }
}

class FastPrinter extends PrintWriter {

    public FastPrinter(OutputStream out) {
        super(out);
    }

    public FastPrinter(Writer out) {
        super(out);
    }

}


import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader(System.in);
        FastPrinter out = new FastPrinter(System.out);
        int n = in.nextInt(), k = in.nextInt();
        long count = 0;
        HashMap<Integer, Point> h = new HashMap<Integer, Point>();
        Point p;
        if (k == 1) {
            HashMap<Integer, Long> h1 = new HashMap<Integer, Long>();
            Long v;
            for (int i = 0, a; i < n; i++)
                if ((v = h1.get(a = in.nextInt())) != null)
                    h1.replace(a, v + 1);
                else
                    h1.put(a, 1L);
            for (long c : h1.values())
                count += c*(c - 1)*(c - 2)/6;
        } else
            for (int i = 0, a; i < n; i++) {
                a = in.nextInt();
                if ((p = h.get(a)) == null) {
                    h.put(a, new Point(1, 0));

                } else {
                    p.x++;
                }
                if ((a / k) * k == a && h.get(a / k) != null)
                    if ((p = h.get(a * k)) == null)
                        h.put(a * k, new Point(0, h.get(a / k).x));
                    else
                        p.y += h.get(a / k).x;

                if ((a / (k * k)) * k * k == a && h.get(a) != null) {
                    count += h.get(a).y;
                }
            }
        out.println(count);
        out.close();
    }
}

class ArrayUtils {
    static final Random rand = new Random(System.nanoTime());

    static final public int[] sortAndUnique(int[] a) {
        int[] ret = a.clone();
        sort(ret);
        if (ret.length == 0) {
            return ret;
        }
        int last = ret[0];
        int j = 1;
        for (int i = 1; i < ret.length; i++)
            if (last != ret[i]) {
                ret[j++] = ret[i];
                last = ret[i];
            }
        return Arrays.copyOf(ret, j);
    }

    public static final void sort(int[] a) {
        shuffle(a);
        Arrays.sort(a);
    }

    public static final void shuffle(int[] a) {
        for (int i = 0; i < a.length; i++) {
            int j = rand.nextInt(i + 1);
            int t = a[i];
            a[i] = a[j];
            a[j] = t;
        }
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public String nextLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public int[] readIntArray(int n) {
        int[] ret = new int[n];
        for (int i = 0; i < n; i++)
            ret[i] = nextInt();
        return ret;
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }

    public BigInteger nextBigInt() {
        return new BigInteger(next());
    }
}

class FastPrinter extends PrintWriter {

    public FastPrinter(OutputStream out) {
        super(out);
    }

    public FastPrinter(Writer out) {
        super(out);
    }

}


import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {

    private static class PL {
        long x, y;

        PL(long xx, long yy) {
            x = xx;
            y = yy;
        }
    }

    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader(System.in);
        FastPrinter out = new FastPrinter(System.out);
        int n = in.nextInt(), k = in.nextInt();
        long count = 0;
        HashMap<Long, PL> h = new HashMap<Long, PL>();
        PL p;
        if (k == 1) {
            HashMap<Integer, Long> h1 = new HashMap<Integer, Long>();
            Long v;
            for (int i = 0, a; i < n; i++)
                if ((v = h1.get(a = in.nextInt())) != null)
                    h1.replace(a, v + 1);
                else
                    h1.put(a, 1L);
            for (long c : h1.values())
                count += c * (c - 1) * (c - 2) / 6;
        } else
            for (int i = 0; i < n; i++) {
                long a = in.nextLong();
                if ((p = h.get(a)) == null) {
                    h.put(a, new PL(1, 0));
                } else
                    p.x++;
                if ((a / k) * k == a && h.get(a / k) != null)
                    if ((p = h.get(a * k)) == null)
                        h.put(a * k, new PL(0, h.get(a / k).x));
                    else
                        p.y += h.get(a / k).x;
                if ((a / (k * k)) * k * k == a && h.get(a) != null){
                    count += h.get(a).y;
                }
            }
        out.println(count);
        out.close();
    }
}

class ArrayUtils {
    static final Random rand = new Random(System.nanoTime());

    static final public int[] sortAndUnique(int[] a) {
        int[] ret = a.clone();
        sort(ret);
        if (ret.length == 0) {
            return ret;
        }
        int last = ret[0];
        int j = 1;
        for (int i = 1; i < ret.length; i++)
            if (last != ret[i]) {
                ret[j++] = ret[i];
                last = ret[i];
            }
        return Arrays.copyOf(ret, j);
    }

    public static final void sort(int[] a) {
        shuffle(a);
        Arrays.sort(a);
    }

    public static final void shuffle(int[] a) {
        for (int i = 0; i < a.length; i++) {
            int j = rand.nextInt(i + 1);
            int t = a[i];
            a[i] = a[j];
            a[j] = t;
        }
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public String nextLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public int[] readIntArray(int n) {
        int[] ret = new int[n];
        for (int i = 0; i < n; i++)
            ret[i] = nextInt();
        return ret;
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }

    public BigInteger nextBigInt() {
        return new BigInteger(next());
    }
}

class FastPrinter extends PrintWriter {

    public FastPrinter(OutputStream out) {
        super(out);
    }

    public FastPrinter(Writer out) {
        super(out);
    }

}


import java.io.*;
import java.util.*;
import java.math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class GeometricProgression {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }
    static class TaskA {
        long mod = (long)(1000000007);
        public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
            while(testNumber-->0){
            	int n = in.nextInt();
            	int k = in.nextInt();
            	long a[] = new long[n];
            	for(int i=0;i<n;i++)
            		a[i] = in.nextLong();
            	HashMap<Long , ArrayList<Integer>> m = new HashMap<>();
            	for(int i=0;i<n;i++){
            		long x = a[i];
            		if(!m.containsKey(x))
            			m.put(x , new ArrayList<>());
            		m.get(x).add(i);
            	}
            	// out.println(m);
            	long count = 0;
            	for(long i:m.keySet()){
            		long a1 = i;
            		long a2 = a1*k;
            		long a3 = a2*k;
            		if(!m.containsKey(a2) || !m.containsKey(a3))
            			continue;
            		ArrayList<Integer> xy = m.get(a2);
            		long posLast[] = new long[xy.size()];
            		for(int j=0;j<xy.size();j++){
            			int pos = upperFirstBound(m.get(a3) , xy.get(j));
            			posLast[j] = m.get(a3).size() - pos;
            		}
            		for(int j = xy.size()-2;j>=0;j--)
            			posLast[j] += posLast[j+1];
            		// print1d(posLast , out);
            		xy = m.get(a1);
            		for(int j=0;j<xy.size();j++){
            			int pos = upperFirstBound(m.get(a2) , xy.get(j));
            			if(pos!=m.get(a2).size())
            				count += posLast[pos];
            		}
            		// out.println(i + " " + count);
            	}
            	out.println(count);
            }
        }
        public void update(int a[] , int index , int value){
        	for(int i=index;i<a.length;i += i&(-i))
        		a[i] += value;
        }
        public int get(int a[] , int index){
        	int res = 0;
        	for(int i=index;i>0;i-=i&(-i))
        		res += a[i];
        	return res;
        }
        public int[] nextGreater(int a[] , int n){
        	int ng[] = new int[n+1];
            Deque<Combine> s = new LinkedList<>();
            for(int i=n;i>0;i--){
                int k = i;
                Combine c = new Combine(a[k] , k);
                while(s.size()>0 && s.peekLast().value <= c.value){
                    Combine d = s.pollLast();
                    ng[d.delete] = c.delete;
                }
                s.addLast(c);
            }
            return ng;
        }
        public void sieve(int a[]){
            a[0] = a[1] = 1;
            int i;
            for(i=2;i*i<=a.length;i++){
                if(a[i] != 0)
                    continue;
                a[i] = i;
                for(int k = (i)*(i);k<a.length;k+=i){
                    if(a[k] != 0)
                        continue;
                    a[k] = i;
                }
            }
        }
        public int [][] matrixExpo(int c[][] , int n){
            int a[][] = new int[c.length][c[0].length];
            int b[][] = new int[a.length][a[0].length];
            for(int i=0;i<c.length;i++)
                for(int j=0;j<c[0].length;j++)
                    a[i][j] = c[i][j];
            for(int i=0;i<a.length;i++)
                b[i][i] = 1;
            while(n!=1){
                if(n%2 == 1){
                    b = matrixMultiply(a , a);
                    n--;
                }
                a = matrixMultiply(a , a);
                n/=2;
            }
            return matrixMultiply(a , b);
        }
        public int [][] matrixMultiply(int a[][] , int b[][]){
            int r1 = a.length;
            int c1 = a[0].length;
            int c2 = b[0].length;
            int c[][] = new int[r1][c2];
            for(int i=0;i<r1;i++){
                for(int j=0;j<c2;j++){
                    for(int k=0;k<c1;k++)
                        c[i][j] += a[i][k]*b[k][j];
                }
            }
            return c;
        }
        public long nCrPFermet(int n , int r , long p){
            if(r==0)
                return 1l;
            long fact[] = new long[n+1];
            fact[0] = 1;
            for(int i=1;i<=n;i++)
                fact[i] = (i*fact[i-1])%p;
            long modInverseR = pow(fact[r] , p-2 , 1l , p);
            long modInverseNR = pow(fact[n-r] , p-2 , 1l , p);
            long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p;
            return w;
        }
        public long pow(long a , long b , long res , long mod){
            if(b==0)
                return res;
            if(b==1)
                return (res*a)%mod;
            if(b%2==1){
                res *= a;
                res %= mod;
                b--;
            }
            // System.out.println(a + " " + b + " " + res);
            return pow((a*a)%mod , b/2 , res , mod);
        }
        public long pow(long a , long b , long res){
            if(b == 0)
                return res;
            if(b==1)
                return res*a;
            if(b%2==1){
                res *= a;
                b--;
            }
            return pow(a*a , b/2 , res);
        }
        public void swap(int a[] , int p1 , int p2){
            int x = a[p1];
            a[p1] = a[p2];
            a[p2] = x;
        }
        public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) {
            if (start > end) {
                return;
            }
            int mid = (start + end) / 2;
            a.add(mid);
            sortedArrayToBST(a, start, mid - 1);
            sortedArrayToBST(a, mid + 1, end);
        }
        class Combine{
            int value;
            int delete;
            Combine(int val , int delete){
                this.value = val;
                this.delete = delete;
            }
        }
        class Sort2 implements Comparator<Combine>{
            public int compare(Combine a , Combine b){
                if(a.value > b.value)
                    return 1;
                else if(a.value == b.value && a.delete>b.delete)
                    return 1;
                else if(a.value == b.value && a.delete == b.delete)
                    return 0;
                return -1;
            }
        }
        public int lowerLastBound(ArrayList<Integer> a , int x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>=x)
                return -1;
            if(a.get(r)<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid-1)<x)
                    return mid-1;
                else if(a.get(mid)>=x)
                    r = mid-1;
                else if(a.get(mid)<x && a.get(mid+1)>=x)
                    return mid;
                else if(a.get(mid)<x && a.get(mid+1)<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(ArrayList<Integer> a , Integer x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>x)
                return l;
            if(a.get(r)<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid+1)>x)
                    return mid+1;
                else if(a.get(mid)<=x)
                    l = mid+1;
                else if(a.get(mid)>x && a.get(mid-1)<=x)
                    return mid;
                else if(a.get(mid)>x && a.get(mid-1)>x)
                    r = mid-1;
            }
            return mid;
        }
        public int lowerLastBound(int a[] , int x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>=x)
                return -1;
            if(a[r]<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid-1]<x)
                    return mid-1;
                else if(a[mid]>=x)
                    r = mid-1;
                else if(a[mid]<x && a[mid+1]>=x)
                    return mid;
                else if(a[mid]<x && a[mid+1]<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(long a[] , long x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>x)
                return l;
            if(a[r]<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid+1]>x)
                    return mid+1;
                else if(a[mid]<=x)
                    l = mid+1;
                else if(a[mid]>x && a[mid-1]<=x)
                    return mid;
                else if(a[mid]>x && a[mid-1]>x)
                    r = mid-1;
            }
            return mid;
        }
        public long log(float number , int base){
            return (long) Math.floor((Math.log(number) / Math.log(base)));
        }
        public long gcd(long a , long b){
            if(a<b){
                long c = b;
                b = a;
                a = c;
            }
            if(b == 0)
                return a;
            return gcd(b , a%b);
        }
        public void print2d(long a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            out.println();
        }
        public void print1d(int a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
            out.println();
        }
        class Node{
        	int index;
        	Node right;
        	Node left;
        	public Node(int index){
        		this.index = index;
        		right = null;
        		left = null;
        	}
        }
        class AVLTree{
            Node root;
            public AVLTree(){
                this.root = null;
            }
            public int height(Node n){
                return (n == null ? 0 : n.height);
            }
            public int getBalance(Node n){
                return (n == null ? 0 : height(n.left) - height(n.right));
            }
            public Node rotateRight(Node a){
                Node b = a.left;
                Node br = b.right;

                a.lSum -= b.lSum;
                a.lCount -= b.lCount;

                b.rSum += a.rSum;
                b.rCount += a.rCount;

                b.right = a;
                a.left = br;

                a.height = 1 + Math.max(height(a.left) , height(a.right));
                b.height = 1 + Math.max(height(b.left) , height(b.right));
                return b;
            }
            public Node rotateLeft(Node a){
                Node b = a.right;
                Node bl = b.left;

                a.rSum -= b.rSum;
                a.rCount -= b.rCount;

                b.lSum += a.lSum;
                b.lCount += a.lCount;

                b.left = a;
                a.right = bl;

                a.height = 1 + Math.max(height(a.left) , height(a.right));
                b.height = 1 + Math.max(height(b.left) , height(b.right));
                return b;
            }
            public Node insert(Node root , long value){
                if(root == null){
                    return new Node(value);
                }
                if(value<=root.value){
                    root.lCount++;
                    root.lSum += value;
                    root.left =  insert(root.left , value);
                }
                if(value>root.value){
                    root.rCount++;
                    root.rSum += value;
                    root.right = insert(root.right , value);
                }
                // updating the height of the root
                root.height = 1 + Math.max(height(root.left) , height(root.right));
                int balance = getBalance(root);

                //ll
                if(balance>1 && value<=root.left.value)
                    return rotateRight(root);
                //rr
                if(balance<-1 && value>root.right.value)
                    return rotateLeft(root);
                //lr
                if(balance>1 && value>root.left.value){
                    root.left = rotateLeft(root.left);
                    return rotateRight(root);
                }
                //rl
                if(balance<-1 && value<=root.right.value){
                    root.right = rotateRight(root.right);
                    return rotateLeft(root);
                }
                return root;
            }
            public void insertElement(long value){
                this.root = insert(root , value);
            }
            public int getElementLessThanK(long k){
                int count = 0;
                Node temp = root;
                while(temp!=null){
                    if(temp.value == k){
                        if(temp.left == null || temp.left.value<k){
                            count += temp.lCount;
                            return count-1;
                        }
                        else
                            temp = temp.left;
                    }
                    else if(temp.value>k){
                        temp = temp.left;
                    }
                    else{
                        count += temp.lCount;
                        temp = temp.right;
                    }
                }
                return count;
            }
            public void inorder(Node root , PrintWriter out){
                Node temp = root;
                if(temp!=null){
                    inorder(temp.left , out);
                    out.println(temp.value + " " + temp.lCount + " " + temp.rCount);
                    inorder(temp.right , out);
                }
            }
            class Node{
	            long value;
	            long lCount , rCount;
	            long lSum , rSum;
	            Node left , right;
	            int height;
	            public Node(long value){
	                this.value = value;
	                left = null;
	                right = null;
	                lCount = 1;
	                rCount = 1;
	                lSum = value;
	                rSum = value;
	                height = 1;
	            }
	        }
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}


import java.io.*;
import java.util.*;
import java.math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class GeometricProgression {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }
    static class TaskA {
        long mod = (long)(1000000007);
        public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
            while(testNumber-->0){
            	int n = in.nextInt();
            	int k = in.nextInt();
            	long a[] = new long[n];
            	for(int i=0;i<n;i++)
            		a[i] = in.nextLong();
            	HashMap<Long , ArrayList<Integer>> m = new HashMap<>();
            	for(int i=0;i<n;i++){
            		long x = a[i];
            		if(!m.containsKey(x))
            			m.put(x , new ArrayList<>());
            		m.get(x).add(i);
            	}
            	// out.println(m);
            	long count = 0;
            	for(long i:m.keySet()){
            		long a1 = i;
            		long a2 = a1*k;
            		long a3 = a2*k;
            		if(!m.containsKey(a2) || !m.containsKey(a3))
            			continue;
            		ArrayList<Integer> xy = m.get(a2);
            		int posLast[] = new int[xy.size()];
            		for(int j=0;j<xy.size();j++){
            			int pos = upperFirstBound(m.get(a3) , xy.get(j));
            			posLast[j] = m.get(a3).size() - pos;
            		}
            		for(int j = xy.size()-2;j>=0;j--)
            			posLast[j] += posLast[j+1];
            		// print1d(posLast , out);
            		xy = m.get(a1);
            		for(int j=0;j<xy.size();j++){
            			int pos = upperFirstBound(m.get(a2) , xy.get(j));
            			if(pos!=m.get(a2).size())
            				count += posLast[pos];
            		}
            		// out.println(i + " " + count);
            	}
            	out.println(count);
            }
        }
        public void update(int a[] , int index , int value){
        	for(int i=index;i<a.length;i += i&(-i))
        		a[i] += value;
        }
        public int get(int a[] , int index){
        	int res = 0;
        	for(int i=index;i>0;i-=i&(-i))
        		res += a[i];
        	return res;
        }
        public int[] nextGreater(int a[] , int n){
        	int ng[] = new int[n+1];
            Deque<Combine> s = new LinkedList<>();
            for(int i=n;i>0;i--){
                int k = i;
                Combine c = new Combine(a[k] , k);
                while(s.size()>0 && s.peekLast().value <= c.value){
                    Combine d = s.pollLast();
                    ng[d.delete] = c.delete;
                }
                s.addLast(c);
            }
            return ng;
        }
        public void sieve(int a[]){
            a[0] = a[1] = 1;
            int i;
            for(i=2;i*i<=a.length;i++){
                if(a[i] != 0)
                    continue;
                a[i] = i;
                for(int k = (i)*(i);k<a.length;k+=i){
                    if(a[k] != 0)
                        continue;
                    a[k] = i;
                }
            }
        }
        public int [][] matrixExpo(int c[][] , int n){
            int a[][] = new int[c.length][c[0].length];
            int b[][] = new int[a.length][a[0].length];
            for(int i=0;i<c.length;i++)
                for(int j=0;j<c[0].length;j++)
                    a[i][j] = c[i][j];
            for(int i=0;i<a.length;i++)
                b[i][i] = 1;
            while(n!=1){
                if(n%2 == 1){
                    b = matrixMultiply(a , a);
                    n--;
                }
                a = matrixMultiply(a , a);
                n/=2;
            }
            return matrixMultiply(a , b);
        }
        public int [][] matrixMultiply(int a[][] , int b[][]){
            int r1 = a.length;
            int c1 = a[0].length;
            int c2 = b[0].length;
            int c[][] = new int[r1][c2];
            for(int i=0;i<r1;i++){
                for(int j=0;j<c2;j++){
                    for(int k=0;k<c1;k++)
                        c[i][j] += a[i][k]*b[k][j];
                }
            }
            return c;
        }
        public long nCrPFermet(int n , int r , long p){
            if(r==0)
                return 1l;
            long fact[] = new long[n+1];
            fact[0] = 1;
            for(int i=1;i<=n;i++)
                fact[i] = (i*fact[i-1])%p;
            long modInverseR = pow(fact[r] , p-2 , 1l , p);
            long modInverseNR = pow(fact[n-r] , p-2 , 1l , p);
            long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p;
            return w;
        }
        public long pow(long a , long b , long res , long mod){
            if(b==0)
                return res;
            if(b==1)
                return (res*a)%mod;
            if(b%2==1){
                res *= a;
                res %= mod;
                b--;
            }
            // System.out.println(a + " " + b + " " + res);
            return pow((a*a)%mod , b/2 , res , mod);
        }
        public long pow(long a , long b , long res){
            if(b == 0)
                return res;
            if(b==1)
                return res*a;
            if(b%2==1){
                res *= a;
                b--;
            }
            return pow(a*a , b/2 , res);
        }
        public void swap(int a[] , int p1 , int p2){
            int x = a[p1];
            a[p1] = a[p2];
            a[p2] = x;
        }
        public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) {
            if (start > end) {
                return;
            }
            int mid = (start + end) / 2;
            a.add(mid);
            sortedArrayToBST(a, start, mid - 1);
            sortedArrayToBST(a, mid + 1, end);
        }
        class Combine{
            int value;
            int delete;
            Combine(int val , int delete){
                this.value = val;
                this.delete = delete;
            }
        }
        class Sort2 implements Comparator<Combine>{
            public int compare(Combine a , Combine b){
                if(a.value > b.value)
                    return 1;
                else if(a.value == b.value && a.delete>b.delete)
                    return 1;
                else if(a.value == b.value && a.delete == b.delete)
                    return 0;
                return -1;
            }
        }
        public int lowerLastBound(ArrayList<Integer> a , int x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>=x)
                return -1;
            if(a.get(r)<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid-1)<x)
                    return mid-1;
                else if(a.get(mid)>=x)
                    r = mid-1;
                else if(a.get(mid)<x && a.get(mid+1)>=x)
                    return mid;
                else if(a.get(mid)<x && a.get(mid+1)<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(ArrayList<Integer> a , Integer x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>x)
                return l;
            if(a.get(r)<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid+1)>x)
                    return mid+1;
                else if(a.get(mid)<=x)
                    l = mid+1;
                else if(a.get(mid)>x && a.get(mid-1)<=x)
                    return mid;
                else if(a.get(mid)>x && a.get(mid-1)>x)
                    r = mid-1;
            }
            return mid;
        }
        public int lowerLastBound(int a[] , int x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>=x)
                return -1;
            if(a[r]<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid-1]<x)
                    return mid-1;
                else if(a[mid]>=x)
                    r = mid-1;
                else if(a[mid]<x && a[mid+1]>=x)
                    return mid;
                else if(a[mid]<x && a[mid+1]<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(long a[] , long x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>x)
                return l;
            if(a[r]<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid+1]>x)
                    return mid+1;
                else if(a[mid]<=x)
                    l = mid+1;
                else if(a[mid]>x && a[mid-1]<=x)
                    return mid;
                else if(a[mid]>x && a[mid-1]>x)
                    r = mid-1;
            }
            return mid;
        }
        public long log(float number , int base){
            return (long) Math.floor((Math.log(number) / Math.log(base)));
        }
        public long gcd(long a , long b){
            if(a<b){
                long c = b;
                b = a;
                a = c;
            }
            if(b == 0)
                return a;
            return gcd(b , a%b);
        }
        public void print2d(long a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            out.println();
        }
        public void print1d(int a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
            out.println();
        }
        class Node{
        	int index;
        	Node right;
        	Node left;
        	public Node(int index){
        		this.index = index;
        		right = null;
        		left = null;
        	}
        }
        class AVLTree{
            Node root;
            public AVLTree(){
                this.root = null;
            }
            public int height(Node n){
                return (n == null ? 0 : n.height);
            }
            public int getBalance(Node n){
                return (n == null ? 0 : height(n.left) - height(n.right));
            }
            public Node rotateRight(Node a){
                Node b = a.left;
                Node br = b.right;

                a.lSum -= b.lSum;
                a.lCount -= b.lCount;

                b.rSum += a.rSum;
                b.rCount += a.rCount;

                b.right = a;
                a.left = br;

                a.height = 1 + Math.max(height(a.left) , height(a.right));
                b.height = 1 + Math.max(height(b.left) , height(b.right));
                return b;
            }
            public Node rotateLeft(Node a){
                Node b = a.right;
                Node bl = b.left;

                a.rSum -= b.rSum;
                a.rCount -= b.rCount;

                b.lSum += a.lSum;
                b.lCount += a.lCount;

                b.left = a;
                a.right = bl;

                a.height = 1 + Math.max(height(a.left) , height(a.right));
                b.height = 1 + Math.max(height(b.left) , height(b.right));
                return b;
            }
            public Node insert(Node root , long value){
                if(root == null){
                    return new Node(value);
                }
                if(value<=root.value){
                    root.lCount++;
                    root.lSum += value;
                    root.left =  insert(root.left , value);
                }
                if(value>root.value){
                    root.rCount++;
                    root.rSum += value;
                    root.right = insert(root.right , value);
                }
                // updating the height of the root
                root.height = 1 + Math.max(height(root.left) , height(root.right));
                int balance = getBalance(root);

                //ll
                if(balance>1 && value<=root.left.value)
                    return rotateRight(root);
                //rr
                if(balance<-1 && value>root.right.value)
                    return rotateLeft(root);
                //lr
                if(balance>1 && value>root.left.value){
                    root.left = rotateLeft(root.left);
                    return rotateRight(root);
                }
                //rl
                if(balance<-1 && value<=root.right.value){
                    root.right = rotateRight(root.right);
                    return rotateLeft(root);
                }
                return root;
            }
            public void insertElement(long value){
                this.root = insert(root , value);
            }
            public int getElementLessThanK(long k){
                int count = 0;
                Node temp = root;
                while(temp!=null){
                    if(temp.value == k){
                        if(temp.left == null || temp.left.value<k){
                            count += temp.lCount;
                            return count-1;
                        }
                        else
                            temp = temp.left;
                    }
                    else if(temp.value>k){
                        temp = temp.left;
                    }
                    else{
                        count += temp.lCount;
                        temp = temp.right;
                    }
                }
                return count;
            }
            public void inorder(Node root , PrintWriter out){
                Node temp = root;
                if(temp!=null){
                    inorder(temp.left , out);
                    out.println(temp.value + " " + temp.lCount + " " + temp.rCount);
                    inorder(temp.right , out);
                }
            }
            class Node{
	            long value;
	            long lCount , rCount;
	            long lSum , rSum;
	            Node left , right;
	            int height;
	            public Node(long value){
	                this.value = value;
	                left = null;
	                right = null;
	                lCount = 1;
	                rCount = 1;
	                lSum = value;
	                rSum = value;
	                height = 1;
	            }
	        }
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}


import java.io.*;
import java.util.*;
import java.math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class GeometricProgression {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }
    static class TaskA {
        long mod = (long)(1000000007);
        public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
            while(testNumber-->0){
            	int n = in.nextInt();
            	int k = in.nextInt();
            	long a[] = new long[n];
            	for(int i=0;i<n;i++)
            		a[i] = in.nextLong();
            	HashMap<Long , ArrayList<Integer>> m = new HashMap<>();
            	for(int i=0;i<n;i++){
            		long x = a[i];
            		if(!m.containsKey(x))
            			m.put(x , new ArrayList<>());
            		m.get(x).add(i);
            	}
            	long count = 0;
            	for(long i:m.keySet()){
            		long a1 = i;
            		long a2 = a1*k;
            		long a3 = a2*k;
            		if(!m.containsKey(a2) || !m.containsKey(a3))
            			continue;
            		ArrayList<Integer> xy = m.get(a2);
            		int posLast[] = new int[xy.size()];
            		for(int j=0;j<xy.size();j++){
            			int pos = upperFirstBound(m.get(a3) , xy.get(j));
            			posLast[j] = xy.size() - pos;
            		}
            		xy = m.get(a1);
            		for(int j=0;j<xy.size();j++){
            			int pos = upperFirstBound(m.get(a2) , xy.get(j));
            			if(pos!=m.get(a2).size())
            				count += posLast[pos];
            		}
            	}
            	out.println(count);
            }
        }
        public void update(int a[] , int index , int value){
        	for(int i=index;i<a.length;i += i&(-i))
        		a[i] += value;
        }
        public int get(int a[] , int index){
        	int res = 0;
        	for(int i=index;i>0;i-=i&(-i))
        		res += a[i];
        	return res;
        }
        public int[] nextGreater(int a[] , int n){
        	int ng[] = new int[n+1];
            Deque<Combine> s = new LinkedList<>();
            for(int i=n;i>0;i--){
                int k = i;
                Combine c = new Combine(a[k] , k);
                while(s.size()>0 && s.peekLast().value <= c.value){
                    Combine d = s.pollLast();
                    ng[d.delete] = c.delete;
                }
                s.addLast(c);
            }
            return ng;
        }
        public void sieve(int a[]){
            a[0] = a[1] = 1;
            int i;
            for(i=2;i*i<=a.length;i++){
                if(a[i] != 0)
                    continue;
                a[i] = i;
                for(int k = (i)*(i);k<a.length;k+=i){
                    if(a[k] != 0)
                        continue;
                    a[k] = i;
                }
            }
        }
        public int [][] matrixExpo(int c[][] , int n){
            int a[][] = new int[c.length][c[0].length];
            int b[][] = new int[a.length][a[0].length];
            for(int i=0;i<c.length;i++)
                for(int j=0;j<c[0].length;j++)
                    a[i][j] = c[i][j];
            for(int i=0;i<a.length;i++)
                b[i][i] = 1;
            while(n!=1){
                if(n%2 == 1){
                    b = matrixMultiply(a , a);
                    n--;
                }
                a = matrixMultiply(a , a);
                n/=2;
            }
            return matrixMultiply(a , b);
        }
        public int [][] matrixMultiply(int a[][] , int b[][]){
            int r1 = a.length;
            int c1 = a[0].length;
            int c2 = b[0].length;
            int c[][] = new int[r1][c2];
            for(int i=0;i<r1;i++){
                for(int j=0;j<c2;j++){
                    for(int k=0;k<c1;k++)
                        c[i][j] += a[i][k]*b[k][j];
                }
            }
            return c;
        }
        public long nCrPFermet(int n , int r , long p){
            if(r==0)
                return 1l;
            long fact[] = new long[n+1];
            fact[0] = 1;
            for(int i=1;i<=n;i++)
                fact[i] = (i*fact[i-1])%p;
            long modInverseR = pow(fact[r] , p-2 , 1l , p);
            long modInverseNR = pow(fact[n-r] , p-2 , 1l , p);
            long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p;
            return w;
        }
        public long pow(long a , long b , long res , long mod){
            if(b==0)
                return res;
            if(b==1)
                return (res*a)%mod;
            if(b%2==1){
                res *= a;
                res %= mod;
                b--;
            }
            // System.out.println(a + " " + b + " " + res);
            return pow((a*a)%mod , b/2 , res , mod);
        }
        public long pow(long a , long b , long res){
            if(b == 0)
                return res;
            if(b==1)
                return res*a;
            if(b%2==1){
                res *= a;
                b--;
            }
            return pow(a*a , b/2 , res);
        }
        public void swap(int a[] , int p1 , int p2){
            int x = a[p1];
            a[p1] = a[p2];
            a[p2] = x;
        }
        public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) {
            if (start > end) {
                return;
            }
            int mid = (start + end) / 2;
            a.add(mid);
            sortedArrayToBST(a, start, mid - 1);
            sortedArrayToBST(a, mid + 1, end);
        }
        class Combine{
            int value;
            int delete;
            Combine(int val , int delete){
                this.value = val;
                this.delete = delete;
            }
        }
        class Sort2 implements Comparator<Combine>{
            public int compare(Combine a , Combine b){
                if(a.value > b.value)
                    return 1;
                else if(a.value == b.value && a.delete>b.delete)
                    return 1;
                else if(a.value == b.value && a.delete == b.delete)
                    return 0;
                return -1;
            }
        }
        public int lowerLastBound(ArrayList<Integer> a , int x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>=x)
                return -1;
            if(a.get(r)<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid-1)<x)
                    return mid-1;
                else if(a.get(mid)>=x)
                    r = mid-1;
                else if(a.get(mid)<x && a.get(mid+1)>=x)
                    return mid;
                else if(a.get(mid)<x && a.get(mid+1)<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(ArrayList<Integer> a , Integer x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>x)
                return l;
            if(a.get(r)<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid+1)>x)
                    return mid+1;
                else if(a.get(mid)<=x)
                    l = mid+1;
                else if(a.get(mid)>x && a.get(mid-1)<=x)
                    return mid;
                else if(a.get(mid)>x && a.get(mid-1)>x)
                    r = mid-1;
            }
            return mid;
        }
        public int lowerLastBound(int a[] , int x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>=x)
                return -1;
            if(a[r]<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid-1]<x)
                    return mid-1;
                else if(a[mid]>=x)
                    r = mid-1;
                else if(a[mid]<x && a[mid+1]>=x)
                    return mid;
                else if(a[mid]<x && a[mid+1]<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(long a[] , long x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>x)
                return l;
            if(a[r]<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid+1]>x)
                    return mid+1;
                else if(a[mid]<=x)
                    l = mid+1;
                else if(a[mid]>x && a[mid-1]<=x)
                    return mid;
                else if(a[mid]>x && a[mid-1]>x)
                    r = mid-1;
            }
            return mid;
        }
        public long log(float number , int base){
            return (long) Math.floor((Math.log(number) / Math.log(base)));
        }
        public long gcd(long a , long b){
            if(a<b){
                long c = b;
                b = a;
                a = c;
            }
            if(b == 0)
                return a;
            return gcd(b , a%b);
        }
        public void print2d(long a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            out.println();
        }
        public void print1d(int a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
            out.println();
        }
        class Node{
        	int index;
        	Node right;
        	Node left;
        	public Node(int index){
        		this.index = index;
        		right = null;
        		left = null;
        	}
        }
        class AVLTree{
            Node root;
            public AVLTree(){
                this.root = null;
            }
            public int height(Node n){
                return (n == null ? 0 : n.height);
            }
            public int getBalance(Node n){
                return (n == null ? 0 : height(n.left) - height(n.right));
            }
            public Node rotateRight(Node a){
                Node b = a.left;
                Node br = b.right;

                a.lSum -= b.lSum;
                a.lCount -= b.lCount;

                b.rSum += a.rSum;
                b.rCount += a.rCount;

                b.right = a;
                a.left = br;

                a.height = 1 + Math.max(height(a.left) , height(a.right));
                b.height = 1 + Math.max(height(b.left) , height(b.right));
                return b;
            }
            public Node rotateLeft(Node a){
                Node b = a.right;
                Node bl = b.left;

                a.rSum -= b.rSum;
                a.rCount -= b.rCount;

                b.lSum += a.lSum;
                b.lCount += a.lCount;

                b.left = a;
                a.right = bl;

                a.height = 1 + Math.max(height(a.left) , height(a.right));
                b.height = 1 + Math.max(height(b.left) , height(b.right));
                return b;
            }
            public Node insert(Node root , long value){
                if(root == null){
                    return new Node(value);
                }
                if(value<=root.value){
                    root.lCount++;
                    root.lSum += value;
                    root.left =  insert(root.left , value);
                }
                if(value>root.value){
                    root.rCount++;
                    root.rSum += value;
                    root.right = insert(root.right , value);
                }
                // updating the height of the root
                root.height = 1 + Math.max(height(root.left) , height(root.right));
                int balance = getBalance(root);

                //ll
                if(balance>1 && value<=root.left.value)
                    return rotateRight(root);
                //rr
                if(balance<-1 && value>root.right.value)
                    return rotateLeft(root);
                //lr
                if(balance>1 && value>root.left.value){
                    root.left = rotateLeft(root.left);
                    return rotateRight(root);
                }
                //rl
                if(balance<-1 && value<=root.right.value){
                    root.right = rotateRight(root.right);
                    return rotateLeft(root);
                }
                return root;
            }
            public void insertElement(long value){
                this.root = insert(root , value);
            }
            public int getElementLessThanK(long k){
                int count = 0;
                Node temp = root;
                while(temp!=null){
                    if(temp.value == k){
                        if(temp.left == null || temp.left.value<k){
                            count += temp.lCount;
                            return count-1;
                        }
                        else
                            temp = temp.left;
                    }
                    else if(temp.value>k){
                        temp = temp.left;
                    }
                    else{
                        count += temp.lCount;
                        temp = temp.right;
                    }
                }
                return count;
            }
            public void inorder(Node root , PrintWriter out){
                Node temp = root;
                if(temp!=null){
                    inorder(temp.left , out);
                    out.println(temp.value + " " + temp.lCount + " " + temp.rCount);
                    inorder(temp.right , out);
                }
            }
            class Node{
	            long value;
	            long lCount , rCount;
	            long lSum , rSum;
	            Node left , right;
	            int height;
	            public Node(long value){
	                this.value = value;
	                left = null;
	                right = null;
	                lCount = 1;
	                rCount = 1;
	                lSum = value;
	                rSum = value;
	                height = 1;
	            }
	        }
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}


import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeMap;

public class A implements Runnable {
	static String Y = "YES", N = "NO";
	static long inf = (long) 1e16;

	public static void main(String[] args) {
		new Thread(null, new A(), "Solver", 1L << 26).start();
	}

	private Scanner scan;
	private PrintWriter out;

	public A() {
		scan = new Scanner(System.in);
		out = new PrintWriter(System.out);
	}

	public void run() {
		try {
			solve();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(1 / 0);
		}
	}
	static long ceilDiv(long n, long d) {
		if(n < 0 != d < 0) {
			return (n / d) - (n % d == 0 ? 0 : 1);
		}
		return (n / d) + (n % d == 0 ? 0 : 1);
	}

	void solve() {
		int n = scan.nextInt(), k = scan.nextInt();
		int[] vs = new int[n];
		TreeMap<Integer, Integer> freqLeft = new TreeMap<>(), freqRight = new TreeMap<>();
		for(int i=0;i<n;i++) {
			vs[i] = scan.nextInt();
			freqLeft.put(vs[i], 0);
			freqRight.put(vs[i], 0);
		}
		if(n < 3) {
			out.println(0);
			return;
		}
		for(int i=1;i<n;i++) {
			freqRight.put(vs[i], freqRight.get(vs[i])+1);
		}
		long ans = 0;
		for(int m=1;m<n-1;m++) {
			freqRight.put(vs[m], freqRight.get(vs[m])-1);
			freqLeft.put(vs[m-1], freqLeft.get(vs[m-1])+1);
			if(vs[m] % k != 0) continue;
			if(!freqLeft.containsKey(vs[m]/k) || !freqRight.containsKey(vs[m] * k)) continue;
			ans += freqLeft.get(vs[m]/k) * freqRight.get(vs[m] * k);
		}
		out.println(ans);
	}
	static class Pair implements Comparable<Pair>{
		long a,b;
		public Pair(long a, long b) {
			this.a = a;
			this.b = b;
		}
		@Override
		public int compareTo(Pair o) {
			if(a == o.a)
				return Long.compare(b, o.b);
			return Long.compare(a,o.a);
		}
	}

	static long max(long... ls) {
		long max = ls[0];
		for (long l : ls) {
			if (l > max)
				max = l;
		}
		return max;
	}

	static int max(int... ls) {
		int max = ls[0];
		for (int l : ls) {
			if (l > max)
				max = l;
		}
		return max;
	}

	static double max(double... ls) {
		double max = ls[0];
		for (double l : ls) {
			if (l > max)
				max = l;
		}
		return max;
	}

	static long min(long... ls) {
		long min = ls[0];
		for (long l : ls) {
			if (l < min)
				min = l;
		}
		return min;
	}

	static double min(double... ls) {
		double min = ls[0];
		for (double l : ls) {
			if (l < min)
				min = l;
		}
		return min;
	}

	static int min(int... ls) {
		int min = ls[0];
		for (int l : ls) {
			if (l < min)
				min = l;
		}
		return min;
	}

	static void yes(boolean a) {
		System.out.println(a ? Y : N);
	}

	static long gcd(long a, long b) {
		if (b == 0)
			return a;
		return gcd(b, a % b);
	}
}


import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeMap;

public class A implements Runnable {
	static String Y = "YES", N = "NO";
	static long inf = (long) 1e16;

	public static void main(String[] args) {
		new Thread(null, new A(), "Solver", 1L << 26).start();
	}

	private Scanner scan;
	private PrintWriter out;

	public A() {
		scan = new Scanner(System.in);
		out = new PrintWriter(System.out);
	}

	public void run() {
		try {
			solve();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(1 / 0);
		}
	}
	static long ceilDiv(long n, long d) {
		if(n < 0 != d < 0) {
			return (n / d) - (n % d == 0 ? 0 : 1);
		}
		return (n / d) + (n % d == 0 ? 0 : 1);
	}

	void solve() {
		int n = scan.nextInt(), k = scan.nextInt();
		int[] vs = new int[n];
		TreeMap<Integer, Integer> freqLeft = new TreeMap<>(), freqRight = new TreeMap<>();
		for(int i=0;i<n;i++) {
			vs[i] = scan.nextInt();
			freqLeft.put(vs[i], 0);
			freqRight.put(vs[i], 0);
		}
		if(n < 3) {
			out.println(0);
			return;
		}
		for(int i=1;i<n;i++) {
			freqRight.put(vs[i], freqRight.get(vs[i])+1);
		}
		long ans = 0;
		for(int m=1;m<n-1;m++) {
			freqRight.put(vs[m], freqRight.get(vs[m])-1);
			freqLeft.put(vs[m-1], freqLeft.get(vs[m-1])+1);
			if(vs[m] % k != 0) continue;
			if(!freqLeft.containsKey(vs[m]/k) || !freqRight.containsKey(vs[m] * k)) continue;
			ans += freqLeft.get(vs[m]/k) * (long)freqRight.get(vs[m] * k);
		}
		out.println(ans);
	}
	static class Pair implements Comparable<Pair>{
		long a,b;
		public Pair(long a, long b) {
			this.a = a;
			this.b = b;
		}
		@Override
		public int compareTo(Pair o) {
			if(a == o.a)
				return Long.compare(b, o.b);
			return Long.compare(a,o.a);
		}
	}

	static long max(long... ls) {
		long max = ls[0];
		for (long l : ls) {
			if (l > max)
				max = l;
		}
		return max;
	}

	static int max(int... ls) {
		int max = ls[0];
		for (int l : ls) {
			if (l > max)
				max = l;
		}
		return max;
	}

	static double max(double... ls) {
		double max = ls[0];
		for (double l : ls) {
			if (l > max)
				max = l;
		}
		return max;
	}

	static long min(long... ls) {
		long min = ls[0];
		for (long l : ls) {
			if (l < min)
				min = l;
		}
		return min;
	}

	static double min(double... ls) {
		double min = ls[0];
		for (double l : ls) {
			if (l < min)
				min = l;
		}
		return min;
	}

	static int min(int... ls) {
		int min = ls[0];
		for (int l : ls) {
			if (l < min)
				min = l;
		}
		return min;
	}

	static void yes(boolean a) {
		System.out.println(a ? Y : N);
	}

	static long gcd(long a, long b) {
		if (b == 0)
			return a;
		return gcd(b, a % b);
	}
}


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

public class A implements Runnable {
	static String Y = "YES", N = "NO";
	static long inf = (long) 1e16;

	public static void main(String[] args) {
		new Thread(null, new A(), "Solver", 1L << 26).start();
	}

	private FastScanner scan;
	private PrintWriter out;

	public A() {
		scan = new FastScanner(System.in);
		out = new PrintWriter(System.out);
	}

	public void run() {
		try {
			solve();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(1 / 0);
		}
	}

	static long ceilDiv(long n, long d) {
		if (n < 0 != d < 0) {
			return (n / d) - (n % d == 0 ? 0 : 1);
		}
		return (n / d) + (n % d == 0 ? 0 : 1);
	}

	void solve() throws IOException {
		int n = scan.nextInt(), k = scan.nextInt();
		long[] vs = new long[n];
		HashMap<Long, Integer> freqLeft = new HashMap<>(), freqRight = new HashMap<>();
		for (int i = 0; i < n; i++) {
			vs[i] = scan.nextInt();
			freqLeft.put(vs[i], 0);
			freqRight.put(vs[i], 0);
		}
		if (n < 3) {
			out.println(0);
			return;
		}
		for (int i = 1; i < n; i++) {
			freqRight.put(vs[i], freqRight.get(vs[i]) + 1);
		}
		long ans = 0;
		for (int m = 1; m < n - 1; m++) {
			freqRight.put(vs[m], freqRight.get(vs[m]) - 1);
			freqLeft.put(vs[m - 1], freqLeft.get(vs[m - 1]) + 1);
			if (vs[m] % k != 0)
				continue;
			if (!freqLeft.containsKey(vs[m] / k) || !freqRight.containsKey(vs[m] * k))
				continue;
			ans += freqLeft.get(vs[m] / k) * (long) freqRight.get(vs[m] * k);
		}
		out.println(ans);
	}

	static class Pair implements Comparable<Pair> {
		long a, b;

		public Pair(long a, long b) {
			this.a = a;
			this.b = b;
		}

		@Override
		public int compareTo(Pair o) {
			if (a == o.a)
				return Long.compare(b, o.b);
			return Long.compare(a, o.a);
		}
	}

	static long max(long... ls) {
		long max = ls[0];
		for (long l : ls) {
			if (l > max)
				max = l;
		}
		return max;
	}

	static int max(int... ls) {
		int max = ls[0];
		for (int l : ls) {
			if (l > max)
				max = l;
		}
		return max;
	}

	static double max(double... ls) {
		double max = ls[0];
		for (double l : ls) {
			if (l > max)
				max = l;
		}
		return max;
	}

	static long min(long... ls) {
		long min = ls[0];
		for (long l : ls) {
			if (l < min)
				min = l;
		}
		return min;
	}

	static double min(double... ls) {
		double min = ls[0];
		for (double l : ls) {
			if (l < min)
				min = l;
		}
		return min;
	}

	static int min(int... ls) {
		int min = ls[0];
		for (int l : ls) {
			if (l < min)
				min = l;
		}
		return min;
	}

	static void yes(boolean a) {
		System.out.println(a ? Y : N);
	}

	static long gcd(long a, long b) {
		if (b == 0)
			return a;
		return gcd(b, a % b);
	}

	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;
		public FastScanner(InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
			st = new StringTokenizer("");
		}
		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
		public String next() throws IOException {
			while(!st.hasMoreTokens()) {
				st = new StringTokenizer(br.readLine());
			}
			return st.nextToken();
		}
	}
}


import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeMap;

public class A implements Runnable {
	static String Y = "YES", N = "NO";
	static long inf = (long) 1e16;

	public static void main(String[] args) {
		new Thread(null, new A(), "Solver", 1L << 26).start();
	}

	private Scanner scan;
	private PrintWriter out;

	public A() {
		scan = new Scanner(System.in);
		out = new PrintWriter(System.out);
	}

	public void run() {
		try {
			solve();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(1 / 0);
		}
	}
	static long ceilDiv(long n, long d) {
		if(n < 0 != d < 0) {
			return (n / d) - (n % d == 0 ? 0 : 1);
		}
		return (n / d) + (n % d == 0 ? 0 : 1);
	}

	void solve() {
		int n = scan.nextInt(), k = scan.nextInt();
		long[] vs = new long[n];
		HashMap<Long, Integer> freqLeft = new HashMap<>(), freqRight = new HashMap<>();
		for(int i=0;i<n;i++) {
			vs[i] = scan.nextInt();
			freqLeft.put(vs[i], 0);
			freqRight.put(vs[i], 0);
		}
		if(n < 3) {
			out.println(0);
			return;
		}
		for(int i=1;i<n;i++) {
			freqRight.put(vs[i], freqRight.get(vs[i])+1);
		}
		long ans = 0;
		for(int m=1;m<n-1;m++) {
			freqRight.put(vs[m], freqRight.get(vs[m])-1);
			freqLeft.put(vs[m-1], freqLeft.get(vs[m-1])+1);
			if(vs[m] % k != 0) continue;
			if(!freqLeft.containsKey(vs[m]/k) || !freqRight.containsKey(vs[m] * k)) continue;
			ans += freqLeft.get(vs[m]/k) * (long)freqRight.get(vs[m] * k);
		}
		out.println(ans);
	}
	static class Pair implements Comparable<Pair>{
		long a,b;
		public Pair(long a, long b) {
			this.a = a;
			this.b = b;
		}
		@Override
		public int compareTo(Pair o) {
			if(a == o.a)
				return Long.compare(b, o.b);
			return Long.compare(a,o.a);
		}
	}

	static long max(long... ls) {
		long max = ls[0];
		for (long l : ls) {
			if (l > max)
				max = l;
		}
		return max;
	}

	static int max(int... ls) {
		int max = ls[0];
		for (int l : ls) {
			if (l > max)
				max = l;
		}
		return max;
	}

	static double max(double... ls) {
		double max = ls[0];
		for (double l : ls) {
			if (l > max)
				max = l;
		}
		return max;
	}

	static long min(long... ls) {
		long min = ls[0];
		for (long l : ls) {
			if (l < min)
				min = l;
		}
		return min;
	}

	static double min(double... ls) {
		double min = ls[0];
		for (double l : ls) {
			if (l < min)
				min = l;
		}
		return min;
	}

	static int min(int... ls) {
		int min = ls[0];
		for (int l : ls) {
			if (l < min)
				min = l;
		}
		return min;
	}

	static void yes(boolean a) {
		System.out.println(a ? Y : N);
	}

	static long gcd(long a, long b) {
		if (b == 0)
			return a;
		return gcd(b, a % b);
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class GeometricProgression {
    public static void main(String args[])throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder output = new StringBuilder();
        String split[] = br.readLine().split(" ");
        int n = Integer.parseInt(split[0]);
        int k = Integer.parseInt(split[1]);
        HashMap<Integer,Integer> right=new HashMap<>();
        HashMap<Integer,Integer> left=new HashMap<>();

        int arr[]=new int[n];
        split=br.readLine().split(" ");
        for (int i = 0; i <n ; i++) {
            arr[i]=Integer.parseInt(split[i]);
            if(right.containsKey(arr[i]))
                right.put(arr[i],right.get(arr[i])+1);
            else
                right.put(arr[i],1);
        }

        long ans=0;
        for (int i = 0; i <n ; i++) {
            int k1=0,k2=0;
            if(arr[i]%k==0){
                if(left.containsKey(arr[i]/k))
                    k1=left.get(arr[i]/k);
            }

            if(right.containsKey(arr[i])){
                right.put(arr[i],right.get(arr[i])-1);
            }

            if(right.containsKey(arr[i]*k))
                k2=right.get(arr[i]*k);

            ans=ans+(k1*k2);
            if(left.containsKey(arr[i])){
                left.put(arr[i],left.get(arr[i])+1);
            }
            else
                left.put(arr[i],1);
        }
        System.out.println(ans);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class GeometricProgression {
    public static void main(String args[])throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder output = new StringBuilder();
        String split[] = br.readLine().split(" ");
        int n = Integer.parseInt(split[0]);
        int k = Integer.parseInt(split[1]);
        HashMap<Integer,Integer> right=new HashMap<>();
        HashMap<Integer,Integer> left=new HashMap<>();

        int arr[]=new int[n];
        split=br.readLine().split(" ");
        for (int i = 0; i <n ; i++) {
            arr[i]=Integer.parseInt(split[i]);
            if(right.containsKey(arr[i]))
                right.put(arr[i],right.get(arr[i])+1);
            else
                right.put(arr[i],1);
        }

        long ans=0;
        for (int i = 0; i <n ; i++) {
            int k1=0,k2=0;
            if(arr[i]%k==0){
                if(left.containsKey(arr[i]/k))
                    k1=left.get(arr[i]/k);
            }

            if(right.containsKey(arr[i])){
                right.put(arr[i],right.get(arr[i])-1);
            }

            if(right.containsKey(arr[i]*k))
                k2=right.get(arr[i]*k);

            ans=ans+(k1*k2*1L);
            if(left.containsKey(arr[i])){
                left.put(arr[i],left.get(arr[i])+1);
            }
            else
                left.put(arr[i],1);
        }
        System.out.println(ans);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class GeometricProgression {
    public static void main(String args[])throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder output = new StringBuilder();
        String split[] = br.readLine().split(" ");
        int n = Integer.parseInt(split[0]);
        int k = Integer.parseInt(split[1]);
        HashMap<Long,Long> right=new HashMap<>();
        HashMap<Long,Long> left=new HashMap<>();

        long arr[]=new long[n];
        split=br.readLine().split(" ");
        for (int i = 0; i <n ; i++) {
            arr[i]=Long.parseLong(split[i]);
            if(right.containsKey(arr[i]))
                right.put(arr[i],right.get(arr[i])+1);
            else
                right.put(arr[i],1L);
        }

        long ans=0;
        for (int i = 0; i <n ; i++) {
            long k1=0,k2=0;
            if(arr[i]%k==0){
                if(left.containsKey(arr[i]/k))
                    k1=left.get(arr[i]/k);
            }

            if(right.containsKey(arr[i])){
                right.put(arr[i],right.get(arr[i])-1);
            }

            if(right.containsKey(arr[i]*k))
                k2=right.get(arr[i]*k);

            ans=ans+(k1*k2);
            if(left.containsKey(arr[i])){
                left.put(arr[i],left.get(arr[i])+1);
            }
            else
                left.put(arr[i],1L);
        }
        System.out.println(ans);
    }
}


import java.io.*;
import java.util.*;

public class Main {

	static InputReader in = new InputReader(System.in);
	static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) {

		int n = in.nextInt();
		int k = in.nextInt();

		long[] a = new long[n];
		for(int i=0; i<n; ++i)
			a[i] = in.nextLong();

		long[] b = new long[n];
		TreeMap<Long, Long> cnt = new TreeMap<>();
		for(int i = n-1; i >= 0; --i) {
			long need = a[i] * k;
			long needC = cnt.getOrDefault(need, 0L);
			b[i] = needC;
			long x = cnt.getOrDefault(a[i], 0L);
			cnt.put(a[i], x + 1);
		}

//		for(int bb : b) {
//			System.out.print(bb + " ");
//		}
//		System.out.println();

		long ans = 0;
		TreeMap<Long, Long> av = new TreeMap<>();
		for(int i = n-1; i >= 0; --i) {
			if(av.containsKey(a[i])) {
				ans += av.get(a[i]);
			}
			if(a[i] % k == 0) {
				long prev = a[i] / k;
				av.put(prev, av.getOrDefault(prev, 0L) + b[i]);
			}
		}
		System.out.println(ans);

		out.close();
	}

}

class InputReader {

	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String readString() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}

}


import java.io.*;
import java.util.*;

public class Main {

	static InputReader in = new InputReader(System.in);
	static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) {

		int n = in.nextInt();
		int k = in.nextInt();
		long[] a = new long[n];
		for(int i=0; i<n; ++i)
			a[i] = in.nextLong();

		long[] l = new long[n], r = new long[n];

		HashMap<Long, Long> lc = new HashMap<>();
		HashMap<Long, Long> rc = new HashMap<>();
		for(int i=n-1; i >= 0; --i) {
			long t = a[i] * k;
			r[i] = rc.getOrDefault(t, 0L);
			rc.put(a[i], rc.getOrDefault(a[i], 0L) + 1);
		}
		for(int i=0; i<n; ++i) {
			if(a[i] % k == 0) {
				long t = a[i] / k;
				l[i] = lc.getOrDefault(t, 0L);
			}
			lc.put(a[i], lc.getOrDefault(a[i], 0L) + 1);
		}
		long ans = 0;
		for(int i=0; i<n; ++i) {
			ans += l[i] * r[i];
		}
		System.out.println(ans);

		out.close();
	}

}

class InputReader {

	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String readString() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}

}


import java.io.*;
import java.util.*;

public class Main {

	static InputReader in = new InputReader(System.in);
	static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) {

		int n = in.nextInt();
		int k = in.nextInt();

		int[] a = new int[n];
		for(int i=0; i<n; ++i)
			a[i] = in.nextInt();

		int[] b = new int[n];
		TreeMap<Integer, Integer> cnt = new TreeMap<>();
		for(int i = n-1; i >= 0; --i) {
			int need = a[i] * k;
			int needC = cnt.getOrDefault(need, 0);
			b[i] = needC;
			int x = cnt.getOrDefault(a[i], 0);
			cnt.put(a[i], x + 1);
		}

//		for(int bb : b) {
//			System.out.print(bb + " ");
//		}
//		System.out.println();

		long ans = 0;
		TreeMap<Integer, Integer> av = new TreeMap<>();
		for(int i = n-1; i >= 0; --i) {
			if(av.containsKey(a[i])) {
				ans += av.get(a[i]);
			}
			if(a[i] % k == 0) {
				int prev = a[i] / k;
				av.put(prev, av.getOrDefault(prev, 0) + b[i]);
			}
		}
		System.out.println(ans);

		out.close();
	}

}

class InputReader {

	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String readString() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}

}