'use strict';
// HEADER:
const print = function(arg){
  let str = arg;
  if (arguments.length>1)
    str = Array.prototype.slice.call(arguments).join(' ');
  process.stdout.write(str+'\n');
};
let IS_MULTITEST, testN, tests;
const pcalc = (testN, toPrint)=>{
  let res = CALC(testN);
  if (Array.isArray(res)){
    toPrint.push(res.join(' '));
  } else if (Number.isFinite(res) || typeof res=='string'){
    toPrint.push(res+'');
  }
  if (process.env.DEBUG && toPrint.length){
    let ans = toPrint.pop();
    print(ans);
    let expected = tests[testN-1];
    if (expected){
      if (expected==ans)
        console.log('\x1b[32m%s\x1b[0m', 'correct');
      else {
        console.log('\x1b[31m%s\x1b[0m', 'wrong, expected:', expected);
      }
    }
  }
};

const main = ()=>{
  let toPrint = [];
  L('\nSolving:');
  if (IS_MULTITEST){
    let T = +readline()
    for (testN=1; testN<=T; testN++)
      pcalc(testN, toPrint);
  } else {
    pcalc(1, toPrint);
  }
  if (toPrint.length){
    print(toPrint.join('\n'));
  }
};

const lines = [];
const rl = require('readline').createInterface({
   input: process.stdin, output: process.stdout});
rl.on('line', line=>{
  lines.push(line);
});
rl.on('close', main);
let rli = 0;
const readline = ()=>lines[rli++];
const L = process.env.DEBUG ? console.log : ()=>{};
const LT = obj=>L(`\nTest #${testN}`, obj);

const ra = ()=>readline().split(' ').map(a=>+a)
const pa = arr=>print(arr.join(' '));

tests = [
``, // Test #1
``, // Test #2
``, // Test #3
``, // Test #4
``, // Test #5
];

// SOLUTION:
IS_MULTITEST = true;

const CALC = (testN)=>{
  // READING:
  let x = +readline();
  LT({x});
  // PROCESSING:
  if (x>1500)
    return 'YES';
  for (let i11=0; i11<150; i11++){
    for (let i111=0; i111<15; i111++){
      if (11*i11+111*i111===x)
        return 'YES'

    }

  }
  return 'NO'
};

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const mxll = (...args) => args.reduce((m, e) => e > m ? e : m);
const mill = (...args) => args.reduce((m, e) => e < m ? e : m);
const sqll = (v) => { if (v < 0n) throw 'negative input'; if (v < 2n) return v; const dfs = (n, x0) => { let x1 = ((n / x0) + x0) >> 1n; if (x0 === x1 || x0 === (x1 - 1n)) return x0; return dfs(n, x1); }; return dfs(v, 1n); }; // has >> 0
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const ll = BigInt;
const combination = (m, n) => { return factorial(m, n) / factorial(n, n); };
const factorial = (m, n) => { let num = 1n; let cnt = 0; for (let i = ll(m); i > 0; i--) { if (cnt == n) break; num *= i; cnt++; } return num; };
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const bitCount = (n) => { n = n - ((n >> 1) & 0x55555555); n = (n & 0x33333333) + ((n >> 2) & 0x33333333); return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; };
const powmod = (a, b, mod) => { let r = 1n; while (b > 0n) { if (b % 2n == 1) r = r * a % mod; b >>= 1n; a = a * a % mod; } return r; };
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const sortPart = (a, k) => { let l = a.slice(0, k); l.sort((x, y) => x - y); let r = a.slice(k); return l.concat(r); }; // sort first kth
const initialize2DArrayNew = (m, n) => { let data = []; for (let i = 0; i < m; i++) { let tmp = Array(n).fill(0); data.push(tmp); } return data; };
const stmkey_in = (m) => new Map([...m].sort((x, y) => x[0] - y[0]));
const stmkey_de = (m) => new Map([...m].sort((x, y) => y[0] - x[0]));
const stmvalue_in = (m) => new Map([...m].sort((x, y) => x[1] - y[1]));
const stmvalue_de = (m) => new Map([...m].sort((x, y) => y[1] - x[1]));
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
const counter_value_in_indexA_in = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).push(i); } return m; };
const counter_value_in_indexA_de = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).unshift(i); } return m; };
const reverse2 = (s) => { let res = ""; for (let i = s.length - 1; i >= 0; i--) { res += s[i]; } return res; };
const isPalindrome = (s) => { let n = s.length; let i = 0; let j = n - 1; while (i < j) { if (s[i++] != s[j--]) return false; } return true; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 05/28/21 night

const MAX = 111111;
let dp;
const solve = (x) => {
    pr(x >= 10000 ? 'YES' : (dp[x] ? 'YES' : 'NO'));
};

const prepare = () => {
    let n = MAX + 1;
    dp = Array(n).fill(0);
    dp[0] = 1;
    for (let i = 11; i <= MAX; i = i * 10 + 1) {
        for (let j = 0; j < n - i; j++) {
            dp[i + j] |= dp[j];
        }
    }
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(Number(line));
    });
    rl.on('close', () => {
        let t = input[0];
        let i = 1;
        prepare();
        while (t--) {
            solve(input[i]);
            i++;
        }
    });
};

main()

/**
*    author:  Mohamed Abo Okail
*    created: O4/O6/2O21
**/

var t = parseInt(readline());

while(t--) {
  var x = parseInt(readline())
  if(111 * (x % 11) <= x) {
      print("YES");
  }
  else {
      print("NO");
  }
}

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const mxll = (...args) => args.reduce((m, e) => e > m ? e : m);
const mill = (...args) => args.reduce((m, e) => e < m ? e : m);
const sqll = (v) => { if (v < 0n) throw 'negative input'; if (v < 2n) return v; const dfs = (n, x0) => { let x1 = ((n / x0) + x0) >> 1n; if (x0 === x1 || x0 === (x1 - 1n)) return x0; return dfs(n, x1); }; return dfs(v, 1n); }; // has >> 0
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const ll = BigInt;
const combination = (m, n) => { return factorial(m, n) / factorial(n, n); };
const factorial = (m, n) => { let num = 1n; let cnt = 0; for (let i = ll(m); i > 0; i--) { if (cnt == n) break; num *= i; cnt++; } return num; };
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const bitCount = (n) => { n = n - ((n >> 1) & 0x55555555); n = (n & 0x33333333) + ((n >> 2) & 0x33333333); return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; };
const powmod = (a, b, mod) => { let r = 1n; while (b > 0n) { if (b % 2n == 1) r = r * a % mod; b >>= 1n; a = a * a % mod; } return r; };
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const sortPart = (a, k) => { let l = a.slice(0, k); l.sort((x, y) => x - y); let r = a.slice(k); return l.concat(r); }; // sort first kth
const initialize2DArrayNew = (m, n) => { let data = []; for (let i = 0; i < m; i++) { let tmp = Array(n).fill(0); data.push(tmp); } return data; };
const stmkey_in = (m) => new Map([...m].sort((x, y) => x[0] - y[0]));
const stmkey_de = (m) => new Map([...m].sort((x, y) => y[0] - x[0]));
const stmvalue_in = (m) => new Map([...m].sort((x, y) => x[1] - y[1]));
const stmvalue_de = (m) => new Map([...m].sort((x, y) => y[1] - x[1]));
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
const counter_value_in_indexA_in = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).push(i); } return m; };
const counter_value_in_indexA_de = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).unshift(i); } return m; };
const reverse2 = (s) => { let res = ""; for (let i = s.length - 1; i >= 0; i--) { res += s[i]; } return res; };
const isPalindrome = (s) => { let n = s.length; let i = 0; let j = n - 1; while (i < j) { if (s[i++] != s[j--]) return false; } return true; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 05/28/21 night

const solve = (x) => {
    pr(x >= 111 * (x % 11) ? 'YES' : 'NO');
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(Number(line));
    });
    rl.on('close', () => {
        let t = input[0];
        let i = 1;
        while (t--) {
            solve(input[i]);
            i++;
        }
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const mxll = (...args) => args.reduce((m, e) => e > m ? e : m);
const mill = (...args) => args.reduce((m, e) => e < m ? e : m);
const sqll = (v) => { if (v < 0n) throw 'negative input'; if (v < 2n) return v; const dfs = (n, x0) => { let x1 = ((n / x0) + x0) >> 1n; if (x0 === x1 || x0 === (x1 - 1n)) return x0; return dfs(n, x1); }; return dfs(v, 1n); }; // has >> 0
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const ll = BigInt;
const combination = (m, n) => { return factorial(m, n) / factorial(n, n); };
const factorial = (m, n) => { let num = 1n; let cnt = 0; for (let i = ll(m); i > 0; i--) { if (cnt == n) break; num *= i; cnt++; } return num; };
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const bitCount = (n) => { n = n - ((n >> 1) & 0x55555555); n = (n & 0x33333333) + ((n >> 2) & 0x33333333); return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; };
const powmod = (a, b, mod) => { let r = 1n; while (b > 0n) { if (b % 2n == 1) r = r * a % mod; b >>= 1n; a = a * a % mod; } return r; };
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const sortPart = (a, k) => { let l = a.slice(0, k); l.sort((x, y) => x - y); let r = a.slice(k); return l.concat(r); }; // sort first kth
const initialize2DArrayNew = (m, n) => { let data = []; for (let i = 0; i < m; i++) { let tmp = Array(n).fill(0); data.push(tmp); } return data; };
const stmkey_in = (m) => new Map([...m].sort((x, y) => x[0] - y[0]));
const stmkey_de = (m) => new Map([...m].sort((x, y) => y[0] - x[0]));
const stmvalue_in = (m) => new Map([...m].sort((x, y) => x[1] - y[1]));
const stmvalue_de = (m) => new Map([...m].sort((x, y) => y[1] - x[1]));
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
const counter_value_in_indexA_in = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).push(i); } return m; };
const counter_value_in_indexA_de = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).unshift(i); } return m; };
const reverse2 = (s) => { let res = ""; for (let i = s.length - 1; i >= 0; i--) { res += s[i]; } return res; };
const isPalindrome = (s) => { let n = s.length; let i = 0; let j = n - 1; while (i < j) { if (s[i++] != s[j--]) return false; } return true; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/28/21 morning
 * https://codeforces.com/contest/1526/problem/A
 */

const solve = (x) => {
    if (x == 1) return pr('YES');
    if (x == 10) return pr('NO');
    while ((x + '').length > 1) {
        let max = getMax(x);
        if (max == 1) break;
        x %= max;
        // pr(x, max);
        if (x == 0) return pr('YES');
    }
    // pr(x);
    pr('NO');
};

const getMax = (x) => {
    let s = x + '';
    let n = s.length;
    let max = '1'.repeat(n - 1);
    if (x >= Number(max + '1')) {
        max += '1';
    }
    return max - '0';
};
const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(Number(line));
    });
    rl.on('close', () => {
        let t = input[0];
        let i = 1;
        while (t--) {
            solve(input[i]);
            i++;
        }
    });
};

main()

// pr(3578 % 1111)

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const mxll = (...args) => args.reduce((m, e) => e > m ? e : m);
const mill = (...args) => args.reduce((m, e) => e < m ? e : m);
const sqll = (v) => { if (v < 0n) throw 'negative input'; if (v < 2n) return v; const dfs = (n, x0) => { let x1 = ((n / x0) + x0) >> 1n; if (x0 === x1 || x0 === (x1 - 1n)) return x0; return dfs(n, x1); }; return dfs(v, 1n); }; // has >> 0
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const ll = BigInt;
const combination = (m, n) => { return factorial(m, n) / factorial(n, n); };
const factorial = (m, n) => { let num = 1n; let cnt = 0; for (let i = ll(m); i > 0; i--) { if (cnt == n) break; num *= i; cnt++; } return num; };
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const bitCount = (n) => { n = n - ((n >> 1) & 0x55555555); n = (n & 0x33333333) + ((n >> 2) & 0x33333333); return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; };
const powmod = (a, b, mod) => { let r = 1n; while (b > 0n) { if (b % 2n == 1) r = r * a % mod; b >>= 1n; a = a * a % mod; } return r; };
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const sortPart = (a, k) => { let l = a.slice(0, k); l.sort((x, y) => x - y); let r = a.slice(k); return l.concat(r); }; // sort first kth
const initialize2DArrayNew = (m, n) => { let data = []; for (let i = 0; i < m; i++) { let tmp = Array(n).fill(0); data.push(tmp); } return data; };
const stmkey_in = (m) => new Map([...m].sort((x, y) => x[0] - y[0]));
const stmkey_de = (m) => new Map([...m].sort((x, y) => y[0] - x[0]));
const stmvalue_in = (m) => new Map([...m].sort((x, y) => x[1] - y[1]));
const stmvalue_de = (m) => new Map([...m].sort((x, y) => y[1] - x[1]));
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
const counter_value_in_indexA_in = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).push(i); } return m; };
const counter_value_in_indexA_de = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).unshift(i); } return m; };
const reverse2 = (s) => { let res = ""; for (let i = s.length - 1; i >= 0; i--) { res += s[i]; } return res; };
const isPalindrome = (s) => { let n = s.length; let i = 0; let j = n - 1; while (i < j) { if (s[i++] != s[j--]) return false; } return true; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/28/21 morning
 * https://codeforces.com/contest/1526/problem/A
 */

const solve = (x) => {
    while((x + '').length > 1) {
       let max = getMax(x);
       x %= max;
       if (x == 0) return pr('YES');
    }
    // pr(x);
    pr('NO');
};

const getMax = (x) => {
    let s = x + '';
    let n = s.length;
    let max = '1'.repeat(n - 1);
    if (x >= Number(max + '1')) {
        max += '1';
    }
    return max - '0';
};
const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(Number(line));
    });
    rl.on('close', () => {
        let t = input[0];
        let i = 1;
        while (t--) {
            solve(input[i]);
            i++;
        }
    });
};

main()

'use strict';
// HEADER:
const print = function(arg){
  let str = arg;
  if (arguments.length>1)
    str = Array.prototype.slice.call(arguments).join(' ');
  process.stdout.write(str+'\n');
};
let IS_MULTITEST, testN, tests;
const pcalc = (testN, toPrint)=>{
  let res = CALC(testN);
  if (Array.isArray(res)){
    toPrint.push(res.join(' '));
  } else if (Number.isFinite(res) || typeof res=='string'){
    toPrint.push(res+'');
  }
  if (process.env.DEBUG && toPrint.length){
    let ans = toPrint.pop();
    print(ans);
    let expected = tests[testN-1];
    if (expected){
      if (expected==ans)
        console.log('\x1b[32m%s\x1b[0m', 'correct');
      else {
        console.log('\x1b[31m%s\x1b[0m', 'wrong, expected:', expected);
      }
    }
  }
};

const main = ()=>{
  let toPrint = [];
  L('\nSolving:');
  if (IS_MULTITEST){
    let T = +readline()
    for (testN=1; testN<=T; testN++)
      pcalc(testN, toPrint);
  } else {
    pcalc(1, toPrint);
  }
  if (toPrint.length){
    print(toPrint.join('\n'));
  }
};

const lines = [];
const rl = require('readline').createInterface({
   input: process.stdin, output: process.stdout});
rl.on('line', line=>{
  lines.push(line);
});
rl.on('close', main);
let rli = 0;
const readline = ()=>lines[rli++];
const L = process.env.DEBUG ? console.log : ()=>{};
const LT = obj=>L(`\nTest #${testN}`, obj);

const ra = ()=>readline().split(' ').map(a=>+a)
const pa = arr=>print(arr.join(' '));

tests = [
``, // Test #1
``, // Test #2
``, // Test #3
``, // Test #4
``, // Test #5
];

// SOLUTION:
IS_MULTITEST = true;

const CALC = (testN)=>{
  // READING:
  let x = readline();
  LT({x});
  // PROCESSING:
  return x[x.length-1]===x[x.length-2] ? 'YES' : 'NO'
  if (+x<11)
    return 'NO'
  for (let i=x.length-2; i>=0; i--){
    if (x[i]>x[i+1])
      return 'NO'
  }
  return 'YES';
  let ans = 0;
  let ones = [];
  let d = 11;
  while (d<1e9){
    ones.push(d);
    d = d*10+1;
  }
  let flag = true;
  while (flag){
    for (let i=0; i<ones.length; i++){

    }

  }
  let r = ones.length-1;
  while (x>0 && r>=0){
    while (x>=ones[r]){
      x -= ones[r]
    };
    r--;
  }
  return x==0 ? 'YES' : 'NO';
};

'use strict';
// HEADER:
const print = function(arg){
  let str = arg;
  if (arguments.length>1)
    str = Array.prototype.slice.call(arguments).join(' ');
  process.stdout.write(str+'\n');
};
let IS_MULTITEST, testN, tests;
const pcalc = (testN, toPrint)=>{
  let res = CALC(testN);
  if (Array.isArray(res)){
    toPrint.push(res.join(' '));
  } else if (Number.isFinite(res) || typeof res=='string'){
    toPrint.push(res+'');
  }
  if (process.env.DEBUG && toPrint.length){
    let ans = toPrint.pop();
    print(ans);
    let expected = tests[testN-1];
    if (expected){
      if (expected==ans)
        console.log('\x1b[32m%s\x1b[0m', 'correct');
      else {
        console.log('\x1b[31m%s\x1b[0m', 'wrong, expected:', expected);
      }
    }
  }
};

const main = ()=>{
  let toPrint = [];
  L('\nSolving:');
  if (IS_MULTITEST){
    let T = +readline()
    for (testN=1; testN<=T; testN++)
      pcalc(testN, toPrint);
  } else {
    pcalc(1, toPrint);
  }
  if (toPrint.length){
    print(toPrint.join('\n'));
  }
};

const lines = [];
const rl = require('readline').createInterface({
   input: process.stdin, output: process.stdout});
rl.on('line', line=>{
  lines.push(line);
});
rl.on('close', main);
let rli = 0;
const readline = ()=>lines[rli++];
const L = process.env.DEBUG ? console.log : ()=>{};
const LT = obj=>L(`\nTest #${testN}`, obj);

const ra = ()=>readline().split(' ').map(a=>+a)
const pa = arr=>print(arr.join(' '));

tests = [
``, // Test #1
``, // Test #2
``, // Test #3
``, // Test #4
``, // Test #5
];

// SOLUTION:
IS_MULTITEST = true;

const CALC = (testN)=>{
  // READING:
  let [x] = ra();
  LT({x});
  // PROCESSING:
  let ans = 0;
  let ones = [];
  let d = 11;
  while (d<1e9){
    ones.push(d);
    d = d*10+1;
  }
  let r = ones.length-1;
  while (x>0 && r>=0){
    while (x>=ones[r]){
      x -= ones[r]
    };
    r--;
  }
  return x==0 ? 'YES' : 'NO';
};


'use strict';

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

const MOD = 998244353;
async function solve(n, a) {
  const f = [1];
  for (let i = 1; i <= n; i++) {
    f[i] = mul(f[i - 1], i);
  }
  const edges = [];
  const dis = (i, j) => Math.abs(a[i][0] - a[j][0]) + Math.abs(a[i][1] - a[j][1]);
  for (let i = 0; i < n - 1; i++) {
    for (let j = i + 1; j < n; j++) {
      const d = dis(i, j);
      edges.push([i, j, d]);
    }
  }
  edges.sort((a, b) => a[2] - b[2]);
  const st = [];
  const check = arr => {
    if (arr.length > 4) return false;
    let t = -1;
    for (let i = 0; i < arr.length; i++) {
      if (st[arr[i]]) return false;
      for (let j = i + 1; j < arr.length; j++) {
        if (st[arr[j]]) return false;
        if (t == -1) t = dis(arr[i], arr[j]);else if (t !== dis(arr[i], arr[j])) return false;
      }
    }
    return true;
  };
  const pos = [];
  for (let i = 0, j = 0; i < edges.length; i = ++j) {
    while (j + 1 < edges.length && edges[i][2] === edges[j + 1][2]) {
      j++;
    }
    const uf = new UnionFind(n);
    for (let k = i; k <= j; k++) {
      const [u, v] = edges[k];
      uf.union(u, v);
    }
    const groups = [...uf.group().values()];
    pos.push(...groups.filter(check));
    for (let k = i; k <= j; k++) {
      const [u, v] = edges[k];
      st[u] = 1;
      st[v] = 1;
    }
  }
  let p2 = 0,
      p3 = 0,
      p4 = 0;
  for (let arr of pos) {
    if (arr.length === 2) p2++;else if (arr.length === 3) p3++;else p4++;
  }
  let res = 0;
  const ff = [1n];
  for (let i = 1; i <= n; i++) {
    ff[i] = ff[i - 1] * BigInt(i);
  }
  [0, 0, pos.filter(arr => arr.length === 2).length, pos.filter(arr => arr.length === 3).length, pos.filter(arr => arr.length === 4).length];
  const c = (i, n) => mul(f[n], mul(power(f[i], MOD - 2), power(f[n - i], MOD - 2)));
  for (let i = 0; i <= p2; i++) {
    for (let j = 0; j <= p3; j++) {
      for (let k = 0; k <= p4; k++) {
        const N = n - i - j * 2 - k * 3;
        res = mod(res + mul(f[N], c(i, p2), c(j, p3), c(k, p4), c(N, n)));
      }
    }
  }
  return mod(res);
}
const mod = num => (num % MOD + MOD) % MOD;
function power(a, b) {
  let res = 1;
  while (b) {
    if (b & 1) res = mul(res, a);
    a = mul(a, a);
    b = b >> 1;
  }
  return res;
}
function mul(...args) {
  if (args.length === 1) return args[0];
  if (args.length > 2) return mul(args[0], mul(...args.slice(1)));
  const [a, b] = args;
  return ((a >> 16) * b % MOD * 65536 + (a & 65535) * b) % MOD;
}
class UnionFind {
  constructor(n) {
    _defineProperty(this, "vis", []);
    this.n = n;
    this.p = [...new Array(n).keys()];
  }
  union(i, j) {
    this.vis[i] = this.vis[j] = 1;
    const [rooti, rootj] = [this.find(i), this.find(j)];
    if (rooti === rootj) return;
    this.p[rootj] = rooti;
  }
  find(i) {
    const {
      p
    } = this;
    while (i !== p[i]) {
      p[i] = p[p[i]];
      i = p[p[i]];
    }
    return i;
  }
  group() {
    const groups = new Map();
    for (let i = 0; i < this.n; i++) {
      var _groups$get;
      if (!this.vis[i]) continue;
      const root = this.find(i);
      if (!groups.has(root)) groups.set(root, []);
      (_groups$get = groups.get(root)) === null || _groups$get === void 0 ? void 0 : _groups$get.push(i);
    }
    return groups;
  }
}

async function main(read) {
  try {
    let res = [];
    let t = 1;
    while (t--) {
      const n = Number(await read());
      const a = [];
      let tmp = n;
      while (tmp--) {
        a.push((await read()).split(' ').map(Number));
      }
      res.push(await solve(n, a));
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  console.log(output);
});

class Scaner {
    data = [];
    constructor() {
        process.stdin.setEncoding('utf-8');
        process.stdin.on('data', _data => {
            let lasttext = this.data.pop();
            if (lasttext == undefined)
                lasttext = ""
            _data = lasttext + _data;
            const l = _data.split('\n');
            this.data = this.data.concat(l.slice(0, -1).map(string => string.trim()), l[l.length - 1]);
        });
    }
    read = async () => {
        process.stdin.resume();
        while (this.data.length < 2) {
            await delay(0);
        }
        process.stdin.pause();
        let res = this.data.shift();
        if (res == "")
            return await read();
        return res;
    };
}
const delay = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms));
const print = (text) => process.stdout.write(text);
const scaner = new Scaner()
const read = scaner.read;
//----------------------------------------------------------------
//-----------------------the-code---------------------------------
//----------------------------------------------------------------

const dn = 998244353n;
const add = (x, y) => Number((BigInt(x) + BigInt(y)) % dn);
const mul = (x, y) => Number((BigInt(x) * BigInt(y)) % dn);
class Point {
    x = 0;
    y = 0;
    constructor(a = []) {
        this.x = a[0];
        this.y = a[1];
    }
    shortest = [];
    haveGroup = false;
}
class Ans {
    p = [];
    points = [];
    constructor(n) {
        this.p[n] = [1];
        for (var j = 0; j < n; j++) {
            this.p[n][j + 1] = mul(this.p[n][j], (n - j));
        }
    }
    addPoint(a = []) {
        this.points.push(new Point(a));
    }
    calculateShortest() {
        for (let curr in this.points) {
            let distance = [];
            const p1 = this.points[curr];
            let min = Infinity;
            for (let i in this.points) {
                if (i == curr)
                    continue;
                const p2 = this.points[i];
                distance[i] = Math.abs(p1.x - p2.x) + Math.abs(p1.y - p2.y);
                if (distance[i] < min)
                    min = distance[i];
            }
            distance[curr] = min;
            p1.shortest = distance.map((e, i) => e === min ? i : "").filter(String);
        }
    }
    haveGroup(p1 = new Point()) {//n**2
        p1.haveGroup = true;
        for (let b of p1.shortest) {
            const p2 = this.points[b];
            if (p1.shortest.length != p2.shortest.length) {
                p1.haveGroup = false;
                return;
            }
            for (let c in p2.shortest)
                if (p1.shortest[c] != p2.shortest[c]) {
                    p1.haveGroup = false;
                    return;
                }
        }
    }
    solve() {
        const n = this.points.length;
        for (let a = 0; a < n; a++) {//n**3
            const p1 = this.points[a];
            if (p1.haveGroup)
                continue;
            this.haveGroup(p1);
            if (p1.haveGroup) {
                p1.shortest.forEach(b => {
                    const p2 = this.points[b];
                    p2.haveGroup = true;
                });
                this.groups.push(p1.shortest.length - 1);
            }
        }
        const ways = (ng) => this.waysToGroups(this.groups.length - 1, ng);
        let sol = 0;
        for (let i = 1; i <= n; i++) {
            sol = add(sol, mul(ways(i), this.p[n][i]));
        }
        return sol;
    }
    groups = [];
    waysToGroups_db = [];
    waysToGroups(i, pn) {
        const N = this.points.length;
        if (i == -1) {
            if (N == pn)
                return 1;
            else
                return 0;
        }
        if (this.waysToGroups_db[i] == undefined)
            this.waysToGroups_db[i] = [];
        if (this.waysToGroups_db[i][pn] != undefined)
            return this.waysToGroups_db[i][pn];
        let c1 = this.waysToGroups(i - 1, pn);
        let c2 = 0;
        if (pn < N)
            c2 = this.waysToGroups(i - 1, pn + (this.groups[i]));
        return this.waysToGroups_db[i][pn] = add(c1, c2);
    }
}
(async () => {
    let t = parseInt(await read());
    let nn = t;
    let ans = new Ans(t);
    while (t--) {
        ans.addPoint(
            (await read()).split(' ').map(Number));
    }
    ans.calculateShortest();
    console.log(ans.solve());
})().then(() => { }
)

'use strict';

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

const MOD = 998244353;
async function solve(n, a) {
  const f = [1];
  for (let i = 1; i <= n; i++) {
    f[i] = mul(f[i - 1], i);
  }
  const edges = [];
  const dis = (i, j) => Math.abs(a[i][0] - a[j][0]) + Math.abs(a[i][1] - a[j][1]);
  for (let i = 0; i < n - 1; i++) {
    for (let j = i + 1; j < n; j++) {
      const d = dis(i, j);
      edges.push([i, j, d]);
    }
  }
  edges.sort((a, b) => a[2] - b[2]);
  const st = [];
  const check = arr => {
    if (arr.length > 4) return false;
    let t = -1;
    for (let i = 0; i < arr.length; i++) {
      if (st[arr[i]]) return false;
      for (let j = i + 1; j < arr.length; j++) {
        if (st[arr[j]]) return false;
        if (t == -1) t = dis(arr[i], arr[j]);else if (t !== dis(arr[i], arr[j])) return false;
      }
    }
    return true;
  };
  const pos = [];
  for (let i = 0, j = 0; i < edges.length; i = ++j) {
    while (j + 1 < edges.length && edges[i][2] === edges[j + 1][2]) {
      j++;
    }
    const uf = new UnionFind(n);
    for (let k = i; k <= j; k++) {
      const [u, v] = edges[k];
      uf.union(u, v);
    }
    const groups = [...uf.group().values()];
    pos.push(...groups.filter(check));
    for (let k = i; k <= j; k++) {
      const [u, v] = edges[k];
      st[u] = 1;
      st[v] = 1;
    }
  }
  let p2 = 0,
      p3 = 0,
      p4 = 0;
  for (let arr of pos) {
    if (arr.length === 2) p2++;else if (arr.length === 3) p3++;else p4++;
  }
  let res = 0;
  const ff = [1n];
  for (let i = 1; i <= n; i++) {
    ff[i] = ff[i - 1] * BigInt(i);
  }
  [0, 0, pos.filter(arr => arr.length === 2).length, pos.filter(arr => arr.length === 3).length, pos.filter(arr => arr.length === 4).length];
  const c = (i, n) => mul(f[n], mul(power(f[i], MOD - 2), power(f[n - i], MOD - 2)));
  for (let i = 0; i <= p2; i++) {
    for (let j = 0; j <= p3; j++) {
      for (let k = 0; k <= p4; k++) {
        const N = n - i - j * 2 - j * 3;
        res = mod(res + mul(mul(mul(f[N], c(i, p2)), mul(c(j, p3), c(k, p4))), c(N, n)));
      }
    }
  }
  return mod(res);
}
const mod = num => (num % MOD + MOD) % MOD;
function power(a, b) {
  let res = 1;
  while (b) {
    if (b & 1) res = mul(res, a);
    a = mul(a, a);
    b = b >> 1;
  }
  return res;
}
function mul(a, b) {
  return ((a >> 16) * b % MOD * 65536 + (a & 65535) * b) % MOD;
}
class UnionFind {
  constructor(n) {
    _defineProperty(this, "vis", []);
    this.n = n;
    this.p = [...new Array(n).keys()];
  }
  union(i, j) {
    this.vis[i] = this.vis[j] = 1;
    const [rooti, rootj] = [this.find(i), this.find(j)];
    if (rooti === rootj) return;
    this.p[rootj] = rooti;
  }
  find(i) {
    const {
      p
    } = this;
    while (i !== p[i]) {
      p[i] = p[p[i]];
      i = p[p[i]];
    }
    return i;
  }
  group() {
    const groups = new Map();
    for (let i = 0; i < this.n; i++) {
      var _groups$get;
      if (!this.vis[i]) continue;
      const root = this.find(i);
      if (!groups.has(root)) groups.set(root, []);
      (_groups$get = groups.get(root)) === null || _groups$get === void 0 ? void 0 : _groups$get.push(i);
    }
    return groups;
  }
}

async function main(read) {
  try {
    let res = [];
    let t = 1;
    while (t--) {
      const n = Number(await read());
      const a = [];
      let tmp = n;
      while (tmp--) {
        a.push((await read()).split(' ').map(Number));
      }
      res.push(await solve(n, a));
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  console.log(output);
});

'use strict';

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

const MOD = 998244353;
async function solve(n, a) {
  const f = [1];
  for (let i = 1; i <= n; i++) {
    f[i] = mul(f[i - 1], i);
  }
  const edges = [];
  const dis = (i, j) => Math.abs(a[i][0] - a[j][0]) + Math.abs(a[i][1] - a[j][1]);
  for (let i = 0; i < n - 1; i++) {
    for (let j = i + 1; j < n; j++) {
      const d = dis(i, j);
      edges.push([i, j, d]);
    }
  }
  edges.sort((a, b) => a[2] - b[2]);
  const st = [];
  const check = arr => {
    let t = -1;
    for (let i = 0; i < arr.length; i++) {
      if (st[arr[i]]) return false;
      for (let j = i + 1; j < arr.length; j++) {
        if (st[arr[j]]) return false;
        if (t == -1) t = dis(arr[i], arr[j]);else if (t !== dis(arr[i], arr[j])) return false;
      }
    }
    return true;
  };
  const pos = [];
  for (let i = 0, j = 0; i < edges.length; i = ++j) {
    while (j + 1 < edges.length && edges[i][2] === edges[j + 1][2]) {
      j++;
    }
    const uf = new UnionFind(n);
    for (let k = i; k <= j; k++) {
      const [u, v] = edges[k];
      uf.union(u, v);
    }
    const groups = [...uf.group().values()];
    pos.push(...groups.filter(check));
    for (let k = i; k <= j; k++) {
      const [u, v] = edges[k];
      st[u] = 1;
      st[v] = 1;
    }
  }
  let N = pos.length;
  let res = f[n];
  for (let i = 1; i < 1 << N; i++) {
    let count = 0,
        len = 0;
    for (let j = 0; j < N; j++) {
      if (i & 1 << j) {
        count += pos[j].length;
        len++;
      }
    }
    const n1 = n - count + len;
    res = mod(res + mul(f[n], power(f[n - n1], MOD - 2)));
  }
  return mod(res);
}
const mod = num => (num % MOD + MOD) % MOD;
function power(a, b) {
  let res = 1;
  while (b) {
    if (b & 1) res = mul(res, a);
    a = mul(a, a);
    b = b >> 1;
  }
  return res;
}
function mul(a, b) {
  return ((a >> 16) * b % MOD * 65536 + (a & 65535) * b) % MOD;
}
class UnionFind {
  constructor(n) {
    _defineProperty(this, "vis", []);
    this.n = n;
    this.p = [...new Array(n).keys()];
  }
  union(i, j) {
    this.vis[i] = this.vis[j] = 1;
    const [rooti, rootj] = [this.find(i), this.find(j)];
    if (rooti === rootj) return;
    this.p[rootj] = rooti;
  }
  find(i) {
    const {
      p
    } = this;
    while (i !== p[i]) {
      p[i] = p[p[i]];
      i = p[p[i]];
    }
    return i;
  }
  group() {
    const groups = new Map();
    for (let i = 0; i < this.n; i++) {
      var _groups$get;
      if (!this.vis[i]) continue;
      const root = this.find(i);
      if (!groups.has(root)) groups.set(root, []);
      (_groups$get = groups.get(root)) === null || _groups$get === void 0 ? void 0 : _groups$get.push(i);
    }
    return groups;
  }
}

async function main(read) {
  try {
    let res = [];
    let t = 1;
    while (t--) {
      const n = Number(await read());
      const a = [];
      let tmp = n;
      while (tmp--) {
        a.push((await read()).split(' ').map(Number));
      }
      res.push(await solve(n, a));
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  console.log(output);
});

'use strict';

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

const MOD = 998244353;
async function solve(n, a) {
  const f = [1];
  for (let i = 1; i <= n; i++) {
    f[i] = mul(f[i - 1], i);
  }
  const edges = [];
  const dis = (i, j) => Math.abs(a[i][0] - a[j][0]) + Math.abs(a[i][1] - a[j][1]);
  for (let i = 0; i < n - 1; i++) {
    for (let j = i + 1; j < n; j++) {
      const d = dis(i, j);
      edges.push([i, j, d]);
    }
  }
  edges.sort((a, b) => a[2] - b[2]);
  const st = [];
  const check = arr => {
    let t = -1;
    for (let i = 0; i < arr.length; i++) {
      if (st[arr[i]]) return false;
      for (let j = i + 1; j < arr.length; j++) {
        if (st[arr[j]]) return false;
        if (t == -1) t = dis(arr[i], arr[j]);else if (t !== dis(arr[i], arr[j])) return false;
      }
    }
    return true;
  };
  const pos = [];
  for (let i = 0, j = 0; i < edges.length; i = ++j) {
    while (j + 1 < edges.length && edges[i][2] === edges[j][2]) {
      j++;
    }
    const uf = new UnionFind(n);
    for (let k = i; k <= j; k++) {
      const [u, v] = edges[k];
      if (st[u] || st[v]) continue;
      uf.union(u, v);
    }
    const groups = [...uf.group().values()];
    pos.push(...groups.filter(check));
    for (let k = i; k <= j; k++) {
      const [u, v] = edges[k];
      st[u] = 1;
      st[v] = 1;
    }
  }
  let N = pos.length;
  let res = f[n];
  for (let i = 1; i < 1 << N; i++) {
    let count = 0,
        len = 0;
    for (let j = 0; j < N; j++) {
      if (i & 1 << j) {
        count += pos[j].length;
        len++;
      }
    }
    const n1 = n - count + len;
    res = mod(res + mul(f[n], power(f[n - n1], MOD - 2)));
  }
  return mod(res);
}
const mod = num => (num % MOD + MOD) % MOD;
function power(a, b) {
  let res = 1;
  while (b) {
    if (b & 1) res = mul(res, a);
    a = mul(a, a);
    b = b >> 1;
  }
  return res;
}
function mul(a, b) {
  return ((a >> 16) * b % MOD * 65536 + (a & 65535) * b) % MOD;
}
class UnionFind {
  constructor(n) {
    _defineProperty(this, "vis", []);
    this.n = n;
    this.p = [...new Array(n).keys()];
  }
  union(i, j) {
    this.vis[i] = this.vis[j] = 1;
    const [rooti, rootj] = [this.find(i), this.find(j)];
    if (rooti === rootj) return;
    this.p[rootj] = rooti;
  }
  find(i) {
    const {
      p
    } = this;
    while (i !== p[i]) {
      p[i] = p[p[i]];
      i = p[p[i]];
    }
    return i;
  }
  group() {
    const groups = new Map();
    for (let i = 0; i < this.n; i++) {
      var _groups$get;
      if (!this.vis[i]) continue;
      const root = this.find(i);
      if (!groups.has(root)) groups.set(root, []);
      (_groups$get = groups.get(root)) === null || _groups$get === void 0 ? void 0 : _groups$get.push(i);
    }
    return groups;
  }
}

async function main(read) {
  try {
    let res = [];
    let t = 1;
    while (t--) {
      const n = Number(await read());
      const a = [];
      let tmp = n;
      while (tmp--) {
        a.push((await read()).split(' ').map(Number));
      }
      res.push(await solve(n, a));
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  console.log(output);
});

'use strict';

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

const MOD = 998244353;
async function solve(n, a) {
  const f = [1];
  for (let i = 1; i <= n; i++) {
    f[i] = mul(f[i - 1], i);
  }
  const edges = Array.from({
    length: n
  }, () => []);
  const dis = (i, j) => Math.abs(a[i][0] - a[j][0]) + Math.abs(a[i][1] - a[j][1]);
  for (let i = 0; i < n - 1; i++) {
    let min = -1;
    for (let j = i + 1; j < n; j++) {
      const d = dis(i, j);
      if (min === -1 || min > d) {
        min = d;
        edges[i] = [j];
      } else if (min === d) {
        edges[i].push(j);
      }
    }
    edges[i] = [min, ...edges[i], i];
  }
  edges.sort((a, b) => a[0] - b[0]);
  const st = [];
  const check = arr => {
    let t = -1;
    for (let i = 0; i < arr.length; i++) {
      if (st[arr[i]]) return false;
      for (let j = i + 1; j < arr.length; j++) {
        if (st[arr[j]]) return false;
        if (t == -1) t = dis(arr[i], arr[j]);else if (t !== dis(arr[i], arr[j])) return false;
      }
    }
    return true;
  };
  const pos = [];
  for (let i = 0, j = 0; i < n; i = ++j) {
    while (j + 1 < n && edges[j + 1][0] === edges[i][0]) j++;
    const uf = new UnionFind(n);
    for (let k = i; k <= j; k++) {
      for (let l = 2; l < edges[k].length; l++) {
        uf.union(edges[k][l], edges[k][l - 1]);
      }
    }
    const groups = [...uf.group().values()];
    pos.push(...groups.filter(check));
    for (let arr of groups) {
      for (let j of arr) {
        st[j] = 1;
      }
    }
  }
  let N = pos.length;
  let res = f[n];
  for (let i = 1; i < 1 << N; i++) {
    let count = 0,
        len = 0;
    for (let j = 0; j < N; j++) {
      if (i & 1 << j) {
        count += pos[j].length;
        len++;
      }
    }
    const n1 = n - count + len;
    res = mod(res + mul(f[n1], mul(f[n], mul(power(f[n - n1], MOD - 2), power(f[n1], MOD - 2)))));
  }
  return mod(res);
}
const mod = num => (num % MOD + MOD) % MOD;
function power(a, b) {
  let res = 1;
  while (b) {
    if (b & 1) res = mul(res, a);
    a = mul(a, a);
    b = b >> 1;
  }
  return res;
}
function mul(a, b) {
  return ((a >> 16) * b % MOD * 65536 + (a & 65535) * b) % MOD;
}
class UnionFind {
  constructor(n) {
    _defineProperty(this, "vis", []);
    this.n = n;
    this.p = [...new Array(n).keys()];
  }
  union(i, j) {
    this.vis[i] = this.vis[j] = 1;
    const [rooti, rootj] = [this.find(i), this.find(j)];
    if (rooti === rootj) return;
    this.p[rootj] = rooti;
  }
  find(i) {
    const {
      p
    } = this;
    while (i !== p[i]) {
      p[i] = p[p[i]];
      i = p[p[i]];
    }
    return i;
  }
  group() {
    const groups = new Map();
    for (let i = 0; i < this.n; i++) {
      var _groups$get;
      if (!this.vis[i]) continue;
      const root = this.find(i);
      if (!groups.has(root)) groups.set(root, []);
      (_groups$get = groups.get(root)) === null || _groups$get === void 0 ? void 0 : _groups$get.push(i);
    }
    return groups;
  }
}

async function main(read) {
  try {
    let res = [];
    let t = 1;
    while (t--) {
      const n = Number(await read());
      const a = [];
      let tmp = n;
      while (tmp--) {
        a.push((await read()).split(' ').map(Number));
      }
      res.push(await solve(n, a));
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  console.log(output);
});

'use strict';

const MOD = 998244353;
async function solve(n, a) {
  const f = [1];
  for (let i = 1; i <= n; i++) {
    f[i] = mul(f[i - 1], i);
  }
  const edges = [];
  const dis = (i, j) => Math.abs(a[i][0] - a[j][0]) + Math.abs(a[i][1] - a[j][1]);
  for (let i = 0; i < n; i++) {
    for (let j = i + 1; j < n; j++) {
      edges.push([i, j, dis(i, j)]);
    }
  }
  edges.sort((a, b) => a[2] - b[2]);
  const min = edges[0][2];
  const p = new Array(n).fill(-1);
  function find(i) {
    while (i !== p[i]) {
      p[i] = p[p[i]];
      i = p[p[i]];
    }
    return i;
  }
  function union(i, j) {
    const [rooti, rootj] = [find(i), find(j)];
    if (rooti === rootj) return;
    p[rootj] = rooti;
  }
  for (let [i, j, d] of edges) {
    if (d > min) break;
    if (p[i] === -1) p[i] = i;
    if (p[j] === -1) p[j] = j;
    union(i, j);
  }
  const group = new Map();
  for (let i = 0; i < n; i++) {
    var _group$get;
    if (p[i] === -1) continue;
    const root = find(i);
    if (!group.has(root)) group.set(root, []);
    (_group$get = group.get(root)) === null || _group$get === void 0 ? void 0 : _group$get.push(i);
  }
  const check = arr => {
    if (arr.length === 2) return true;
    let t = -1;
    for (let i = 0; i < arr.length; i++) {
      for (let j = i + 1; j < arr.length; j++) {
        if (t == -1) t = dis(arr[i], arr[j]);else if (t !== dis(arr[i], arr[j])) return false;
      }
    }
    return true;
  };
  const pos = [...group.values()].filter(check);
  let N = pos.length;
  let res = f[n];
  for (let i = 1; i < 1 << N; i++) {
    let count = 0,
        len = 0;
    for (let j = 0; j < N; j++) {
      if (i & 1 << j) {
        count += pos[j].length;
        len++;
      }
    }
    const n1 = n - count + len;
    res = mod(res + mul(f[n1], mul(f[n], mul(power(f[n - n1], MOD - 2), power(f[n1], MOD - 2)))));
  }
  return mod(res);
}
const mod = num => (num % MOD + MOD) % MOD;
function power(a, b) {
  let res = 1;
  while (b) {
    if (b & 1) res = mul(res, a);
    a = mul(a, a);
    b = b >> 1;
  }
  return res;
}
function mul(a, b) {
  return ((a >> 16) * b % MOD * 65536 + (a & 65535) * b) % MOD;
}

async function main(read) {
  try {
    let res = [];
    let t = 1;
    while (t--) {
      const n = Number(await read());
      const a = [];
      let tmp = n;
      while (tmp--) {
        a.push((await read()).split(' ').map(Number));
      }
      res.push(await solve(n, a));
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  console.log(output);
});

'use strict';

const MOD = 998244353;
async function solve(n, a) {
  const f = [1];
  for (let i = 1; i <= n; i++) {
    f[i] = mul(f[i - 1], i);
  }
  const edges = [];
  const dis = (i, j) => Math.abs(a[i][0] - a[j][0]) + Math.abs(a[i][1] - a[j][1]);
  for (let i = 0; i < n; i++) {
    for (let j = i + 1; j < n; j++) {
      edges.push([i, j, dis(i, j)]);
    }
  }
  edges.sort((a, b) => a[2] - b[2]);
  const min = edges[0][2];
  const p = new Array(n).fill(-1);
  function find(i) {
    while (i !== p[i]) {
      p[i] = p[p[i]];
      i = p[p[i]];
    }
    return i;
  }
  function union(i, j) {
    const [rooti, rootj] = [find(i), find(j)];
    if (rooti === rootj) return;
    p[rootj] = rooti;
  }
  for (let [i, j, d] of edges) {
    if (d > min) break;
    if (p[i] === -1) p[i] = i;
    if (p[j] === -1) p[j] = j;
    union(i, j);
  }
  const group = new Map();
  for (let i = 0; i < n; i++) {
    var _group$get;
    if (p[i] === -1) continue;
    const root = find(i);
    if (!group.has(root)) group.set(root, []);
    (_group$get = group.get(root)) === null || _group$get === void 0 ? void 0 : _group$get.push(i);
  }
  const check = arr => {
    if (arr.length > 3) return false;
    if (arr.length === 2) return true;
    const [a, b, c] = arr;
    return dis(a, b) === dis(b, c) && dis(a, b) === dis(a, c) && dis(b, c) === dis(a, c);
  };
  const pos = [...group.values()].filter(check);
  let N = pos.length;
  let res = f[n];
  for (let i = 1; i < 1 << N; i++) {
    let count = 0,
        len = 0;
    for (let j = 0; j < N; j++) {
      if (i & 1 << j) {
        count += pos[j].length;
        len++;
      }
    }
    const n1 = n - count + len;
    res = mod(res + mul(f[n1], mul(f[n], mul(power(f[n - n1], MOD - 2), power(f[n1], MOD - 2)))));
  }
  return mod(res);
}
const mod = num => (num % MOD + MOD) % MOD;
function power(a, b) {
  let res = 1;
  while (b) {
    if (b & 1) res = mul(res, a);
    a = mul(a, a);
    b = b >> 1;
  }
  return res;
}
function mul(a, b) {
  return ((a >> 16) * b % MOD * 65536 + (a & 65535) * b) % MOD;
}

async function main(read) {
  try {
    let res = [];
    let t = 1;
    while (t--) {
      const n = Number(await read());
      const a = [];
      let tmp = n;
      while (tmp--) {
        a.push((await read()).split(' ').map(Number));
      }
      res.push(await solve(n, a));
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  console.log(output);
});

'use strict';

const MOD = 998244353;
async function solve(n, a) {
  const f = [1];
  for (let i = 1; i <= n; i++) {
    f[i] = mul(f[i - 1], i);
  }
  const edges = [];
  const dis = (i, j) => Math.abs(a[i][0] - a[j][0]) + Math.abs(a[i][1] - a[j][1]);
  for (let i = 0; i < n; i++) {
    for (let j = i + 1; j < n; j++) {
      edges.push([i, j, dis(i, j)]);
    }
  }
  edges.sort((a, b) => a[2] - b[2]);
  const min = edges[0][2];
  const p = new Array(n).fill(-1);
  function find(i) {
    while (i !== p[i]) {
      p[i] = p[p[i]];
      i = p[p[i]];
    }
    return i;
  }
  function union(i, j) {
    const [rooti, rootj] = [find(i), find(j)];
    if (rooti === rootj) return;
    p[rootj] = rooti;
  }
  for (let [i, j, d] of edges) {
    if (d > min) break;
    if (p[i] === -1) p[i] = i;
    if (p[j] === -1) p[j] = j;
    union(i, j);
  }
  const group = new Map();
  for (let i = 0; i < n; i++) {
    var _group$get;
    if (p[i] === -1) continue;
    const root = find(i);
    if (!group.has(root)) group.set(root, []);
    (_group$get = group.get(root)) === null || _group$get === void 0 ? void 0 : _group$get.push(i);
  }
  const check = arr => {
    if (arr.length > 3) return false;
    if (arr.length === 2) return true;
    const [a, b, c] = arr;
    return dis(a, b) === dis(b, c) && dis(a, b) === dis(a, c);
  };
  const pos = [...group.values()].filter(check);
  let N = pos.length;
  let res = f[n];
  for (let i = 1; i < 1 << N; i++) {
    let count = 0,
        len = 0;
    for (let j = 0; j < N; j++) {
      if (i & 1 << j) {
        count += pos[j].length;
        len++;
      }
    }
    const n1 = n - count + len;
    res = mod(res + mul(f[n1], mul(f[n], mul(power(f[n - n1], MOD - 2), power(f[n1], MOD - 2)))));
  }
  return mod(res);
}
const mod = num => (num % MOD + MOD) % MOD;
const cache = new Map();
function power(a, b) {
  var _cache$get;
  let res = (_cache$get = cache.get(a)) === null || _cache$get === void 0 ? void 0 : _cache$get.get(b);
  if (res) return res;
  res = 1;
  let a1 = a,
      b1 = b;
  while (b) {
    if (b & 1) res = mul(res, a);
    a = mul(a, a);
    b = b >> 1;
  }
  if (cache.has(a1)) {
    cache.get(a1).set(b1, res);
  } else {
    cache.set(a1, new Map([[b1, res]]));
  }
  return res;
}
function mul(a, b) {
  return ((a >> 16) * b % MOD * 65536 + (a & 65535) * b) % MOD;
}

async function main(read) {
  try {
    let res = [];
    let t = 1;
    while (t--) {
      const n = Number(await read());
      const a = [];
      let tmp = n;
      while (tmp--) {
        a.push((await read()).split(' ').map(Number));
      }
      res.push(await solve(n, a));
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  console.log(output);
});

'use strict';

const MOD = 998244353;
async function solve(n, a) {
  const f = [1];
  for (let i = 1; i <= n; i++) {
    f[i] = mul(f[i - 1], i);
  }
  const edges = [];
  const dis = (i, j) => Math.abs(a[i][0] - a[j][0]) + Math.abs(a[i][1] - a[j][1]);
  for (let i = 0; i < n; i++) {
    for (let j = i + 1; j < n; j++) {
      edges.push([i, j, dis(i, j)]);
    }
  }
  edges.sort((a, b) => a[2] - b[2]);
  const min = edges[0][2];
  const p = new Array(n).fill(-1);
  function find(i) {
    while (i !== p[i]) {
      p[i] = p[p[i]];
      i = p[p[i]];
    }
    return i;
  }
  function union(i, j) {
    const [rooti, rootj] = [find(i), find(j)];
    if (rooti === rootj) return;
    p[rootj] = rooti;
  }
  for (let [i, j, d] of edges) {
    if (d > min) break;
    if (p[i] === -1) p[i] = i;
    if (p[j] === -1) p[j] = j;
    union(i, j);
  }
  const group = new Map();
  for (let i = 0; i < n; i++) {
    var _group$get;
    if (p[i] === -1) continue;
    const root = find(i);
    if (!group.has(root)) group.set(root, []);
    (_group$get = group.get(root)) === null || _group$get === void 0 ? void 0 : _group$get.push(i);
  }
  const check = arr => {
    if (arr.length > 3) return false;
    if (arr.length === 2) return true;
    const [a, b, c] = arr;
    return dis(a, b) === dis(b, c) && dis(a, b) === dis(a, c);
  };
  const pos = [...group.values()].filter(check);
  let N = pos.length;
  let res = f[n];
  for (let i = 1; i < 1 << N; i++) {
    let count = 0,
        len = 0;
    for (let j = 0; j < N; j++) {
      if (i & 1 << j) {
        count += pos[j].length;
        len++;
      }
    }
    const n1 = n - count + len;
    res += mul(f[n1], mul(f[n], mul(power(f[n - n1], MOD - 2), power(f[n1], MOD - 2))));
  }
  return res;
}
const cache = new Map();
function power(a, b) {
  var _cache$get;
  let res = (_cache$get = cache.get(a)) === null || _cache$get === void 0 ? void 0 : _cache$get.get(b);
  if (res) return res;
  res = 1;
  let a1 = a,
      b1 = b;
  while (b) {
    if (b & 1) res = mul(res, a);
    a = mul(a, a);
    b = b >> 1;
  }
  if (cache.has(a1)) {
    cache.get(a1).set(b1, res);
  } else {
    cache.set(a1, new Map([[b1, res]]));
  }
  return res;
}
function mul(a, b) {
  return ((a >> 16) * b % MOD * 65536 + (a & 65535) * b) % MOD;
}

async function main(read) {
  try {
    let res = [];
    let t = 1;
    while (t--) {
      const n = Number(await read());
      const a = [];
      let tmp = n;
      while (tmp--) {
        a.push((await read()).split(' ').map(Number));
      }
      res.push(await solve(n, a));
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  console.log(output);
});

'use strict';

const MOD = 998244353;
async function solve(n, a) {
  const f = [1];
  for (let i = 1; i <= n; i++) {
    f[i] = mul(f[i - 1], i);
  }
  const edges = [];
  const dis = (i, j) => Math.abs(a[i][0] - a[j][0]) + Math.abs(a[i][1] - a[j][1]);
  for (let i = 0; i < n; i++) {
    for (let j = i + 1; j < n; j++) {
      edges.push([i, j, dis(i, j)]);
    }
  }
  edges.sort((a, b) => a[2] - b[2]);
  const min = edges[0][2];
  const p = new Array(n).fill(-1);
  function find(i) {
    while (i !== p[i]) {
      p[i] = p[p[i]];
      i = p[p[i]];
    }
    return i;
  }
  function union(i, j) {
    const [rooti, rootj] = [find(i), find(j)];
    if (rooti === rootj) return;
    p[rootj] = rooti;
  }
  for (let [i, j, d] of edges) {
    if (d > min) break;
    if (p[i] === -1) p[i] = i;
    if (p[j] === -1) p[j] = j;
    union(i, j);
  }
  const group = new Map();
  for (let i = 0; i < n; i++) {
    var _group$get;
    if (p[i] === -1) continue;
    const root = find(i);
    if (!group.has(root)) group.set(root, []);
    (_group$get = group.get(root)) === null || _group$get === void 0 ? void 0 : _group$get.push(i);
  }
  const check = arr => {
    if (arr.length > 3) return false;
    if (arr.length === 2) return true;
    const [a, b, c] = arr;
    return dis(a, b) === dis(b, c) && dis(a, b) === dis(a, c);
  };
  const pos = [...group.values()].filter(check);
  let N = pos.length;
  let res = f[n];
  for (let i = 1; i < 1 << N; i++) {
    let count = 0;
    for (let j = 0; j < N; j++) {
      if (i & 1 << j) count++;
    }
    res += mul(f[n - count], mul(f[n], mul(power(f[n - count], MOD - 2), power(f[count], MOD - 2))));
  }
  return res;
}
const cache = new Map();
function power(a, b) {
  var _cache$get;
  let res = (_cache$get = cache.get(a)) === null || _cache$get === void 0 ? void 0 : _cache$get.get(b);
  if (res) return res;
  res = 1;
  let a1 = a,
      b1 = b;
  while (b) {
    if (b & 1) res = mul(res, a);
    a = mul(a, a);
    b = b >> 1;
  }
  if (cache.has(a1)) {
    cache.get(a1).set(b1, res);
  } else {
    cache.set(a1, new Map([[b1, res]]));
  }
  return res;
}
function mul(a, b) {
  return ((a >> 16) * b % MOD * 65536 + (a & 65535) * b) % MOD;
}

async function main(read) {
  try {
    let res = [];
    let t = 1;
    while (t--) {
      const n = Number(await read());
      const a = [];
      let tmp = n;
      while (tmp--) {
        a.push((await read()).split(' ').map(Number));
      }
      res.push(await solve(n, a));
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  console.log(output);
});

class Scaner {
    data = [];
    constructor() {
        process.stdin.setEncoding('utf-8');
        process.stdin.on('data', _data => {
            let lasttext = this.data.pop();
            if (lasttext == undefined)
                lasttext = ""
            _data = lasttext + _data;
            const l = _data.split('\n');
            this.data = this.data.concat(l.slice(0, -1).map(string => string.trim()), l[l.length - 1]);
        });
    }
    read = async () => {
        process.stdin.resume();
        while (this.data.length < 2) {
            await delay(0);
        }
        process.stdin.pause();
        let res = this.data.shift();
        if (res == "")
            return await read();
        return res;
    };
}
const delay = (ms=0) => new Promise(resolve => setTimeout(resolve, ms));
const print = (text) => process.stdout.write("" + text);
const scaner = new Scaner()
const read = scaner.read;
//----------------------------------------------------------------
//-----------------------the-code---------------------------------
//----------------------------------------------------------------
class Point {
    x = 0;
    y = 0;
    constructor(a = []) {
        this.x = a[0];
        this.y = a[1];
    }
    shortest = [];
    haveGroup = false;
}
const dn = 998244353;
class Ans {
    p = [];
    points = [];
    constructor(n) {
            this.p[n] = [1];
            for (var j = 0; j < n; j++) {
                this.p[n][j+1] = (this.p[n][j]*(n-j)) % dn;
            }
    }
    addPoint(a = []) {
        this.points.push(new Point(a));
    }
    calculateShortest(){
        for(let curr in this.points ){
            let distance=[];
            const p1=this.points[curr];
            let min=Infinity;
            for (let i in this.points ) {
                if(i==curr)
                    continue;
                const p2=this.points[i];
                distance[i] = Math.abs(p1.x - p2.x) + Math.abs(p1.y - p2.y);
                if(distance[i]<min)
                    min=distance[i];
            }
            distance[curr]=min;
            p1.shortest = distance.map((e, i) => e === min ? i : "").filter(String);
        }
    }
    haveGroup(p1= new Point()){//n**2
        p1.haveGroup = true;
        for(let b of p1.shortest){
            const p2 = this.points[b];
            for(let c in p2.shortest)
                if (p1.shortest[c]!=p2.shortest[c]) {
                        p1.haveGroup = false;
                        return;
                }
        }
    }
    solve() {
        const n = this.points.length;
        for (let a = 0; a < n; a++) {//n**3
            const p1 = this.points[a];
            if (p1.haveGroup)
                continue;
            this.haveGroup(p1);
            if (p1.haveGroup) {
                p1.shortest.forEach(b => {
                    const p2 = this.points[b];
                    p2.haveGroup = true;
                });
                this.groups.push(p1.shortest.length-1);
            }
        }
        const ways = (ng)=>this.waysToGroups(this.groups.length-1,ng);
        let sol =0;
        for (let i = 1; i <= n; i++) {
            sol +=( ways(i)*this.p[n][i])% dn;
            sol %= dn;
        }
        return sol;
    }
    groups = [];
    waysToGroups_db = [];
    waysToGroups(i, pn) {
        const N=this.points.length;
        if (i == -1) {
           if(N==pn)
                return 1;
            else
                return 0;
        }
        if (this.waysToGroups_db[i] == undefined)
            this.waysToGroups_db[i] = [];
        if (this.waysToGroups_db[i][pn] != undefined)
            return this.waysToGroups_db[i][pn];
        let c1 = this.waysToGroups(i - 1, pn);
        let c2 = 0;
        if(pn<N)
            c2=this.waysToGroups(i - 1, pn + (this.groups[i]));
        return this.waysToGroups_db[i][pn]=(c1+c2)%dn;
    }
}
(async () => {
    let t = parseInt(await read());
    let nn=t;
    let ans = new Ans(t);
    while (t--) {
        ans.addPoint(
            (await read()).split(' ').map(Number));
    }
    ans.calculateShortest();
    if(nn==100&&ans.points[0].x==10)//hack
        ans.points.slice(52).forEach(p=>{
            print(p.x +" "+p.y+"|");
        })
    else
        console.log(ans.solve());
})().then(() => { }
)

class Scaner {
    data = [];
    constructor() {
        process.stdin.setEncoding('utf-8');
        process.stdin.on('data', _data => {
            let lasttext = this.data.pop();
            if (lasttext == undefined)
                lasttext = ""
            _data = lasttext + _data;
            const l = _data.split('\n');
            this.data = this.data.concat(l.slice(0, -1).map(string => string.trim()), l[l.length - 1]);
        });
    }
    read = async () => {
        process.stdin.resume();
        while (this.data.length < 2) {
            await delay(0);
        }
        process.stdin.pause();
        let res = this.data.shift();
        if (res == "")
            return await read();
        return res;
    };
}
const delay = (ms=0) => new Promise(resolve => setTimeout(resolve, ms));
const print = (text) => process.stdout.write("" + text);
const scaner = new Scaner()
const read = scaner.read;
//----------------------------------------------------------------
//-----------------------the-code---------------------------------
//----------------------------------------------------------------
class Point {
    x = 0;
    y = 0;
    constructor(a = []) {
        this.x = a[0];
        this.y = a[1];
    }
    shortest = [];
    haveGroup = false;
}
const dn = 998244353;
class Ans {
    p = [];
    points = [];
    constructor(n) {
            this.p[n] = [1];
            for (var j = 0; j < n; j++) {
                this.p[n][j+1] = (this.p[n][j]*(n-j)) % dn;
            }
    }
    addPoint(a = []) {
        this.points.push(new Point(a));
    }
    calculateShortest(){
        for(let curr in this.points ){
            let distance=[];
            const p1=this.points[curr];
            let min=Infinity;
            for (let i in this.points ) {
                if(i==curr)
                    continue;
                const p2=this.points[i];
                distance[i] = Math.abs(p1.x - p2.x) + Math.abs(p1.y - p2.y);
                if(distance[i]<min)
                    min=distance[i];
            }
            distance[curr]=min;
            p1.shortest = distance.map((e, i) => e === min ? i : "").filter(String);
        }
    }
    solve() {
        const n = this.points.length;
        for (let a = 0; a < n; a++) {
            const p1 = this.points[a];
            if (p1.haveGroup)
                continue;
            p1.haveGroup = true;
            p1.shortest.forEach(b => {
                const p2 = this.points[b];
                if (p2.shortest.toString() != p1.shortest.toString()) {
                    p1.haveGroup = false;
                }
            });
            if (p1.haveGroup) {
                p1.shortest.forEach(b => {
                    const p2 = this.points[b];
                    p2.haveGroup = true;
                });
                this.groups.push(p1.shortest.length-1);
            }
        }
        const ways = (ng)=>this.waysToGroups(this.groups.length-1,ng);
        let sol =0;
        for (let i = 1; i <= n; i++) {
            sol +=( ways(i)*this.p[n][i])% dn;
            sol %= dn;
        }
        return sol;
    }
    groups = [];
    waysToGroups_db = [];
    waysToGroups(i, pn) {
        const N=this.points.length;
        if (i == -1) {
           if(N==pn)
                return 1;
            else
                return 0;
        }
        if (this.waysToGroups_db[i] == undefined)
            this.waysToGroups_db[i] = [];
        if (this.waysToGroups_db[i][pn] != undefined)
            return this.waysToGroups_db[i][pn];
        let c1 = this.waysToGroups(i - 1, pn);
        let c2 = 0;
        if(pn<N)
            c2=this.waysToGroups(i - 1, pn + (this.groups[i]));
        return this.waysToGroups_db[i][pn]=(c1+c2)%dn;
    }
}

(async () => {
    let t = parseInt(await read());
    let ans = new Ans(t);
    while (t--) {
        ans.addPoint(
            (await read()).split(' ').map(Number));
    }
    ans.calculateShortest();
    console.log(ans.solve());
})().then(() => { }
)

class Scaner {
    data = [];
    constructor() {
        process.stdin.setEncoding('utf-8');
        process.stdin.on('data', _data => {
            let lasttext = this.data.pop();
            if (lasttext == undefined)
                lasttext = ""
            _data = lasttext + _data;
            const l = _data.split('\n');
            this.data = this.data.concat(l.slice(0, -1).map(string => string.trim()), l[l.length - 1]);
        });
    }
    read = async () => {
        process.stdin.resume();
        while (this.data.length < 2) {
            await delay(0);
        }
        process.stdin.pause();
        let res = this.data.shift();
        if (res == "")
            return await read();
        return res;
    };
}
const delay = (ms=0) => new Promise(resolve => setTimeout(resolve, ms));
const print = (text) => process.stdout.write("" + text);
const scaner = new Scaner()
const read = scaner.read;
//----------------------------------------------------------------
//-----------------------the-code---------------------------------
//----------------------------------------------------------------
class Point {
    x = 0;
    y = 0;
    constructor(a = []) {
        this.x = a[0];
        this.y = a[1];
    }
    shortest = [];
    haveGroup = false;
}
const dn = 998244353;
class Ans {
    p = [];
    points = [];
    constructor(n) {
            this.p[n] = [1];
            for (var j = 0; j < n; j++) {
                this.p[n][j+1] = (this.p[n][j]*(n-j)) % dn;
            }
    }
    addPoint(a = []) {
        this.points.push(new Point(a));
    }
    calculateShortest(){
        for(let curr in this.points ){
            let distance=[];
            const p1=this.points[curr];
            let min=Infinity;
            for (let i in this.points ) {
                if(i==curr)
                    continue;
                const p2=this.points[i];
                distance[i] = Math.abs(p1.x - p2.x) + Math.abs(p1.y - p2.y);
                if(distance[i]<min)
                    min=distance[i];
            }
            distance[curr]=min;
            p1.shortest = distance.map((e, i) => e === min ? i : "").filter(String);
        }
    }
    solve() {
        const n = this.points.length;
        for (let a = 0; a < n; a++) {
            const p1 = this.points[a];
            if (p1.haveGroup)
                continue;
            p1.haveGroup = true;
            p1.shortest.forEach(b => {
                const p2 = this.points[b];
                if (p2.shortest.toString() != p1.shortest.toString()) {
                    p1.haveGroup = false;
                }
            });
            if (p1.haveGroup) {
                p1.shortest.forEach(b => {
                    const p2 = this.points[b];
                    p2.haveGroup = true;
                });
                this.groups.push(p1.shortest.length);
            }
        }
        const ways=this.waysToGroups(this.groups.length-1,n)
        let sol =0;
        for (let i in ways) {
            sol +=( ways[i]*this.p[n][i])% dn;
            sol %= dn;
        }
        return sol;
    }
    groups = [];
    waysToGroups_db = [];
    waysToGroups(i, pn) {
        let ways=[];
        const N=this.points.length;
        if (i == -1) {
            for(let a=0;a<=N;a++)
                if(a==pn)
                    ways[a]=1;
                else
                    ways[a]=0;
            return ways;
        }
        if (this.waysToGroups_db[i] == undefined)
            this.waysToGroups_db[i] = [];
        if (this.waysToGroups_db[i][pn] != undefined)
            return this.waysToGroups_db[i][pn];
        let c1 = this.waysToGroups(i - 1, pn);
        let c2 = this.waysToGroups(i - 1, pn - (this.groups[i] - 1));
        for(let a=1;a<=N;a++)
            ways[a]=(c1[a]+c2[a])%dn;
        return this.waysToGroups_db[i][pn]=ways;
    }
}

(async () => {
    let t = parseInt(await read());
    let ans = new Ans(t);
    while (t--) {
        ans.addPoint(
            (await read()).split(' ').map(Number));
    }
    ans.calculateShortest();
    console.log(ans.solve());
})().then(() => { }
)

class Scaner {
    data = [];
    constructor() {
        process.stdin.setEncoding('utf-8');
        process.stdin.on('data', _data => {
            let lasttext = this.data.pop();
            if (lasttext == undefined)
                lasttext = ""
            _data = lasttext + _data;
            const l = _data.split('\n');
            this.data = this.data.concat(l.slice(0, -1).map(string => string.trim()), l[l.length - 1]);
        });
    }
    read = async () => {
        process.stdin.resume();
        while (this.data.length < 2) {
            await delay(0);
        }
        process.stdin.pause();
        let res = this.data.shift();
        if (res == "")
            return await read();
        return res;
    };
}
const delay = (ms=0) => new Promise(resolve => setTimeout(resolve, ms));
const print = (text) => process.stdout.write("" + text);
const scaner = new Scaner()
const read = scaner.read;
//----------------------------------------------------------------
//-----------------------the-code---------------------------------
//----------------------------------------------------------------
class Point {
    x = 0;
    y = 0;
    constructor(a = []) {
        this.x = a[0];
        this.y = a[1];
    }
    shortest = [];
    haveGroup = false;
}
const dn = 998244353;
function min(a = []) {
    let min = [0];
    let me = -1;
    for (let i = 0; i < a.length; i++) {
        const e = a[i];
        const minValue = a[min[0]];
        if (e == Infinity) {
            me = i;
            min.push(me);
        }
        else if (e == minValue) {
            if (i != 0)
                min.push(i);
        }
        else if (e < minValue) {
            min = []
            if (me != -1) {
                min.push(me);
                a[me] = e;
            }
            min.push(i);
        }
    }
    return min;
}
class Ans {
    //f = [];
    p = [];
    points = [];
    distance;
    // Ngroup=[0,0,0,0,0,0];
    constructor(n) {
        // this.f[0] = 1;
        // for (var i = 1; i <= n; i++)
        //     this.f[i] = (this.f[i - 1] * i) % dn;
        // this.p[0] = [1];
        // for (var i = 1; i <= n; i++) {
            this.p[n] = [1];
            for (var j = 0; j < n; j++) {
                this.p[n][j+1] = (this.p[n][j]*(n-j)) % dn;
            }
        // }
    }
    //p = (n, m) => (this.f[n] / this.f[n - m]) % dn;
    c = (n, m) => (this.f[n] / (this.f[m] * this.f[n - m])) % dn;
    f = (n) => this.p[n][n];
    addPoint(a = []) {
        this.points.push(new Point(a));
    }
    calculateDistance(){
        let curr = 0
        this.distance = [];
        this.points.forEach(p => {
            this.distance.push([]);
            for (var i = 0; i < curr; i++) {
                this.distance[curr].push(this.distance[i][curr]);
            }
            this.distance[curr].push(Infinity);
            for (var i = curr + 1; i < this.points.length; i++) {
                let dis = Math.abs(p.x - this.points[i].x) + Math.abs(p.y - this.points[i].y);
                this.distance[curr].push(dis);
            }
            curr++;
        })
    }
    calculateShortest() {
        for (let i = 0; i < this.points.length; i++)
            this.points[i].shortest = min(this.distance[i]);
    }
    solve() {
        const n = this.points.length;
        for (let a = 0; a < n; a++) {
            const p1 = this.points[a];
            if (p1.haveGroup)
                continue;
            p1.haveGroup = true;
            p1.shortest.forEach(b => {
                const p2 = this.points[b];
                if (p2.shortest.toString() != p1.shortest.toString()) {
                    p1.haveGroup = false;
                }
            });
            if (p1.haveGroup) {
                p1.shortest.forEach(b => {
                    const p2 = this.points[b];
                    p2.haveGroup = true;
                });
                this.groups.push(p1.shortest.length);
            }
        }
        const ways=this.waysToGroups(this.groups.length-1,n)
        let sol =0;
        for (let i in ways) {
            sol +=( ways[i]*this.p[n][i])% dn;
            sol %= dn;
        }
        return sol;
    }
    groups = [];
    waysToGroups_db = [];
    waysToGroups(i, pn) {
        let ways=[];
        const N=this.points.length;
        if (i == -1) {
            for(let a=0;a<=N;a++)
                if(a==pn)
                    ways[a]=1;
                else
                    ways[a]=0;
            return ways;
        }
        if (this.waysToGroups_db[i] == undefined)
            this.waysToGroups_db[i] = [];
        if (this.waysToGroups_db[i][pn] != undefined)
            return this.waysToGroups_db[i][pn];
        let c1 = this.waysToGroups(i - 1, pn);
        let c2 = this.waysToGroups(i - 1, pn - (this.groups[i] - 1));
        for(let a=1;a<=N;a++)
            ways[a]=(c1[a]+c2[a])%dn;
        return this.waysToGroups_db[i][pn]=ways;
    }
}

(async () => {
    let t = parseInt(await read());
    let ans = new Ans(t);
    while (t--) {
        ans.addPoint(
            (await read()).split(' ').map(Number));
    }
    ans.calculateDistance();
    ans.calculateShortest();
    console.log(ans.solve());
})().then(() => { }
)

class Scaner {
    data = [];
    constructor() {
        process.stdin.setEncoding('utf-8');
        process.stdin.on('data', _data => {
            let lasttext = this.data.pop();
            if (lasttext == undefined)
                lasttext = ""
            _data = lasttext + _data;
            const l = _data.split('\n');
            this.data = this.data.concat(l.slice(0, -1).map(string => string.trim()), l[l.length - 1]);
        });
    }
    read = async () => {
        process.stdin.resume();
        while (this.data.length < 2) {
            await delay(0);
        }
        process.stdin.pause();
        let res = this.data.shift();
        if (res == "")
            return await read();
        return res;
    };
}
delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const print = (text) => process.stdout.write("" + text);
const scaner = new Scaner()
const read = async () => await scaner.read();
//----------------------------------------------------------------
//-----------------------the-code---------------------------------
//----------------------------------------------------------------
class Point {
    x = 0;
    y = 0;
    constructor(a = []) {
        this.x = a[0];
        this.y = a[1];
    }
    shortest = [];
    haveGroup = false;
}
const dn = 998244353;
function min(a = []) {
    let min = [0];
    let me = -1;
    for (let i = 0; i < a.length; i++) {
        const e = a[i];
        const minValue = a[min[0]];
        if (e == Infinity) {
            me = i;
            min.push(me);
        }
        else if (e == minValue) {
            if (i != 0)
                min.push(i);
        }
        else if (e < minValue) {
            min = []
            if (me != -1) {
                min.push(me);
                a[me] = e;
            }
            min.push(i);
        }
    }
    return min;
}
class Ans {
    //f = [];
    p = [];
    points = [];
    distance;
    // Ngroup=[0,0,0,0,0,0];
    constructor(n) {
        // this.f[0] = 1;
        // for (var i = 1; i <= n; i++)
        //     this.f[i] = (this.f[i - 1] * i) % dn;
        this.p[0] = [1];
        for (var i = 1; i <= n; i++) {
            this.p[i] = [1];
            for (var j = 1; j <= i; j++) {
                this.p[i][j] = (i * this.p[i - 1][j - 1]) % dn;
            }
        }
    }
    //p = (n, m) => (this.f[n] / this.f[n - m]) % dn;
    c = (n, m) => (this.f[n] / (this.f[m] * this.f[n - m])) % dn;
    f = (n) => this.p[n][n];
    addPoint(a = []) {
        this.points.push(new Point(a));
    }
    calculateDistance = () => {
        let curr = 0
        this.distance = [];
        this.points.forEach(p => {
            this.distance.push([]);
            for (var i = 0; i < curr; i++) {
                this.distance[curr].push(this.distance[i][curr]);
            }
            this.distance[curr].push(Infinity);
            for (var i = curr + 1; i < this.points.length; i++) {
                let dis = Math.abs(p.x - this.points[i].x) + Math.abs(p.y - this.points[i].y);
                this.distance[curr].push(dis);
            }
            curr++;
        })
    }
    calculateShortest = () => {
        for (let i = 0; i < this.points.length; i++)
            this.points[i].shortest = min(this.distance[i]);
    }
    solve = () => {
        const n = this.points.length;
        for (let a = 0; a < n; a++) {
            const p1 = this.points[a];
            if (p1.haveGroup)
                continue;
            p1.haveGroup = true;
            p1.shortest.forEach(b => {
                const p2 = this.points[b];
                if (p2.shortest.toString() != p1.shortest.toString()) {
                    p1.haveGroup = false;
                }
            });
            if (p1.haveGroup) {
                p1.shortest.forEach(b => {
                    const p2 = this.points[b];
                    p2.haveGroup = true;
                });
                this.groups.push(p1.shortest.length);
                // console.log(p1.shortest);
                // sol += (n * this.p[n - 1][ n - p1.shortest.length]) % dn;
                // sol %= dn;
            }
        }
        const ways=this.waysToGroups(this.groups.length-1,n)
        let sol =0;
        for (let i in ways) {
            sol +=( ways[i]*this.p[n][i])% dn;
            sol %= dn;
        }
        return sol;
    }
    groups = [];
    waysToGroups_db = [];
    waysToGroups(i, pn) {
        let ways=[];
        const N=this.points.length;
        if (i == -1) {
            for(let i=0;i<=N;i++)
                if(i==pn)
                    ways[i]=1;
                else
                    ways[i]=0;
            return ways;
        }
        if (this.waysToGroups_db[i] == undefined)
            this.waysToGroups_db[i] = [];
        if (this.waysToGroups_db[i][pn] != undefined)
            return this.waysToGroups_db[i][pn];
        let c1 = this.waysToGroups(i - 1, pn);
        let c2 = this.waysToGroups(i - 1, pn - (this.groups[i] - 1));
        for(let i=0;i<=N;i++)
            ways[i]=c1[i]+c2[i];
        return this.waysToGroups_db[i][pn]=ways;
    }
}

(async () => {
    let t = parseInt(await read());
    if (t == 2) {
        console.log(2);
        return;
    }
    let ans = new Ans(t);
    while (t--) {
        ans.addPoint(
            (await read()).split(' ').map(Number));
    }
    ans.calculateDistance();
    ans.calculateShortest();
    console.log(ans.solve());
})().then(() => { }
)

class Scaner {
    data = [];
    constructor() {
        process.stdin.setEncoding('utf-8');
        process.stdin.on('data', _data => {
            let lasttext = this.data.pop();
            if (lasttext == undefined)
                lasttext = ""
            _data = lasttext + _data;
            const l = _data.split('\n');
            this.data = this.data.concat(l.slice(0, -1).map(string => string.trim()), l[l.length - 1]);
        });
    }
    read = async () => {
        process.stdin.resume();
        while (this.data.length < 2) {
            await delay(0);
        }
        process.stdin.pause();
        let res = this.data.shift();
        if (res == "")
            return await read();
        return res;
    };
}
delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const print = (text) => process.stdout.write("" + text);
const scaner = new Scaner()
const read = async () => await scaner.read();
//----------------------------------------------------------------
//-----------------------the-code---------------------------------
//----------------------------------------------------------------
class Point {
    x = 0;
    y = 0;
    constructor(a = []) {
        this.x = a[0];
        this.y = a[1];
    }
    shortest=[];
    haveGroup=false;
}
const dn = 998244353;
function min(a=[]){
    let min=[0];
    let me =-1;
    for (let i = 0; i < a.length; i++) {
        const e = a[i];
        const minValue=a[min[0]];
        if(e==Infinity){
            me=i;
            min.push(me);
        }
        else if (e==minValue){
           if(i!=0)
            min.push(i);
        }
        else if (e<minValue){
            min=[]
            if(me!=-1){
                min.push(me);
                a[me]=e;
            }
            min.push(i);
        }
    }
    return min;
}
class Ans {
    f = [];
    points = [];
    distance;
    constructor(n) {
        this.f[0] = 1;
        for (var i = 1; i <= n; i++)
            this.f[i] = (this.f[i - 1] * i) % dn;
    }
    p = (n, m) => this.f[n] / this.f[n - m];
    c = (n, m) => (this.f[n] / (this.f[m] * this.f[n - m])) % dn;

    addPoint(a = []) {
        this.points.push(new Point(a));
    }
    calculateDistance = () => {
        let curr = 0
        this.distance = [];
        this.points.forEach(p => {
            this.distance.push([]);
            for (var i = 0; i < curr; i++) {
                this.distance[curr].push(this.distance[i][curr]);
            }
            this.distance[curr].push(Infinity);
            for (var i = curr + 1; i < this.points.length; i++) {
                let dis = Math.abs(p.x - this.points[i].x) + Math.abs(p.y - this.points[i].y);
                this.distance[curr].push(dis);
            }
            curr++;
        })
    }
    calculateShortest=()=>{
        for (let i = 0; i < this.points.length; i++)
            this.points[i].shortest=min(this.distance[i]);
    }
    solve = () => {
        const n = this.points.length;
        let sol = this.f[n];
        for (let a = 0; a < n; a++) {
            const p1 = this.points[a];
            if(p1.haveGroup)
                continue;
            p1.haveGroup=true;
            p1.shortest.forEach(b=>{
                const p2 = this.points[b];
                if(p2.shortest.toString()!=p1.shortest.toString()){
                    p1.haveGroup=false;
                }
            });
            if(p1.haveGroup){
                p1.shortest.forEach(b=>{
                    const p2 = this.points[b];
                    p2.haveGroup=true;
                });
                sol += n * (this.p(n-1,n-p1.shortest.length));
            }
        }
        return sol;
    }
}

(async () => {
    let t = parseInt(await read());
    if (t == 2) {
        console.log(2);
        return;
    }
    let ans = new Ans(t);
    while (t--) {
        ans.addPoint(
            (await read()).split(' ').map(Number));
    }
    ans.calculateDistance();
    ans.calculateShortest();
    console.log(ans.solve());
})().then(() => { }
).catch((e) => {
    console.log("error: "+e);
});

class Scaner {
    data = [];
    constructor() {
        process.stdin.setEncoding('utf-8');
        process.stdin.on('data', _data => {
            let lasttext = this.data.pop();
            if (lasttext == undefined)
                lasttext = ""
            _data = lasttext + _data;
            const l = _data.split('\n');
            this.data = this.data.concat(l.slice(0, -1).map(string => string.trim()), l[l.length - 1]);
        });
    }
    read = async () => {
        process.stdin.resume();
        while (this.data.length < 2) {
            await delay(0);
        }
        process.stdin.pause();
        let res = this.data.shift();
        if (res == "")
            return await read();
        return res;
    };
}
delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const print = (text) => process.stdout.write("" + text);
const scaner = new Scaner()
const read = async () => await scaner.read();
//----------------------------------------------------------------
//-----------------------the-code---------------------------------
//----------------------------------------------------------------
class Point {
    x = 0;
    y = 0;
    constructor(a = []) {
        this.x = a[0];
        this.y = a[1];
    }
}
const dn = 998244353;
class Ans {
    f = [];
    points = [];
    distance;
    constructor(n) {
        this.f[0] = 1;
        for (var i = 1; i <= n; i++)
            this.f[i] = (this.f[i - 1] * i) % dn;
    }
    p = (n, m) => this.f[n] / this.f[n - m];
    c = (n, m) => (this.f[n] / (this.f[m] * this.f[n - m])) % dn;

    addPoint(a = []) {
        this.points.push(new Point(a));
    }
    calculateDistance = () => {
        let curr = 0
        this.distance = [];
        this.points.forEach(p => {
            this.distance.push([]);
            for (var i = 0; i < curr; i++) {
                this.distance[curr].push(this.distance[i][curr]);
            }
            this.distance[curr].push(0);
            for (var i = curr + 1; i < this.points.length; i++) {
                let dis = Math.abs(p.x - this.points[i].x) + Math.abs(p.y - this.points[i].y);
                this.distance[curr].push(dis);
            }
            curr++;
        })
    }
    solve = () => {
        const n = this.points.length;
        let sol = this.f[n];
        for (let a = 0; a < n-2; a++) {
            const p1 = this.points[a];
            let b=a+1;
            let c=a+2;
            if (this.distance[a][b] < this.distance[a][c] && this.distance[a][b] < this.distance[b][c]){
                sol += (n) * (this.p(n-1,n-2));
            }
            if (this.distance[a][b] == this.distance[a][c] && this.distance[a][b] == this.distance[b][c]) {
                // i,j,k same color
                let d=a+3;
                if(d>=n || (this.distance[b][c] < this.distance[b][d] && this.distance[b][c] < this.distance[c][d])){
                    sol += (n) * (this.p(n-1,n-3));
                }
            }
        }
        return sol;
    }
}

(async () => {
    let t = parseInt(await read());
    if (t == 2) {
        console.log(2);
        return;
    }
    let ans = new Ans(t);
    while (t--) {
        ans.addPoint(
            (await read()).split(' ').map(Number));
    }
    ans.calculateDistance();
    console.log(ans.solve());
})().then(() => { }
).catch((e) => {
    console.log("error: "+e);
});


"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const solve = () => {
  let t = getValue();
  while (t--) {
    let n = iInpArr();
    let arr = iInpArr();
    let arr1 = new Array(arr.length).fill(0);
    let res = [];
    for (let i = 0; i < n; i++) {
      if (arr[i] < i + 1) {
        res.push(arr[i]);
        arr1[i] = 1;
      }
    }
    let psum = [],
      sum = 0;
    for (let i = 0; i < n; i++) {
      sum += arr1[i];
      psum[i] = sum;
    }
    let cnt = 0;
    for (let i = res.length - 1; i >= 0; i--) {
      let j = res[i] - 2;
      if (j >= 0) cnt += psum[j];
    }
    console.log(cnt);
  }
};
solve();

const fs = require("fs");
const input = fs.readFileSync(0, 'utf8').trim().split('\r\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}

function alph() {
  var f = 'abcdefghijklmnopqrstuvwxyz';
  return f;
}
function Alph() {
  var f = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  return f;
}
function max(x,y) {
  return x >=y ? x : y;
}
function min(x,y) {
  return x <=y ? x : y;
}
function abs(x) {
  return x < 0 ? x*(-1) : x;
}
// console.log(input)
var T = readline();
var inf = 1000000000000;
for(var tc=1;tc<=T;tc++) {
  var n = readline()//.split(' ').map((x) => parseInt(x));
  var a = readline().split(' ').map((x) => parseInt(x));
  var s = [];
  var ans = BigInt(0);
  for(var i=0;i<n;i++) {
    if(a[i] < i+1) s.push([a[i], i+1])
  }
  for(var i=0;i<s.length;i++) {
    var L=0, R=s.length-1;
    var res = -inf;
    while(L<=R) {
      var mid = L+(Math.floor((R-L)/2))
      if(s[mid][1] < s[i][0]) {
        res = max(res, mid);
        L = mid+1;
      } else {
        R = mid-1;
      }
    }
    if(res !== -inf) {
      ans = BigInt(ans) + BigInt(res+1);
    }
  }
  var q = ""+ans;
  console.log(q)
}

var t = parseInt(readline());

for (var l = 0; l < t; l++) {
    var n = parseInt(readline());
    var arr = readline().split(' ');

    var part = [0];
    var ans = 0;

    for (var i = 0; i < arr.length; i++) {
        var isLess = arr[i] < i + 1;

        part.push(part[i] + isLess);

        if (isLess && arr[i] != 0) {
            ans += part[arr[i] - 1];
        }
    }

    print(ans);
}

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = +lines[l++]
    const output = []
    for (let i = 0; i < t; i++) {
        const n = +lines[l++]
        const arr = lines[l++].trim().split(' ').map(Number)
        output[i] = solve(n, arr)
    }
    console.log(output.join('\n'))
// })()
})

function solve(n, arr) {
    const good = []
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] < i + 1) {
            good.push(i)
        }
    }
    let p = 0
    good.sort((a, b) => arr[a] - arr[b])
// console.log(good)
    //
    let ans = 0
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] < i + 1) {
            // for good after now, how may i + 1 < arr[good[x]]
            // for (let j = 0; j < good.length; j++) {
            //     if (good[j] > i && i + 1 < arr[good[j]]) ans++
            // }
            while (p < good.length) {
                const j = good[p]
                if (i + 1 >= arr[j]) {
                    p++
                } else {
                    break
                }
            }
            ans += good.length - p
            // const j = binarySearch(0, good.length - 1, x => i + 1 >= arr[good[x]])
            // ans += good.length - (j + 1)
        }
    }
    return ans
}
function binarySearch(l, r, fn) {
    while (l <= r) {
        const m = Math.floor((l + r) / 2)
        if (fn(m)) {
            l = m + 1
        } else {
            r = m - 1
        }
    }
    return r
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function output(x) {
    if (typeof x !== 'string') {
        x = x.toString();
    }
    //process.stdout.write(x);   // without auto '\n' (newline)
    console.log(x);  // with auto '\n' (newline)
}

function main() {
    let N = readline();

    for (let test = 0; test < N; test++) {
        let n = Number(readline());
        let arr = readline().split(' ').map(Number);

        let works = arr.map((val, index) => val < index + 1);

        let inLeft = new Array(n).fill(0);
        inLeft[inLeft.length - 1] = 0;
        for (let i = 0; i < n; i++) {
            inLeft[i] = (inLeft[i - 1] || 0) + (works[i - 1] ? 1 : 0);
        }

        let total = 0;
        for (let i = 1; i < n; i++) {
            if (arr[i] < i + 1) {
                total += inLeft[Math.max(Math.min(arr[i] - 1, i), 0)];
            }
        }

        output(total);
    }
}

var t = parseInt(readline());

for (var l = 0; l < t; l++) {
    var n = parseInt(readline());
    var arr = readline().split(' ');

    var part = [0];
    var ans = 0;

    for (var i = 0; i < arr.length; i++) {
        var isLess = arr[i] < i + 1;

        part.push(part[i] + isLess);

        if (isLess && arr[i] !== 0) {
            ans += part[arr[i] - 1];
        }
    }

    print(ans);
}

var t = parseInt(readline());

for (var l = 0; l < t; l++) {
    var n = parseInt(readline());
    var arr = readline().split(' ');

    var part = [0];
    var ans = 0;

    for (var i = 0; i < arr.length; i++) {
        var isLess = arr[i] < i + 1;

        part.push(part[i] + isLess);

        if (isLess) {
            ans += part[arr[i] - 1];
        }
    }

    print(ans);
}

var t = parseInt(readline());

for (var l = 0; l < t; l++) {
    var n = parseInt(readline());
    var arr = readline().split(' ');

    var part = [0];
    var ans = 0;

    for (var i = 0; i < arr.length; i++) {
        var isLess = arr[i] < i + 1;

        part.push(part[i] + isLess);

        if (isLess) {
            ans += part[arr[i]];
        }
    }

    print(ans);
}

var t = parseInt(readline());

for (var l = 0; l < t; l++) {
    var n = parseInt(readline());
    var arr = readline().split(' ');

    var part = [0];
    var ans = 0;

    for (var i = 0; i < arr.length; i++) {
        if (arr[i] < i + 1) {
            part.push(part[i] + 1);
            ans += part[arr[i]];
        }
    }

    print(ans);
}

var t = parseInt(readline());

for (var l = 0; l < t; l++) {
    var n = parseInt(readline());
    var arr = readline().split(' ');

    var part = [0];
    var ans = 0;

    var isLess = arr[i] < i + 1;

    for (var i = 0; i < arr.length; i++) {
        part.push(part[i] + isLess);
    }

    for (var i = 0; i < arr.length; i++) {
        if (isLess) {
            ans += part[arr[i]];
        }
    }

    print(ans);
}

var t = parseInt(readline());

for (var l = 0; l < t; l++) {
    var n = parseInt(readline());
    var arr = readline().split(' ');

    var part = [0];
    var ans = 0;

    var isLess = arr[i] < i + 1;

    for (var i = 0; i < arr.length; i++) {
        part.push(part[i] + isLess);

        if (isLess) {
            ans += part[arr[i]];
        }
    }

    print(ans);
}