import java.util.*;

public class GeometricProgression {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        HashMap<Integer, Integer> right = new HashMap<>();
        HashMap<Integer, Integer> left = new HashMap<>();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            arr[i] = a;
            if (right.containsKey(a)) {
                right.replace(a, right.get(a)+1);
            }
            else {right.put(a, 1);
            }
            left.put(a, 0);
        }
        long sum = 0;
        right.replace(arr[0], right.get(arr[0])-1);
        left.replace(arr[0], 1);
        for (int i = 1; i < n-1; i++) {
            right.replace(arr[i], right.get(arr[i])-1);
            int before = (arr[i] % k == 0 ? arr[i]/k : -17);
            if (before != -17){
                long after = arr[i] * k;
                if (right.containsKey(after) && right.containsKey(before)) {
                    sum += (long)right.get(after) * left.get(before);
                }
            }
            left.replace(arr[i], left.get(arr[i])+1);
        }
        System.out.println(sum);
    }
}


import java.util.*;

public class GeometricProgression {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        HashMap<Integer, Integer> right = new HashMap<>();
        HashMap<Integer, Integer> left = new HashMap<>();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            arr[i] = a;
            if (right.containsKey(a)) {
                right.replace(a, right.get(a)+1);
            }
            else {right.put(a, 1);
            }
            left.put(a, 0);
        }
        long sum = 0;
        right.replace(arr[0], right.get(arr[0])-1);
        left.replace(arr[0], 1);
        for (int i = 1; i < n-1; i++) {
            right.replace(arr[i], right.get(arr[i])-1);
            int before = (arr[i] % k == 0 ? arr[i]/k : -17);
            if (before != -17 && arr[i] * k < 1000000000){
                int after = arr[i] * k;
                if (right.containsKey(after) && right.containsKey(before)) {
                    sum += (long)right.get(after) * left.get(before);
                }
            }
            left.replace(arr[i], left.get(arr[i])+1);
        }
        System.out.println(sum);
    }
}


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

public class Third {

    public static void main(String[] args) throws Exception{
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder("");
        //int t=Integer.parseInt(br.readLine());
    //  while(t-->0)
        //{
        //int n=Integer.parseInt(br.readLine());
        //String l[]=br.readLine().split(" ");
    //  int m=Integer.parseInt(l[0]);
        //char ch=a.charAt();
    //  char c[]=new char[n];
        //int a[]=new int[n];
        //int a[]=new int[n][n];

        //long b[]=new long[n];
        //int min=100000000;
    //  String a=br.readLine();
        //for(int i=0;i<n;i++)
        //{
            //a[i]=Integer.parseInt(l[i]);
        //}
        String l[]=br.readLine().split(" ");
        int n=Integer.parseInt(l[0]);
        long k=Long.parseLong(l[1]);
        long a[]=new long[n];
        ArrayList<Long>ar=new ArrayList<Long>();
        l=br.readLine().split(" ");

        long max=0;
   for(int i=0;i<n;i++)
   {

       long x=Long.parseLong(l[i]);
       a[i]=x;
       ar.add(x);

   }
   HashSet<Long>hs=new HashSet<Long>();
  // Arrays.sort(a);
   for(int i=0;i<n;i++)
   {
       if(k!=1)
       {
       long d=0,d1=0;;
       long h=k*a[i];
       long e=h*k;

       long g1=0;
       long g2=0;

       int f=0;
       for(int j=i+1;j<n;j++)
        {
           if(a[j]==h)
            {
                g1++;
            }
            else if(a[j]==e)
            {
                if(g1!=0)
                {

                d1+=g1;
        }
        }
            else
                continue;
        }
       max+=d1;
       }

       else
       {
           if(!hs.contains(a[i]))
           {
           long d=0;
           long g1=1,g2=0;
           for(int j=i+1;j<n;j++)
            {
                if(a[j]==a[i])
                {
                    g1++;
                }
            d+=(g1*(g1-1)*(g1-2));
            d=d/6;
            hs.add(a[i]);
            }
           max+=d;
           }
       }
   }
           System.out.println(max);
        }
}


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

public class Third {

    public static void main(String[] args) throws Exception{
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder("");
        //int t=Integer.parseInt(br.readLine());
    //  while(t-->0)
        //{
        //int n=Integer.parseInt(br.readLine());
        //String l[]=br.readLine().split(" ");
    //  int m=Integer.parseInt(l[0]);
        //char ch=a.charAt();
    //  char c[]=new char[n];
        //int a[]=new int[n];
        //int a[]=new int[n][n];

        //long b[]=new long[n];
        //int min=100000000;
    //  String a=br.readLine();
        //for(int i=0;i<n;i++)
        //{
            //a[i]=Integer.parseInt(l[i]);
        //}
        String l[]=br.readLine().split(" ");
        int n=Integer.parseInt(l[0]);
        long k=Long.parseLong(l[1]);
        long a[]=new long[n];
        ArrayList<Long>ar=new ArrayList<Long>();
        l=br.readLine().split(" ");

        long max=0;
         HashMap<Long,Integer>hm=new HashMap<Long,Integer>();
   for(int i=0;i<n;i++)
   {

       long x=Long.parseLong(l[i]);
       a[i]=x;

         if(i>0)
         {
         int y=1;
         if(hm.get(x)!=null)
         {
             y=hm.get(x);
             ++y;
             hm.put(x, y);
         }
         else
             hm.put(x,y);

         }

   }
   HashMap<Long,Integer>hm1=new HashMap<Long,Integer>();
  // Arrays.sort(a);
   hm1.put(a[0], 1);

   for(int i=1;i<n-1;i++)
   {
       if(a[i]%k==0)
       {
      long x=a[i]/k;
      long z=a[i]*k;
      long c1=0,c2=0;
     int y=1;
     if(hm.get(a[i])>1)
         hm.put(a[i], hm.get(a[i])-1);
     else
         hm.remove(a[i]);
     if(hm1.get(x)!=null)
     {
         c1=hm1.get(x);
         if(hm.get(z)!=null)
         {
             c2=hm.get(z);
            /* if(k==1)
             {
             if(c2>1)
                 hm.put(z, (int)--c2);
             else
                 hm.remove(z);
             }
        */

             max+=c1*c2;

         }

     }
       }

     int f=1;
     if(hm1.get(a[i])!=null)
     {
         f=hm1.get(a[i]);
         ++f;
         hm1.put(a[i], f);
     }
     else
         hm1.put(a[i],f);
   }
           System.out.println(max);
        }
}


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

public class q3 {
public static void main(String[] args) throws Exception
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    PrintWriter pw=new PrintWriter(System.out);
    StringTokenizer st=new StringTokenizer(br.readLine());
    int n=ip(st.nextToken());
    long k=ip(st.nextToken());
    HashMap<Long,Integer> hs=new HashMap<Long,Integer>();
    StringTokenizer st2=new StringTokenizer(br.readLine());
    long[] arr=new long[n];
    arr[0]=lp(st2.nextToken());
    for(int i=1;i<n;i++)
    {
        long tt=lp(st2.nextToken());
        arr[i]=tt;
        if(hs.containsKey(tt))
        {
            int cnt=hs.get(tt);
            hs.put(tt, cnt+1);
        }
        else
        {
            hs.put(tt,1);
        }
    }
    HashMap<Long,Integer> left=new HashMap<Long,Integer>();
    left.put(arr[0],1);
    long ans=0;
    for(int i=1;i<n-1;i++)
    {
        int cnt=hs.get(arr[i]);
        if(cnt==1)
        {
            hs.remove(arr[i]);
        }
        else
        {
            hs.put(arr[i], cnt-1);
        }
        long tt=Math.abs(arr[i]);
        if(tt%k==0)
        {
            int ll=0;
            if(left.containsKey(arr[i]/k))
            {
                ll=left.get(arr[i]/k);
            }
            int rr=0;
            if(hs.containsKey(arr[i]*k))
            {
                rr=hs.get(arr[i]*k);
            }
            //System.out.println("for i"+i+" left is"+ll+"  right is"+rr);
            ans+=rr*1l*ll;
        }
        if(left.containsKey(arr[i]))
        {
            int ccn=left.get(arr[i]);
            left.put(arr[i], ccn+1);
        }
        else
        {
            left.put(arr[i], 1);
        }
    }
    System.out.println(ans);

}
public static int  ip(String s)
{
    return Integer.parseInt(s);
}
public static long lp(String s)
{
    return Long.parseLong(s);
}
}


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

public class Third {

    public static void main(String[] args) throws Exception{
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder("");
        //int t=Integer.parseInt(br.readLine());
    //  while(t-->0)
        //{
        //int n=Integer.parseInt(br.readLine());
        //String l[]=br.readLine().split(" ");
    //  int m=Integer.parseInt(l[0]);
        //char ch=a.charAt();
    //  char c[]=new char[n];
        //int a[]=new int[n];
        //int a[]=new int[n][n];

        //long b[]=new long[n];
        //int min=100000000;
    //  String a=br.readLine();
        //for(int i=0;i<n;i++)
        //{
            //a[i]=Integer.parseInt(l[i]);
        //}
        String l[]=br.readLine().split(" ");
        int n=Integer.parseInt(l[0]);
        long k=Long.parseLong(l[1]);
        long a[]=new long[n];
        ArrayList<Long>ar=new ArrayList<Long>();
        l=br.readLine().split(" ");

        long max=0;
         HashMap<Long,Integer>hm=new HashMap<Long,Integer>();
   for(int i=0;i<n;i++)
   {

       long x=Long.parseLong(l[i]);
       a[i]=x;

         if(i>0)
         {
         int y=1;
         if(hm.get(x)!=null)
         {
             y=hm.get(x);
             ++y;
             hm.put(x, y);
         }
         else
             hm.put(x,y);

         }

   }
   HashMap<Long,Integer>hm1=new HashMap<Long,Integer>();
  // Arrays.sort(a);
   hm1.put(a[0], 1);

   for(int i=1;i<n-1;i++)
   {
      long x=a[i]/k;
      long z=a[i]*k;
      long c1=0,c2=0;
     int y=1;
     if(hm.get(a[i])>1)
         hm.put(a[i], hm.get(a[i])-1);
     else
         hm.remove(a[i]);
     if(hm1.get(x)!=null)
     {
         c1=hm1.get(x);
         if(hm.get(z)!=null)
         {
             c2=hm.get(z);
            /* if(k==1)
             {
             if(c2>1)
                 hm.put(z, (int)--c2);
             else
                 hm.remove(z);
             }
        */

             max+=c1*c2;

         }

     }

     int f=1;
     if(hm1.get(a[i])!=null)
     {
         f=hm1.get(a[i]);
         ++f;
         hm1.put(a[i], f);
     }
     else
         hm1.put(a[i],f);
   }
           System.out.println(max);
        }
}


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

public class Third {

    public static void main(String[] args) throws Exception{
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder("");
        String l[]=br.readLine().split(" ");
        int n=Integer.parseInt(l[0]);
        long k=Long.parseLong(l[1]);
        long a[]=new long[n];

        l=br.readLine().split(" ");

        long max=0;
         HashMap<Long,Integer>hm=new HashMap<Long,Integer>();
   for(int i=0;i<n;i++)
   {

       long x=Long.parseLong(l[i]);
       a[i]=x;

         if(i>0)
         {
         int y=1;
         if(hm.get(x)!=null)
         {
             y=hm.get(x);
             ++y;
             hm.put(x, y);
         }
         else
             hm.put(x,y);

         }

   }
   HashMap<Long,Integer>hm1=new HashMap<Long,Integer>();
   hm1.put(a[0], 1);

   for(int i=1;i<n-1;i++)
   {if(hm.get(a[i])>1)
         hm.put(a[i], hm.get(a[i])-1);
     else
         hm.remove(a[i]);
       if(a[i]%k==0)
       {
      long x=a[i]/k;
      long z=a[i]*k;
      long c1=0,c2=0;
     int y=1;

     if(hm1.get(x)!=null)
     {
         c1=hm1.get(x);
         if(hm.get(z)!=null)
         {
             c2=hm.get(z);
             max+=c1*c2;

         }

     }
   }

     int f=1;
     if(hm1.get(a[i])!=null)
     {
         f=hm1.get(a[i]);
         ++f;
         hm1.put(a[i], f);
     }
     else
         hm1.put(a[i],f);
   }
           System.out.println(max);
        }
}


import java.util.*;

public class C {

    private static final long MAX = 1000000000;

    private static class Point implements Comparable<Point> {
        private final int x;
        private final long value;

        public Point(int x, long value) {
            this.x = x;
            this.value = value;
        }

        public int getX() {
            return x;
        }

        public long getValue() {
            return value;
        }

        @Override
        public int compareTo(Point o) {
            return x - o.x;
        }
    }

    private static class Solver {

        private final int k;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.seq = seq;
        }

        public long getCombinationsCount() {
            if (seq.size() < 3) {
                return 0;
            }
            Map<Integer, List<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getBaseCandidates(itemPositions.keySet());
            long result = 0;
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, List<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                if (n < 3) {
                    return 0;
                }
                return n * (n - 1) * (n - 2) / 6;
            }
            List<Point> secondPoints = new ArrayList<>();
            long secondCounter = 0;
            List<Integer> secondPositions = itemPositions.get(starter * k * k);
            for (int i = secondPositions.size() - 1; i >= 0; i--) {
                secondPoints.add(new Point(secondPositions.get(i), ++secondCounter));
            }
            Collections.reverse(secondPoints);

            List<Point> firstPoints = new ArrayList<>();
            long firstCounter = 0;
            List<Integer> firstPositions = itemPositions.get(starter * k);
            for (int i = firstPositions.size() - 1; i >= 0; i--) {
                int nextSecondIndex = firstIndexGreater(secondPoints, firstPositions.get(i));
                if (nextSecondIndex < secondPoints.size()) {
                    firstCounter += secondPoints.get(nextSecondIndex).getValue();
                }
                firstPoints.add(new Point(firstPositions.get(i), firstCounter));
            }
            Collections.reverse(firstPoints);

            List<Point> zeroPoints = new ArrayList<>();
            long zeroCounter = 0;
            List<Integer> zeroPositions = itemPositions.get(starter);
            for (int i = zeroPositions.size() - 1; i >= 0; i--) {
                int nextFirstIndex = firstIndexGreater(firstPoints, zeroPositions.get(i));
                if (nextFirstIndex < firstPoints.size()) {
                    zeroCounter += firstPoints.get(nextFirstIndex).getValue();
                }
                zeroPoints.add(new Point(zeroPositions.get(i), zeroCounter));
            }
            Collections.reverse(zeroPoints);
            return zeroPoints.get(0).getValue();
        }

        int firstIndexGreater(List<Point> list, int value) {
            int searchResult = Collections.binarySearch(list, new Point(value, 0));
            return -(searchResult + 1);
        }

        private Map<Integer, List<Integer>> seqToItemPositions() {
            Map<Integer, List<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new ArrayList<>());
                }
                result.get(val).add(i);
            }
            result.values().forEach(Collections::sort);
            return result;
        }

        private List<Integer> getBaseCandidates(Set<Integer> items) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : items) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (first > MAX || second > MAX) {
                    continue;
                }
                if (!items.contains(candidate * k) || !items.contains(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;
import java.util.stream.Collectors;

public class C {

    private static final long MAX = 1000000000;

    private static class Point implements Comparable<Point> {
        private final int position;
        private final long value;

        public Point(int position, long value) {
            this.position = position;
            this.value = value;
        }

        public int getPosition() {
            return position;
        }

        public long getValue() {
            return value;
        }

        @Override
        public int compareTo(Point o) {
            return position - o.position;
        }
    }

    private static class Solver {

        private final int k;
        private final long zeroCount;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.zeroCount = seq.stream().filter(x -> x == 0).count();
            this.seq = seq.stream().filter(x -> x != 0).collect(Collectors.toList());
        }

        public long getCombinationsCount() {
            long result = c3(zeroCount);
            if (seq.size() < 3) {
                return result;
            }
            Map<Integer, List<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getBaseCandidates(itemPositions.keySet());
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, List<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                return c3(n);
            }

            List<Point> secondPoints = getSecondPoints(itemPositions.get(starter * k * k));
            List<Point> firstPoints = getFirstOrZeroPoints(itemPositions.get(starter * k), secondPoints);
            List<Point> zeroPoints = getFirstOrZeroPoints(itemPositions.get(starter), firstPoints);

            return zeroPoints.get(0).getValue();
        }

        private List<Point> getSecondPoints(List<Integer> positions) {
            List<Point> result = new ArrayList<>();
            long counter = 0;
            for (int i = positions.size() - 1; i >= 0; i--) {
                result.add(new Point(positions.get(i), ++counter));
            }
            Collections.reverse(result);
            return result;
        }

        private List<Point> getFirstOrZeroPoints(List<Integer> positions, List<Point> previousPoints) {
            List<Point> result = new ArrayList<>();
            long counter = 0;
            for (int i = positions.size() - 1; i >= 0; i--) {
                int nextPointInPrevious = firstIndexGreater(previousPoints, positions.get(i));
                if (nextPointInPrevious < previousPoints.size()) {
                    counter += previousPoints.get(nextPointInPrevious).getValue();
                }
                previousPoints.add(new Point(positions.get(i), counter));
            }
            Collections.reverse(result);
            return result;
        }

        private long c3(long n) {
            return n < 3 ? 0 : (n - 2) * (n - 1) * n / 6;
        }

        private int firstIndexGreater(List<Point> list, int value) {
            int searchResult = Collections.binarySearch(list, new Point(value, 0));
            return -(searchResult + 1);
        }

        private Map<Integer, List<Integer>> seqToItemPositions() {
            Map<Integer, List<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new ArrayList<>());
                }
                result.get(val).add(i);
            }
            result.values().forEach(Collections::sort);
            return result;
        }

        private List<Integer> getBaseCandidates(Set<Integer> items) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : items) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (Math.abs(first) > MAX || Math.abs(second) > MAX) {
                    continue;
                }
                if (!items.contains(candidate * k) || !items.contains(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;
import java.util.stream.Collectors;

public class C {

    private static final long MAX = 1000000000;

    private static class Point implements Comparable<Point> {
        private final int x;
        private final long value;

        public Point(int x, long value) {
            this.x = x;
            this.value = value;
        }

        public int getX() {
            return x;
        }

        public long getValue() {
            return value;
        }

        @Override
        public int compareTo(Point o) {
            return x - o.x;
        }
    }

    private static class Solver {

        private final int k;
        private final long zeroCount;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.zeroCount = seq.stream().filter(x -> x == 0).count();
            this.seq = seq.stream().filter(x -> x != 0).collect(Collectors.toList());
        }

        public long getCombinationsCount() {
            long result = zeroCount >=3 ? zeroCount * (zeroCount - 1) * (zeroCount - 2) / 6 : 0;
            if (seq.size() < 3) {
                return result;
            }
            Map<Integer, List<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getBaseCandidates(itemPositions.keySet());
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, List<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                if (n < 3) {
                    return 0;
                }
                return n * (n - 1) * (n - 2) / 6;
            }
            List<Point> secondPoints = new ArrayList<>();
            long secondCounter = 0;
            List<Integer> secondPositions = itemPositions.get(starter * k * k);
            for (int i = secondPositions.size() - 1; i >= 0; i--) {
                secondPoints.add(new Point(secondPositions.get(i), ++secondCounter));
            }
            Collections.reverse(secondPoints);

            List<Point> firstPoints = new ArrayList<>();
            long firstCounter = 0;
            List<Integer> firstPositions = itemPositions.get(starter * k);
            for (int i = firstPositions.size() - 1; i >= 0; i--) {
                int nextSecondIndex = firstIndexGreater(secondPoints, firstPositions.get(i));
                if (nextSecondIndex < secondPoints.size()) {
                    firstCounter += secondPoints.get(nextSecondIndex).getValue();
                }
                firstPoints.add(new Point(firstPositions.get(i), firstCounter));
            }
            Collections.reverse(firstPoints);

            List<Point> zeroPoints = new ArrayList<>();
            long zeroCounter = 0;
            List<Integer> zeroPositions = itemPositions.get(starter);
            for (int i = zeroPositions.size() - 1; i >= 0; i--) {
                int nextFirstIndex = firstIndexGreater(firstPoints, zeroPositions.get(i));
                if (nextFirstIndex < firstPoints.size()) {
                    zeroCounter += firstPoints.get(nextFirstIndex).getValue();
                }
                zeroPoints.add(new Point(zeroPositions.get(i), zeroCounter));
            }
            Collections.reverse(zeroPoints);
            return zeroPoints.get(0).getValue();
        }

        int firstIndexGreater(List<Point> list, int value) {
            int searchResult = Collections.binarySearch(list, new Point(value, 0));
            return -(searchResult + 1);
        }

        private Map<Integer, List<Integer>> seqToItemPositions() {
            Map<Integer, List<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new ArrayList<>());
                }
                result.get(val).add(i);
            }
            result.values().forEach(Collections::sort);
            return result;
        }

        private List<Integer> getBaseCandidates(Set<Integer> items) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : items) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (Math.abs(first) > MAX || Math.abs(second) > MAX) {
                    continue;
                }
                if (!items.contains(candidate * k) || !items.contains(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;

public class C {

    private static final long MAX = 1000000000;

    private static class Solver {

        private final int k;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.seq = seq;
        }

        public long getCombinationsCount() {
            Map<Integer, Set<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getBaseCandidates(itemPositions);
            long result = 0;
            long result0 = 0;
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
                result0 += getCombinationsCountForStarter0(starter, itemPositions);
            }
//            if (result != result0) {
//                System.out.println("wrong");
//            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, Set<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                if (n < 3) {
                    return 0;
                }
                return n * (n - 1) * (n - 2) / 6;
            }
            long[] secondCounts = new long[seq.size()];
            long secondCounter = 0;
            Set<Integer> secondPositions = itemPositions.get(starter * k * k);
            for (int i = secondCounts.length - 1; i >= 0; i--) {
                if (secondPositions.contains(i)) {
                    secondCounter += 1;
                }
                secondCounts[i] = secondCounter;
            }

            long[] firstCounts = new long[seq.size()];
            long firstCounter = 0;
            Set<Integer> firstPositions = itemPositions.get(starter * k);
            for (int i = firstCounts.length - 1; i >= 0; i--) {
                if (firstPositions.contains(i)) {
                    firstCounter += secondCounts[i];
                }
                firstCounts[i] = firstCounter;
            }

            long [] zeroCounts = new long[seq.size()];
            long zeroCounter = 0;
            Set<Integer> zeroPositions = itemPositions.get(starter);
            for (int i = zeroCounts.length - 1; i >= 0; i--) {
                if (zeroPositions.contains(i)) {
                    zeroCounter += firstCounts[i];
                }
                zeroCounts[i] = zeroCounter;
            }
            return zeroCounts[0];
        }

        private int getCombinationsCountForStarter0(Integer starter, Map<Integer, Set<Integer>> itemPositions) {
            List<Integer> zeroPositions = new ArrayList<>(itemPositions.get(starter));
            List<Integer> firstPositions = new ArrayList<>(itemPositions.get(starter * k));
            List<Integer> secondPositions = new ArrayList<>(itemPositions.get(starter * k * k));
            Collections.sort(zeroPositions);
            Collections.sort(firstPositions);
            Collections.sort(secondPositions);
            int result = 0;
            for (int zeroIndex = 0; zeroIndex < zeroPositions.size(); zeroIndex++) {
                int zeroPosition = zeroPositions.get(zeroIndex);
                int firstStart = k != 1 ? firstIndexGreater(firstPositions, zeroPosition) : zeroIndex + 1;
                for (int firstIndex = firstStart; firstIndex < firstPositions.size(); firstIndex++) {
                    int firstPosition = firstPositions.get(firstIndex);
                    int secondStart = k != 1 ? firstIndexGreater(secondPositions, firstPosition) : firstIndex + 1;
                    int secondCounts = secondPositions.size() - secondStart;
                    if (secondCounts > 0) {
                        result += secondCounts;
                    }
                }
            }
            return result;
        }

        int firstIndexGreater(List<Integer> list, int value) {
            int searchResult = Collections.binarySearch(list, value);
            return searchResult >= 0 ? searchResult +1 : -(searchResult + 1);
        }

        private Map<Integer, Set<Integer>> seqToItemPositions() {
            Map<Integer, Set<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new HashSet<>());
                }
                result.get(val).add(i);
            }
            return result;
        }

        private List<Integer> getBaseCandidates(Map<Integer, Set<Integer>> itemPositions) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : itemPositions.keySet()) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (first > MAX || second > MAX) {
                    continue;
                }
                if (!itemPositions.containsKey(candidate * k) || !itemPositions.containsKey(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
//        Random r = new Random();
//        for (int i = 0; i < 100; i++) {
//            int[] items = new int[] {1, 2, 4};
//            List<Integer> seq = new ArrayList<>();
//            int size = 1000;
//            for (int j = 0; j < size; j++) {
//                seq.add(items[r.nextInt(items.length)]);
//            }
//            new Solver(2, seq).getCombinationsCount();
//        }
//        System.out.println("success");

        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;

public class C {

    private static final long MAX = 200000;

    private static class Solver {

        private final int k;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.seq = seq;
        }

        public int getCombinationsCount() {
            Map<Integer, Set<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getBaseCandidates(itemPositions);
            int result = 0;
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private int getCombinationsCountForStarter(Integer starter, Map<Integer, Set<Integer>> itemPositions) {
            if (k == 1) {
                int n = itemPositions.get(starter).size();
                if (n < 3) {
                    return 0;
                }
                return n * (n - 1) * (n - 2) / 6;
            }
            int[] secondCounts = new int[seq.size()];
            int secondCounter = 0;
            Set<Integer> secondPositions = itemPositions.get(starter * k * k);
            for (int i = secondCounts.length - 1; i >= 0; i--) {
                if (secondPositions.contains(i)) {
                    secondCounter += 1;
                }
                secondCounts[i] = secondCounter;
            }

            int[] firstCounts = new int[seq.size()];
            int firstCounter = 0;
            Set<Integer> firstPositions = itemPositions.get(starter * k);
            for (int i = firstCounts.length - 1; i >= 0; i--) {
                if (firstPositions.contains(i)) {
                    firstCounter += secondCounts[i];
                }
                firstCounts[i] = firstCounter;
            }

            int [] zeroCounts = new int[seq.size()];
            int zeroCounter = 0;
            Set<Integer> zeroPositions = itemPositions.get(starter);
            for (int i = zeroCounts.length - 1; i >= 0; i--) {
                if (zeroPositions.contains(i)) {
                    zeroCounter += firstCounts[i];
                }
                zeroCounts[i] = zeroCounter;
            }
            return zeroCounts[0];
        }

        private Map<Integer, Set<Integer>> seqToItemPositions() {
            Map<Integer, Set<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new HashSet<>());
                }
                result.get(val).add(i);
            }
            return result;
        }

        private List<Integer> getBaseCandidates(Map<Integer, Set<Integer>> itemPositions) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : itemPositions.keySet()) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (first > MAX || second > MAX) {
                    continue;
                }
                if (!itemPositions.containsKey(candidate * k) || !itemPositions.containsKey(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;
import java.util.stream.IntStream;

public class C {

    private static final long MAX = 1000000000;

    private static class Solver {

        private final int k;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.seq = seq;
        }

        public long getCombinationsCount() {
            Map<Integer, int[]> itemPositions = seqToItemPositions();
            List<Integer> starters = getBaseCandidates(itemPositions);
            long result = 0;
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, int[]> itemPositions) {
            if (k == 1) {
                long n = IntStream.of(itemPositions.get(starter)).sum();
                if (n < 3) {
                    return 0;
                }
                return n * (n - 1) * (n - 2) / 6;
            }
            long[] secondCounts = new long[seq.size()];
            long secondCounter = 0;
            int[] secondPositions = itemPositions.get(starter * k * k);
            for (int i = secondCounts.length - 1; i >= 0; i--) {
                if (secondPositions[i] != 0) {
                    secondCounter += 1;
                }
                secondCounts[i] = secondCounter;
            }

            long[] firstCounts = new long[seq.size()];
            long firstCounter = 0;
            int[] firstPositions = itemPositions.get(starter * k);
            for (int i = firstCounts.length - 1; i >= 0; i--) {
                if (firstPositions[i] != 0) {
                    firstCounter += secondCounts[i];
                }
                firstCounts[i] = firstCounter;
            }

            long [] zeroCounts = new long[seq.size()];
            long zeroCounter = 0;
            int[] zeroPositions = itemPositions.get(starter);
            for (int i = zeroCounts.length - 1; i >= 0; i--) {
                if (zeroPositions[i] != 0) {
                    zeroCounter += firstCounts[i];
                }
                zeroCounts[i] = zeroCounter;
            }
            return zeroCounts[0];
        }

        private Map<Integer, int[]> seqToItemPositions() {
            Map<Integer, int[]> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new int[seq.size()]);
                }
                result.get(val)[i] = 1;
            }
            return result;
        }

        private List<Integer> getBaseCandidates(Map<Integer, int[]> itemPositions) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : itemPositions.keySet()) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (first > MAX || second > MAX) {
                    continue;
                }
                if (!itemPositions.containsKey(candidate * k) || !itemPositions.containsKey(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;

public class C {

    private static final long MAX = 200000;

    private static class Solver {

        private final int k;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.seq = seq;
        }

        public long getCombinationsCount() {
            Map<Integer, Set<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getBaseCandidates(itemPositions);
            long result = 0;
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, Set<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                if (n < 3) {
                    return 0;
                }
                return n * (n - 1) * (n - 2) / 6;
            }
            long[] secondCounts = new long[seq.size()];
            long secondCounter = 0;
            Set<Integer> secondPositions = itemPositions.get(starter * k * k);
            for (int i = secondCounts.length - 1; i >= 0; i--) {
                if (secondPositions.contains(i)) {
                    secondCounter += 1;
                }
                secondCounts[i] = secondCounter;
            }

            long[] firstCounts = new long[seq.size()];
            long firstCounter = 0;
            Set<Integer> firstPositions = itemPositions.get(starter * k);
            for (int i = firstCounts.length - 1; i >= 0; i--) {
                if (firstPositions.contains(i)) {
                    firstCounter += secondCounts[i];
                }
                firstCounts[i] = firstCounter;
            }

            long [] zeroCounts = new long[seq.size()];
            long zeroCounter = 0;
            Set<Integer> zeroPositions = itemPositions.get(starter);
            for (int i = zeroCounts.length - 1; i >= 0; i--) {
                if (zeroPositions.contains(i)) {
                    zeroCounter += firstCounts[i];
                }
                zeroCounts[i] = zeroCounter;
            }
            return zeroCounts[0];
        }

        private int getCombinationsCountForStarter0(Integer starter, Map<Integer, Set<Integer>> itemPositions) {
            List<Integer> zeroPositions = new ArrayList<>(itemPositions.get(starter));
            List<Integer> firstPositions = new ArrayList<>(itemPositions.get(starter * k));
            List<Integer> secondPositions = new ArrayList<>(itemPositions.get(starter * k * k));
            Collections.sort(zeroPositions);
            Collections.sort(firstPositions);
            Collections.sort(secondPositions);
            int result = 0;
            for (int zeroIndex = 0; zeroIndex < zeroPositions.size(); zeroIndex++) {
                int zeroPosition = zeroPositions.get(zeroIndex);
                int firstStart = k != 1 ? firstIndexGreater(firstPositions, zeroPosition) : zeroIndex + 1;
                for (int firstIndex = firstStart; firstIndex < firstPositions.size(); firstIndex++) {
                    int firstPosition = firstPositions.get(firstIndex);
                    int secondStart = k != 1 ? firstIndexGreater(secondPositions, firstPosition) : firstIndex + 1;
                    int secondCounts = secondPositions.size() - secondStart;
                    if (secondCounts > 0) {
                        result += secondCounts;
                    }
                }
            }
            return result;
        }

        int firstIndexGreater(List<Integer> list, int value) {
            int searchResult = Collections.binarySearch(list, value);
            return searchResult >= 0 ? searchResult +1 : -(searchResult + 1);
        }

        private Map<Integer, Set<Integer>> seqToItemPositions() {
            Map<Integer, Set<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new HashSet<>());
                }
                result.get(val).add(i);
            }
            return result;
        }

        private List<Integer> getBaseCandidates(Map<Integer, Set<Integer>> itemPositions) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : itemPositions.keySet()) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (first > MAX || second > MAX) {
                    continue;
                }
                if (!itemPositions.containsKey(candidate * k) || !itemPositions.containsKey(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
//        Random r = new Random();
//        for (int i = 0; i < 100; i++) {
//            int[] items = new int[] {1, 2, 4};
//            List<Integer> seq = new ArrayList<>();
//            int size = 100;
//            for (int j = 0; j < size; j++) {
//                seq.add(items[r.nextInt(items.length)]);
//            }
//            new Solver(2, seq).getCombinationsCount();
//        }
//        System.out.println("success");

        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;
import java.util.stream.Collectors;

public class C {

    private static final long MAX = 1000000000;

    private static class Point implements Comparable<Point> {
        private final int position;
        private final long value;

        public Point(int position, long value) {
            this.position = position;
            this.value = value;
        }

        public int getPosition() {
            return position;
        }

        public long getValue() {
            return value;
        }

        @Override
        public int compareTo(Point o) {
            return position - o.position;
        }
    }

    private static class Solver {

        private final int k;
        private final long zeroCount;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.zeroCount = seq.stream().filter(x -> x == 0).count();
            this.seq = seq.stream().filter(x -> x != 0).collect(Collectors.toList());
        }

        public long getCombinationsCount() {
            long result = c3(zeroCount);
            if (seq.size() < 3) {
                return result;
            }
            Map<Integer, List<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getStarters(itemPositions.keySet());
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, List<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                return c3(n);
            }

            List<Point> secondPoints = getSecondPoints(itemPositions.get(starter * k * k));
            List<Point> firstPoints = getFirstOrZeroPoints(itemPositions.get(starter * k), secondPoints);
            List<Point> zeroPoints = getFirstOrZeroPoints(itemPositions.get(starter), firstPoints);

            return zeroPoints.get(0).getValue();
        }

        private List<Point> getSecondPoints(List<Integer> positions) {
            List<Point> result = new ArrayList<>();
            long counter = 0;
            for (int i = positions.size() - 1; i >= 0; i--) {
                result.add(new Point(positions.get(i), ++counter));
            }
            Collections.reverse(result);
            return result;
        }

        private List<Point> getFirstOrZeroPoints(List<Integer> positions, List<Point> previousPoints) {
            List<Point> result = new ArrayList<>();
            long counter = 0;
            for (int i = positions.size() - 1; i >= 0; i--) {
                int nextPointInPrevious = firstIndexGreater(previousPoints, positions.get(i));
                if (nextPointInPrevious < previousPoints.size()) {
                    counter += previousPoints.get(nextPointInPrevious).getValue();
                }
                result.add(new Point(positions.get(i), counter));
            }
            Collections.reverse(result);
            return result;
        }

        private long c3(long n) {
            return n < 3 ? 0 : (n - 2) * (n - 1) * n / 6;
        }

        private int firstIndexGreater(List<Point> list, int value) {
            int searchResult = Collections.binarySearch(list, new Point(value, 0));
            return -(searchResult + 1);
        }

        private Map<Integer, List<Integer>> seqToItemPositions() {
            Map<Integer, List<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new ArrayList<>());
                }
                result.get(val).add(i);
            }
            result.values().forEach(Collections::sort);
            return result;
        }

        private List<Integer> getStarters(Set<Integer> items) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : items) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (Math.abs(first) > MAX || Math.abs(second) > MAX) {
                    continue;
                }
                if (!items.contains(candidate * k) || !items.contains(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;

public class C {

    private static final long MAX = 1000000000;

    private static class Point implements Comparable<Point> {
        private final int x;
        private final long value;

        public Point(int x, long value) {
            this.x = x;
            this.value = value;
        }

        public int getX() {
            return x;
        }

        public long getValue() {
            return value;
        }

        @Override
        public int compareTo(Point o) {
            return x - o.x;
        }
    }

    private static class Solver {

        private final int k;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.seq = seq;
        }

        public long getCombinationsCount() {
            Map<Integer, List<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getBaseCandidates(itemPositions.keySet());
            long result = 0;
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, List<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                if (n < 3) {
                    return 0;
                }
                return n * (n - 1) * (n - 2) / 6;
            }
            List<Point> secondPoints = new ArrayList<>();
            long secondCounter = 0;
            List<Integer> secondPositions = itemPositions.get(starter * k * k);
            for (int i = secondPositions.size() - 1; i >= 0; i--) {
                secondPoints.add(new Point(secondPositions.get(i), ++secondCounter));
            }
            Collections.reverse(secondPoints);

            List<Point> firstPoints = new ArrayList<>();
            long firstCounter = 0;
            List<Integer> firstPositions = itemPositions.get(starter * k);
            for (int i = firstPositions.size() - 1; i >= 0; i--) {
                int nextSecondIndex = firstIndexGreater(secondPoints, firstPositions.get(i));
                if (nextSecondIndex < secondPoints.size()) {
                    firstCounter += secondPoints.get(nextSecondIndex).getValue();
                }
                firstPoints.add(new Point(firstPositions.get(i), firstCounter));
            }
            Collections.reverse(firstPoints);

            List<Point> zeroPoints = new ArrayList<>();
            long zeroCounter = 0;
            List<Integer> zeroPositions = itemPositions.get(starter);
            for (int i = zeroPositions.size() - 1; i >= 0; i--) {
                int nextFirstIndex = firstIndexGreater(firstPoints, zeroPositions.get(i));
                if (nextFirstIndex < firstPoints.size()) {
                    zeroCounter += firstPoints.get(nextFirstIndex).getValue();
                }
                zeroPoints.add(new Point(zeroPositions.get(i), zeroCounter));
            }
            Collections.reverse(zeroPoints);
            return zeroPoints.get(0).getValue();
        }

        int firstIndexGreater(List<Point> list, int value) {
            int searchResult = Collections.binarySearch(list, new Point(value, 0));
            return -(searchResult + 1);
        }

        private Map<Integer, List<Integer>> seqToItemPositions() {
            Map<Integer, List<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new ArrayList<>());
                }
                result.get(val).add(i);
            }
            result.values().forEach(Collections::sort);
            return result;
        }

        private List<Integer> getBaseCandidates(Set<Integer> items) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : items) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (first > MAX || second > MAX) {
                    continue;
                }
                if (!items.contains(candidate * k) || !items.contains(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;
import java.util.stream.Collectors;

public class C {

    private static final long MAX = 1000000000;

    private static class Point implements Comparable<Point> {
        private final int position;
        private final long value;

        public Point(int position, long value) {
            this.position = position;
            this.value = value;
        }

        public int getPosition() {
            return position;
        }

        public long getValue() {
            return value;
        }

        @Override
        public int compareTo(Point o) {
            return position - o.position;
        }
    }

    private static class Solver {

        private final int k;
        private final long zeroCount;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.zeroCount = seq.stream().filter(x -> x == 0).count();
            this.seq = seq.stream().filter(x -> x != 0).collect(Collectors.toList());
        }

        public long getCombinationsCount() {
            long result = c3(zeroCount);
            if (seq.size() < 3) {
                return result;
            }
            Map<Integer, List<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getStarters(itemPositions.keySet());
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, List<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                return c3(n);
            }

            List<Point> secondPoints = getSecondPoints(itemPositions.get(starter * k * k));
            List<Point> firstPoints = getFirstOrZeroPoints(itemPositions.get(starter * k), secondPoints);
            List<Point> zeroPoints = getFirstOrZeroPoints(itemPositions.get(starter), firstPoints);

            return zeroPoints.isEmpty() ? 0 : zeroPoints.get(0).getValue();
        }

        private List<Point> getSecondPoints(List<Integer> positions) {
            List<Point> result = new ArrayList<>();
            long counter = 0;
            for (int i = positions.size() - 1; i >= 0; i--) {
                result.add(new Point(positions.get(i), ++counter));
            }
            Collections.reverse(result);
            return result;
        }

        private List<Point> getFirstOrZeroPoints(List<Integer> positions, List<Point> previousPoints) {
            List<Point> result = new ArrayList<>();
            long counter = 0;
            for (int i = positions.size() - 1; i >= 0; i--) {
                int searchResult = Collections.binarySearch(previousPoints, new Point(positions.get(i), 0));
                int nextPointInPrevious = -(searchResult + 1);
                if (nextPointInPrevious < previousPoints.size()) {
                    counter += previousPoints.get(nextPointInPrevious).getValue();
                    result.add(new Point(positions.get(i), counter));
                }
            }
            Collections.reverse(result);
            return result;
        }

        private long c3(long n) {
            return n < 3 ? 0 : (n - 2) * (n - 1) * n / 6;
        }

        private Map<Integer, List<Integer>> seqToItemPositions() {
            Map<Integer, List<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new ArrayList<>());
                }
                result.get(val).add(i);
            }
            result.values().forEach(Collections::sort);
            return result;
        }

        private List<Integer> getStarters(Set<Integer> items) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : items) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (Math.abs(first) > MAX || Math.abs(second) > MAX) {
                    continue;
                }
                if (!items.contains(candidate * k) || !items.contains(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;
import java.util.stream.IntStream;

public class C {

    private static final long MAX = 1000000000;

    private static class Solver {

        private final int k;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.seq = seq;
        }

        public long getCombinationsCount() {
            Map<Integer, int[]> itemPositions = seqToItemPositions();
            List<Integer> starters = getBaseCandidates(itemPositions);
            long result = 0;
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, int[]> itemPositions) {
            if (k == 1) {
                long n = IntStream.of(itemPositions.get(starter)).sum();
                if (n < 3) {
                    return 0;
                }
                return n * (n - 1) * (n - 2) / 6;
            }
            long[] secondCounts = new long[seq.size()];
            long secondCounter = 0;
            int[] secondPositions = itemPositions.get(starter * k * k);
            for (int i = secondCounts.length - 1; i >= 0; i--) {
                if (secondPositions[i] != 0)) {
                    secondCounter += 1;
                }
                secondCounts[i] = secondCounter;
            }

            long[] firstCounts = new long[seq.size()];
            long firstCounter = 0;
            int[] firstPositions = itemPositions.get(starter * k);
            for (int i = firstCounts.length - 1; i >= 0; i--) {
                if (firstPositions[i] != 0) {
                    firstCounter += secondCounts[i];
                }
                firstCounts[i] = firstCounter;
            }

            long [] zeroCounts = new long[seq.size()];
            long zeroCounter = 0;
            int[] zeroPositions = itemPositions.get(starter);
            for (int i = zeroCounts.length - 1; i >= 0; i--) {
                if (zeroPositions[i] != 0)) {
                    zeroCounter += firstCounts[i];
                }
                zeroCounts[i] = zeroCounter;
            }
            return zeroCounts[0];
        }

        private Map<Integer, int[]> seqToItemPositions() {
            Map<Integer, int[]> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new int[seq.size()]);
                }
                result.get(val)[i] = 1;
            }
            return result;
        }

        private List<Integer> getBaseCandidates(Map<Integer, int[]> itemPositions) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : itemPositions.keySet()) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (first > MAX || second > MAX) {
                    continue;
                }
                if (!itemPositions.containsKey(candidate * k) || !itemPositions.containsKey(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;
import java.util.stream.Collectors;

public class C {

    private static final long MAX = 1000000000;

    private static class Point implements Comparable<Point> {
        private final int x;
        private final long value;

        public Point(int x, long value) {
            this.x = x;
            this.value = value;
        }

        public int getX() {
            return x;
        }

        public long getValue() {
            return value;
        }

        @Override
        public int compareTo(Point o) {
            return x - o.x;
        }
    }

    private static class Solver {

        private final int k;
        private final long zeroCount;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.zeroCount = seq.stream().filter(x -> x == 0).count();
            this.seq = seq.stream().filter(x -> x != 0).collect(Collectors.toList());
        }

        public long getCombinationsCount() {
            long result = zeroCount >=3 ? zeroCount * (zeroCount - 1) * (zeroCount - 2) / 6 : 0;
            if (seq.size() < 3) {
                return result;
            }
            Map<Integer, List<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getBaseCandidates(itemPositions.keySet());
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, List<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                if (n < 3) {
                    return 0;
                }
                return n * (n - 1) * (n - 2) / 6;
            }
            List<Point> secondPoints = new ArrayList<>();
            long secondCounter = 0;
            List<Integer> secondPositions = itemPositions.get(starter * k * k);
            for (int i = secondPositions.size() - 1; i >= 0; i--) {
                secondPoints.add(new Point(secondPositions.get(i), ++secondCounter));
            }
            Collections.reverse(secondPoints);

            List<Point> firstPoints = new ArrayList<>();
            long firstCounter = 0;
            List<Integer> firstPositions = itemPositions.get(starter * k);
            for (int i = firstPositions.size() - 1; i >= 0; i--) {
                int nextSecondIndex = firstIndexGreater(secondPoints, firstPositions.get(i));
                if (nextSecondIndex < secondPoints.size()) {
                    firstCounter += secondPoints.get(nextSecondIndex).getValue();
                }
                firstPoints.add(new Point(firstPositions.get(i), firstCounter));
            }
            Collections.reverse(firstPoints);

            List<Point> zeroPoints = new ArrayList<>();
            long zeroCounter = 0;
            List<Integer> zeroPositions = itemPositions.get(starter);
            for (int i = zeroPositions.size() - 1; i >= 0; i--) {
                int nextFirstIndex = firstIndexGreater(firstPoints, zeroPositions.get(i));
                if (nextFirstIndex < firstPoints.size()) {
                    zeroCounter += firstPoints.get(nextFirstIndex).getValue();
                }
                zeroPoints.add(new Point(zeroPositions.get(i), zeroCounter));
            }
            Collections.reverse(zeroPoints);
            return zeroPoints.get(0).getValue();
        }

        int firstIndexGreater(List<Point> list, int value) {
            int searchResult = Collections.binarySearch(list, new Point(value, 0));
            return -(searchResult + 1);
        }

        private Map<Integer, List<Integer>> seqToItemPositions() {
            Map<Integer, List<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new ArrayList<>());
                }
                result.get(val).add(i);
            }
            result.values().forEach(Collections::sort);
            return result;
        }

        private List<Integer> getBaseCandidates(Set<Integer> items) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : items) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (first > MAX || second > MAX) {
                    continue;
                }
                if (!items.contains(candidate * k) || !items.contains(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;
import java.util.stream.Collectors;

public class C {

    private static final long MAX = 1000000000;

    private static class Point implements Comparable<Point> {
        private final int position;
        private final long value;

        public Point(int position, long value) {
            this.position = position;
            this.value = value;
        }

        public int getPosition() {
            return position;
        }

        public long getValue() {
            return value;
        }

        @Override
        public int compareTo(Point o) {
            return position - o.position;
        }
    }

    private static class Solver {

        private final int k;
        private final long zeroCount;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.zeroCount = seq.stream().filter(x -> x == 0).count();
            this.seq = seq.stream().filter(x -> x != 0).collect(Collectors.toList());
        }

        public long getCombinationsCount() {
            long result = c3(zeroCount);
            if (seq.size() < 3) {
                return result;
            }
            Map<Integer, List<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getStarters(itemPositions.keySet());
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, List<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                return c3(n);
            }

            List<Point> secondPoints = getSecondPoints(itemPositions.get(starter * k * k));
            List<Point> firstPoints = getFirstOrZeroPoints(itemPositions.get(starter * k), secondPoints);
            List<Point> zeroPoints = getFirstOrZeroPoints(itemPositions.get(starter), firstPoints);

            return zeroPoints.get(0).getValue();
        }

        private List<Point> getSecondPoints(List<Integer> positions) {
            List<Point> result = new ArrayList<>();
            long counter = 0;
            for (int i = positions.size() - 1; i >= 0; i--) {
                result.add(new Point(positions.get(i), ++counter));
            }
            Collections.reverse(result);
            return result;
        }

        private List<Point> getFirstOrZeroPoints(List<Integer> positions, List<Point> previousPoints) {
            List<Point> result = new ArrayList<>();
            long counter = 0;
            for (int i = positions.size() - 1; i >= 0; i--) {
                int searchResult = Collections.binarySearch(previousPoints, new Point(positions.get(i), 0));
                int nextPointInPrevious = -(searchResult + 1);
                if (nextPointInPrevious < previousPoints.size()) {
                    counter += previousPoints.get(nextPointInPrevious).getValue();
                    result.add(new Point(positions.get(i), counter));
                }
            }
            Collections.reverse(result);
            return result;
        }

        private long c3(long n) {
            return n < 3 ? 0 : (n - 2) * (n - 1) * n / 6;
        }

        private Map<Integer, List<Integer>> seqToItemPositions() {
            Map<Integer, List<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new ArrayList<>());
                }
                result.get(val).add(i);
            }
            result.values().forEach(Collections::sort);
            return result;
        }

        private List<Integer> getStarters(Set<Integer> items) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : items) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (Math.abs(first) > MAX || Math.abs(second) > MAX) {
                    continue;
                }
                if (!items.contains(candidate * k) || !items.contains(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;
import java.util.stream.Collectors;

public class C {

    private static final long MAX = 1000000000;

    private static class Point implements Comparable<Point> {
        private final int position;
        private final long value;

        public Point(int position, long value) {
            this.position = position;
            this.value = value;
        }

        public int getPosition() {
            return position;
        }

        public long getValue() {
            return value;
        }

        @Override
        public int compareTo(Point o) {
            return position - o.position;
        }
    }

    private static class Solver {

        private final int k;
        private final long zeroCount;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.zeroCount = seq.stream().filter(x -> x == 0).count();
            this.seq = seq.stream().filter(x -> x != 0).collect(Collectors.toList());
        }

        public long getCombinationsCount() {
            long result = c3(zeroCount);
            if (seq.size() < 3) {
                return result;
            }
            Map<Integer, List<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getStarters(itemPositions.keySet());
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private Map<Integer, List<Integer>> seqToItemPositions() {
            Map<Integer, List<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new ArrayList<>());
                }
                result.get(val).add(i);
            }
            result.values().forEach(Collections::sort);
            return result;
        }

        private List<Integer> getStarters(Set<Integer> seqItems) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : seqItems) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (Math.abs(first) > MAX || Math.abs(second) > MAX) {
                    continue;
                }
                if (!seqItems.contains(candidate * k) || !seqItems.contains(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, List<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                return c3(n);
            }

            List<Point> secondPoints = getSecondPoints(itemPositions.get(starter * k * k));
            List<Point> firstPoints = getFirstOrZeroPoints(itemPositions.get(starter * k), secondPoints);
            List<Point> zeroPoints = getFirstOrZeroPoints(itemPositions.get(starter), firstPoints);

            return zeroPoints.isEmpty() ? 0 : zeroPoints.get(0).getValue();
        }

        private List<Point> getSecondPoints(List<Integer> positions) {
            List<Point> result = new ArrayList<>();
            long counter = 0;
            for (int i = positions.size() - 1; i >= 0; i--) {
                result.add(new Point(positions.get(i), ++counter));
            }
            Collections.reverse(result);
            return result;
        }

        private List<Point> getFirstOrZeroPoints(List<Integer> positions, List<Point> previousPoints) {
            List<Point> result = new ArrayList<>();
            long counter = 0;
            for (int i = positions.size() - 1; i >= 0; i--) {
                int searchResult = Collections.binarySearch(previousPoints, new Point(positions.get(i), 0));
                int nextPointInPrevious = -(searchResult + 1);
                if (nextPointInPrevious < previousPoints.size()) {
                    counter += previousPoints.get(nextPointInPrevious).getValue();
                    result.add(new Point(positions.get(i), counter));
                }
            }
            Collections.reverse(result);
            return result;
        }

        private long c3(long n) {
            return n < 3 ? 0 : (n - 2) * (n - 1) * n / 6;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;
import java.util.stream.Collectors;

public class C {

    private static final long MAX = 1000000000;

    private static class Point implements Comparable<Point> {
        private final int x;
        private final long value;

        public Point(int x, long value) {
            this.x = x;
            this.value = value;
        }

        public int getX() {
            return x;
        }

        public long getValue() {
            return value;
        }

        @Override
        public int compareTo(Point o) {
            return x - o.x;
        }
    }

    private static class Solver {

        private final int k;
        private final long zeroCount;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.zeroCount = seq.stream().filter(x -> x == 0).count();
            this.seq = seq.stream().filter(x -> x != 0).collect(Collectors.toList());
        }

        public long getCombinationsCount() {
            if (seq.size() < 3) {
                return 0;
            }
            Map<Integer, List<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getBaseCandidates(itemPositions.keySet());
            long result = zeroCount * (zeroCount - 1) * (zeroCount - 2) / 6;
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, List<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                if (n < 3) {
                    return 0;
                }
                return n * (n - 1) * (n - 2) / 6;
            }
            List<Point> secondPoints = new ArrayList<>();
            long secondCounter = 0;
            List<Integer> secondPositions = itemPositions.get(starter * k * k);
            for (int i = secondPositions.size() - 1; i >= 0; i--) {
                secondPoints.add(new Point(secondPositions.get(i), ++secondCounter));
            }
            Collections.reverse(secondPoints);

            List<Point> firstPoints = new ArrayList<>();
            long firstCounter = 0;
            List<Integer> firstPositions = itemPositions.get(starter * k);
            for (int i = firstPositions.size() - 1; i >= 0; i--) {
                int nextSecondIndex = firstIndexGreater(secondPoints, firstPositions.get(i));
                if (nextSecondIndex < secondPoints.size()) {
                    firstCounter += secondPoints.get(nextSecondIndex).getValue();
                }
                firstPoints.add(new Point(firstPositions.get(i), firstCounter));
            }
            Collections.reverse(firstPoints);

            List<Point> zeroPoints = new ArrayList<>();
            long zeroCounter = 0;
            List<Integer> zeroPositions = itemPositions.get(starter);
            for (int i = zeroPositions.size() - 1; i >= 0; i--) {
                int nextFirstIndex = firstIndexGreater(firstPoints, zeroPositions.get(i));
                if (nextFirstIndex < firstPoints.size()) {
                    zeroCounter += firstPoints.get(nextFirstIndex).getValue();
                }
                zeroPoints.add(new Point(zeroPositions.get(i), zeroCounter));
            }
            Collections.reverse(zeroPoints);
            return zeroPoints.get(0).getValue();
        }

        int firstIndexGreater(List<Point> list, int value) {
            int searchResult = Collections.binarySearch(list, new Point(value, 0));
            return -(searchResult + 1);
        }

        private Map<Integer, List<Integer>> seqToItemPositions() {
            Map<Integer, List<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new ArrayList<>());
                }
                result.get(val).add(i);
            }
            result.values().forEach(Collections::sort);
            return result;
        }

        private List<Integer> getBaseCandidates(Set<Integer> items) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : items) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (first > MAX || second > MAX) {
                    continue;
                }
                if (!items.contains(candidate * k) || !items.contains(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;
import java.util.stream.Collectors;

public class C {

    private static final long MAX = 1000000000;

    private static class Point implements Comparable<Point> {
        private final int position;
        private final long value;

        public Point(int position, long value) {
            this.position = position;
            this.value = value;
        }

        public int getPosition() {
            return position;
        }

        public long getValue() {
            return value;
        }

        @Override
        public int compareTo(Point o) {
            return position - o.position;
        }
    }

    private static class Solver {

        private final int k;
        private final long zeroCount;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.zeroCount = seq.stream().filter(x -> x == 0).count();
            this.seq = seq.stream().filter(x -> x != 0).collect(Collectors.toList());
        }

        public long getCombinationsCount() {
            long result = c3(zeroCount);
            if (seq.size() < 3) {
                return result;
            }
            Map<Integer, List<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getStarters(itemPositions.keySet());
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, List<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                return c3(n);
            }

            List<Point> secondPoints = getSecondPoints(itemPositions.get(starter * k * k));
            List<Point> firstPoints = getFirstOrZeroPoints(itemPositions.get(starter * k), secondPoints);
            List<Point> zeroPoints = getFirstOrZeroPoints(itemPositions.get(starter), firstPoints);

            return zeroPoints.get(0).getValue();
        }

        private List<Point> getSecondPoints(List<Integer> positions) {
            List<Point> result = new ArrayList<>();
            long counter = 0;
            for (int i = positions.size() - 1; i >= 0; i--) {
                result.add(new Point(positions.get(i), ++counter));
            }
            Collections.reverse(result);
            return result;
        }

        private List<Point> getFirstOrZeroPoints(List<Integer> positions, List<Point> previousPoints) {
            List<Point> result = new ArrayList<>();
            long counter = 0;
            for (int i = positions.size() - 1; i >= 0; i--) {
                int searchResult = Collections.binarySearch(previousPoints, new Point(positions.get(i), 0));
                int nextPointInPrevious = -(searchResult + 1);
                if (nextPointInPrevious < previousPoints.size()) {
                    counter += previousPoints.get(nextPointInPrevious).getValue();
                }
                result.add(new Point(positions.get(i), counter));
            }
            Collections.reverse(result);
            return result;
        }

        private long c3(long n) {
            return n < 3 ? 0 : (n - 2) * (n - 1) * n / 6;
        }

        private Map<Integer, List<Integer>> seqToItemPositions() {
            Map<Integer, List<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new ArrayList<>());
                }
                result.get(val).add(i);
            }
            result.values().forEach(Collections::sort);
            return result;
        }

        private List<Integer> getStarters(Set<Integer> items) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : items) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (Math.abs(first) > MAX || Math.abs(second) > MAX) {
                    continue;
                }
                if (!items.contains(candidate * k) || !items.contains(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}


import java.util.*;

public class C {

    private static final long MAX = 1000000000;

    private static class Solver {

        private final int k;
        private final List<Integer> seq;

        private Solver(int k, List<Integer> seq) {
            this.k = k;
            this.seq = seq;
        }

        public long getCombinationsCount() {
            Map<Integer, Set<Integer>> itemPositions = seqToItemPositions();
            List<Integer> starters = getBaseCandidates(itemPositions);
            long result = 0;
            long result0 = 0;
            for (Integer starter : starters) {
                result += getCombinationsCountForStarter(starter, itemPositions);
//                result0 += getCombinationsCountForStarter0(starter, itemPositions);
            }
//            if (result != result0) {
//                System.out.println("wrong");
//            }
            return result;
        }

        private long getCombinationsCountForStarter(Integer starter, Map<Integer, Set<Integer>> itemPositions) {
            if (k == 1) {
                long n = itemPositions.get(starter).size();
                if (n < 3) {
                    return 0;
                }
                return n * (n - 1) * (n - 2) / 6;
            }
            long[] secondCounts = new long[seq.size()];
            long secondCounter = 0;
            Set<Integer> secondPositions = itemPositions.get(starter * k * k);
            for (int i = secondCounts.length - 1; i >= 0; i--) {
                if (secondPositions.contains(i)) {
                    secondCounter += 1;
                }
                secondCounts[i] = secondCounter;
            }

            long[] firstCounts = new long[seq.size()];
            long firstCounter = 0;
            Set<Integer> firstPositions = itemPositions.get(starter * k);
            for (int i = firstCounts.length - 1; i >= 0; i--) {
                if (firstPositions.contains(i)) {
                    firstCounter += secondCounts[i];
                }
                firstCounts[i] = firstCounter;
            }

            long [] zeroCounts = new long[seq.size()];
            long zeroCounter = 0;
            Set<Integer> zeroPositions = itemPositions.get(starter);
            for (int i = zeroCounts.length - 1; i >= 0; i--) {
                if (zeroPositions.contains(i)) {
                    zeroCounter += firstCounts[i];
                }
                zeroCounts[i] = zeroCounter;
            }
            return zeroCounts[0];
        }

        private int getCombinationsCountForStarter0(Integer starter, Map<Integer, Set<Integer>> itemPositions) {
            List<Integer> zeroPositions = new ArrayList<>(itemPositions.get(starter));
            List<Integer> firstPositions = new ArrayList<>(itemPositions.get(starter * k));
            List<Integer> secondPositions = new ArrayList<>(itemPositions.get(starter * k * k));
            Collections.sort(zeroPositions);
            Collections.sort(firstPositions);
            Collections.sort(secondPositions);
            int result = 0;
            for (int zeroIndex = 0; zeroIndex < zeroPositions.size(); zeroIndex++) {
                int zeroPosition = zeroPositions.get(zeroIndex);
                int firstStart = k != 1 ? firstIndexGreater(firstPositions, zeroPosition) : zeroIndex + 1;
                for (int firstIndex = firstStart; firstIndex < firstPositions.size(); firstIndex++) {
                    int firstPosition = firstPositions.get(firstIndex);
                    int secondStart = k != 1 ? firstIndexGreater(secondPositions, firstPosition) : firstIndex + 1;
                    int secondCounts = secondPositions.size() - secondStart;
                    if (secondCounts > 0) {
                        result += secondCounts;
                    }
                }
            }
            return result;
        }

        int firstIndexGreater(List<Integer> list, int value) {
            int searchResult = Collections.binarySearch(list, value);
            return searchResult >= 0 ? searchResult +1 : -(searchResult + 1);
        }

        private Map<Integer, Set<Integer>> seqToItemPositions() {
            Map<Integer, Set<Integer>> result = new HashMap<>();
            for (int i = 0; i < seq.size(); i++) {
                int val = seq.get(i);
                if (!result.containsKey(val)) {
                    result.put(val, new HashSet<>());
                }
                result.get(val).add(i);
            }
            return result;
        }

        private List<Integer> getBaseCandidates(Map<Integer, Set<Integer>> itemPositions) {
            List<Integer> result = new ArrayList<>();
            for (Integer candidate : itemPositions.keySet()) {
                long first = ((long) candidate) * k;
                long second = first * k;
                if (first > MAX || second > MAX) {
                    continue;
                }
                if (!itemPositions.containsKey(candidate * k) || !itemPositions.containsKey(candidate * k * k)) {
                    continue;
                }
                result.add(candidate);
            }
            return result;
        }
    }

    public static void main(String[] args) {
//        Random r = new Random();
//        for (int i = 0; i < 100; i++) {
//            int[] items = new int[] {1, 2, 4};
//            List<Integer> seq = new ArrayList<>();
//            int size = 1000;
//            for (int j = 0; j < size; j++) {
//                seq.add(items[r.nextInt(items.length)]);
//            }
//            new Solver(2, seq).getCombinationsCount();
//        }
//        System.out.println("success");

        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        List<Integer> seq = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            seq.add(s.nextInt());
        }
        System.out.println(new Solver(k, seq).getCombinationsCount());
    }
}