description: Monocarp has got an array a consisting of n integers. Let's denote k as the mathematic mean of these elements (note that it's possible that k is not an integer). 
  
  The mathematic mean of an array of n elements is the sum of elements divided by the number of these elements (i. e. sum divided by n).
  
  Monocarp wants to delete exactly two elements from a so that the mathematic mean of the remaining (n - 2) elements is still equal to k.
  
  Your task is to calculate the number of pairs of positions [i, j] (i < j) such that if the elements on these positions are deleted, the mathematic mean of (n - 2) remaining elements is equal to k (that is, it is equal to the mathematic mean of n elements of the original array a).
  
  Input
  
  The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of testcases.
  
  The first line of each testcase contains one integer n (3 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in the array.
  
  The second line contains a sequence of integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^{9}), where a_i is the i-th element of the array.
  
  The sum of n over all testcases doesn't exceed 2 ⋅ 10^5.
  
  Output
  
  Print one integer — the number of pairs of positions [i, j] (i < j) such that if the elements on these positions are deleted, the mathematic mean of (n - 2) remaining elements is equal to k (that is, it is equal to the mathematic mean of n elements of the original array a).
  
  Example
  
  Input
  
  
  4
  4
  8 8 8 8
  3
  50 20 10
  5
  1 4 7 3 5
  7
  1 2 3 4 5 6 7
  
  
  Output
  
  
  6
  0
  2
  3
  
  Note
  
  In the first example, any pair of elements can be removed since all of them are equal.
  
  In the second example, there is no way to delete two elements so the mathematic mean doesn't change.
  
  In the third example, it is possible to delete the elements on positions 1 and 3, or the elements on positions 4 and 5.
solution: import java.io.*;
  import java.util.*;
  
  import static java.lang.Math.*;
  
  public class _1598C {
      static void MainCode() {
          t = ni();
          while (t-- > 0) {
              HashMap<Long, Long> map = new HashMap<>();
              n = ni();
              long sum = 0;
              long ar[] = new long[n];
              for (int i = 0; i < n; i++) {
                  ar[i] = ni();
                  sum += ar[i];
                  map.put(ar[i], map.getOrDefault(ar[i], 0L) + 1);
              }
              if ((sum << 1) % n != 0) {
                  pl(0);
                  continue;
              }
              long ans = 0;
              long need = (sum << 1) / n;
              for (int i = 0; i < n; i++) {
                  long a = ar[i];
                  long b = need - ar[i];
  
                  if (map.containsKey(b)) ans += map.get(b) + ((a == b) ? -1 : 0);
              }
              pl(ans >> 1);
  
          }
      }
  
      //----------------------------------------The main code ends here------------------------------------------------------
      /*-------------------------------------------------------------------------------------------------------------------*/
      //-----------------------------------------Rest's all dust-------------------------------------------------------------
  
      static int mod9 = 1_000_000_007;
      static int n, m, l, k, t;
      static AwesomeInput input;
      static PrintWriter pw;
  
      static long power(long a, long b) {
          long x = max(a, b);
          if (b == 0) return 1;
          if ((b & 1) == 1) return a * power(a * a, b >> 1);
          return power(a * a, b >> 1);
      }
  
      // The Awesome Input Code is a fast IO method //
      static class AwesomeInput {
          private InputStream letsDoIT;
          private byte[] buf = new byte[1024];
          private int curChar;
          private int numChars;
          private SpaceCharFilter filter;
  
          private AwesomeInput(InputStream incoming) {
              this.letsDoIT = incoming;
          }
  
          public int read() {
              if (numChars == -1)
                  throw new InputMismatchException();
              if (curChar >= numChars) {
                  curChar = 0;
                  try {
                      numChars = letsDoIT.read(buf);
                  } catch (IOException e) {
                      throw new InputMismatchException();
                  }
                  if (numChars <= 0)
                      return -1;
              }
              return buf[curChar++];
          }
  
          private long ForLong() {
              int c = read();
              while (isSpaceChar(c))
                  c = read();
              int sgn = 1;
              if (c == '-') {
                  sgn = -1;
                  c = read();
              }
              long res = 0;
              do {
                  if (c < '0' || c > '9')
                      throw new InputMismatchException();
                  res *= 10;
                  res += c - '0';
                  c = read();
              }
              while (!isSpaceChar(c));
              return res * sgn;
          }
  
          private String ForString() {
              int c = read();
              while (isSpaceChar(c))
                  c = read();
              StringBuilder res = new StringBuilder();
              do {
                  res.appendCodePoint(c);
                  c = read();
              }
              while (!isSpaceChar(c));
  
              return res.toString();
          }
  
          public boolean isSpaceChar(int c) {
              if (filter != null)
                  return filter.isSpaceChar(c);
              return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
          }
  
          public interface SpaceCharFilter {
              boolean isSpaceChar(int ch);
          }
      }
  
      // functions to take input//
      static int ni() {
          return (int) input.ForLong();
      }
  
      static String ns() {
          return input.ForString();
      }
  
      static long nl() {
          return input.ForLong();
      }
  
      //functions to give output
      static void pl() {
          pw.println();
      }
  
      static void p(Object o) {
          pw.print(o + " ");
      }
  
      static void pws(Object o) {
          pw.print(o + "");
      }
  
      static void pl(Object o) {
          pw.println(o);
      }
  
      // Fast Sort is Radix Sort
      public static int[] fastSort(int[] f) {
          int n = f.length;
          int[] to = new int[n];
          {
              int[] b = new int[65537];
              for (int i = 0; i < n; i++) b[1 + (f[i] & 0xffff)]++;
              for (int i = 1; i <= 65536; i++) b[i] += b[i - 1];
              for (int i = 0; i < n; i++) to[b[f[i] & 0xffff]++] = f[i];
              int[] d = f;
              f = to;
              to = d;
          }
          {
              int[] b = new int[65537];
              for (int i = 0; i < n; i++) b[1 + (f[i] >>> 16)]++;
              for (int i = 1; i <= 65536; i++) b[i] += b[i - 1];
              for (int i = 0; i < n; i++) to[b[f[i] >>> 16]++] = f[i];
              int[] d = f;
              f = to;
              to = d;
          }
          return f;
      }
  
      public static void main(String[] args) {      //threading has been used to increase the stack size.
          try {
              input = new AwesomeInput(System.in);
              pw = new PrintWriter(System.out, true);
              input = new AwesomeInput(new FileInputStream(new File("input.txt")));
              pw = new PrintWriter(new BufferedWriter(new FileWriter(new File("output.txt"))), true);
  
          } catch (Exception e) {
          }
  
          new Thread(null, null, "MyThread", 1 << 25)  //the last parameter is stack size desired.
          {
              public void run() {
                  try {
                      double s = System.currentTimeMillis();
                      MainCode();
                      //System.out.println(("\nExecution Time : " + ((double) System.currentTimeMillis() - s) / 1000) + " s");
                      pw.flush();
                      pw.close();
                  } catch (Exception e) {
                      e.printStackTrace();
                      System.exit(1);
                  }
              }
          }.start();
      }
  }