100 1
100 50 99 49 98 48 97 47 96 46 95 45 94 44 93 43 92 42 91 41 90 40 89 39 88 38 87 37 86 36 85 35 84 34 83 33 82 32 81 31 80 30 79 29 78 28 77 27 76 26 75 25 74 24 73 23 72 22 71 21 70 20 69 19 68 18 67 17 66 16 65 15 64 14 63 13 62 12 61 11 60 10 59 9 58 8 57 7 56 6 55 5 54 4 53 3 52 2 51 1
9 8 99 93
 2 9
1 2
1 1 1 1
1 1 1 2
1 2 1 2
1 1 2 1
1 1 2 2
1 2 2 2
2 1 2 1
2 1 2 2
2 2 2 2
 10 10
6 5 7 4 8 3 9 2 10 1
8 8 8 8
2 1 2 7
8 4 8 4
3 9 3 9
7 7 7 7
2 10 2 10
6 8 9 8
1 4 1 4
1 5 4 5
8 10 8 10
 10 10
1 10 2 9 3 8 4 7 5 6
4 3 7 10
3 6 8 7
3 2 6 4
1 1 2 8
3 8 10 9
7 3 8 9
4 5 5 9
2 5 6 7
2 5 6 7
1 7 6 10
 10 5
5 10 4 9 3 8 2 7 1 6
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
 10 10
10 9 8 7 6 5 4 3 2 1
8 8 8 8
2 1 2 7
8 4 8 4
3 9 3 9
7 7 7 7
2 10 2 10
6 8 9 8
1 4 1 4
1 5 4 5
8 10 8 10
 7 7
1 2 3 6 7 4 5
3 2 4 6
3 2 5 4
3 6 7 7
1 1 4 4
2 2 7 6
1 2 6 2
1 2 6 7
 10 10
6 1 7 2 8 3 9 4 10 5
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
 10 10
5 1 6 2 8 3 4 10 9 7
8 1 8 1
1 7 2 7
4 3 8 7
3 7 9 7
7 6 7 7
2 3 10 6
6 4 8 7
1 6 4 7
1 2 5 4
8 5 10 10
 10 10
5 6 4 7 3 8 2 9 1 10
8 8 8 8
2 1 2 7
8 4 8 4
3 9 3 9
7 7 7 7
2 10 2 10
6 8 9 8
1 4 1 4
1 5 4 5
8 10 8 10
 10 10
10 5 9 4 8 3 7 2 6 1
8 1 8 1
1 7 2 7
4 3 8 7
3 7 9 7
7 6 7 7
2 3 10 6
6 4 8 7
1 6 4 7
1 2 5 4
8 5 10 10
 10 10
7 5 1 6 10 3 4 8 9 2
4 3 7 10
3 6 8 7
3 2 6 4
1 1 2 8
3 8 10 9
7 3 8 9
4 5 5 9
2 5 6 7
2 5 6 7
1 7 6 10
 10 10
1 2 3 4 5 6 7 8 9 10
8 8 8 8
2 1 2 7
8 4 8 4
3 9 3 9
7 7 7 7
2 10 2 10
6 8 9 8
1 4 1 4
1 5 4 5
8 10 8 10
 10 10
5 1 6 2 8 3 4 10 9 7
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
 10 10
9 8 3 4 5 6 7 2 1 10
8 1 8 1
1 7 2 7
4 3 8 7
3 7 9 7
7 6 7 7
2 3 10 6
6 4 8 7
1 6 4 7
1 2 5 4
8 5 10 10
 10 10
10 1 9 2 8 3 7 4 6 5
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
1 1 10 10
 10 10
5 1 6 2 8 3 4 10 9 7
9 5 9 5
4 7 4 7
4 8 5 8
9 1 9 4
4 1 4 6
10 9 10 9
3 5 6 5
3 4 10 4
3 2 5 2
7 8 7 8
 6 10
3 6 1 2 5 4
2 6 2 6
2 3 2 5
6 2 6 2
1 5 1 5
3 5 3 5
2 2 2 4
2 4 5 4
5 4 5 4
1 5 6 5
6 2 6 2
 10 10
1 6 2 7 3 8 4 9 5 10
8 8 8 8
2 1 2 7
8 4 8 4
3 9 3 9
7 7 7 7
2 10 2 10
6 8 9 8
1 4 1 4
1 5 4 5
8 10 8 10

                                                            4886
                                                                1
                                                               1
                                                               1
                                                               1
                                                               1
                                                               1
                                                               1
                                                               1
                                                               1
                                                               11
                                                              17
                                                              15
                                                               6
                                                              16
                                                               2
                                                              19
                                                               4
                                                              20
                                                               8
                                                               39
                                                              30
                                                              26
                                                              16
                                                              23
                                                              29
                                                              27
                                                              30
                                                              30
                                                              27
                                                               45
                                                              45
                                                              45
                                                              45
                                                              45
                                                                9
                                                              14
                                                              21
                                                              16
                                                              16
                                                               9
                                                              15
                                                               4
                                                              20
                                                               3
                                                               17
                                                              16
                                                              11
                                                              16
                                                              21
                                                              11
                                                              21
                                                               45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                                3
                                                               8
                                                              37
                                                              27
                                                              21
                                                              38
                                                              29
                                                              21
                                                              23
                                                              24
                                                               16
                                                              17
                                                              12
                                                               6
                                                              16
                                                               2
                                                              23
                                                               4
                                                              20
                                                               8
                                                                8
                                                              10
                                                              38
                                                              26
                                                              21
                                                              38
                                                              31
                                                              20
                                                              21
                                                              19
                                                               38
                                                              30
                                                              25
                                                              17
                                                              24
                                                              28
                                                              29
                                                              32
                                                              32
                                                              25
                                                               23
                                                              17
                                                              12
                                                               6
                                                              27
                                                               2
                                                              23
                                                               7
                                                              24
                                                               8
                                                               45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                                8
                                                              14
                                                              38
                                                              27
                                                              19
                                                              38
                                                              29
                                                              20
                                                              23
                                                              19
                                                               45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                              45
                                                               10
                                                              16
                                                              17
                                                               8
                                                              27
                                                               2
                                                              25
                                                              26
                                                              16
                                                              16
                                                                5
                                                               9
                                                               2
                                                               2
                                                               6
                                                               8
                                                              11
                                                               8
                                                               9
                                                               2
                                                               16
                                                              17
                                                              12
                                                               6
                                                              16
                                                               2
                                                              23
                                                               7
                                                              18
                                                               8

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 200007;
const int MAXS = 2000007;
int OK[3 * 3 * 3 * 3];
class ftT {
 public:
  void upd(int p, int x) {
    for (; p < MAXN; p += p & -p) data[p] += x;
  }
  int sum(int p) {
    int ans = 0;
    for (; p; p ^= p & -p) ans += data[p];
    return ans;
  }

 private:
  int data[MAXN];
} ft;
struct qT {
  int l, d, r, u;
} que[MAXN];
struct oT {
  void setd(int _id, int _x, int _y) { id = _id, x = _x, y = _y; }
  int id, x, y, ans;
} nq[MAXS];
int n, q;
int nk;
int cp[MAXN], p[MAXN];
int nt[3][3];
void init();
void input();
void work();
void presolve();
int main() {
  init();
  input();
  work();
}
void init() {
  ios::sync_with_stdio(false);
  for (int a = 0; a < 3; ++a) {
    for (int b = 0; b < 3; ++b) {
      for (int c = 0; c < 3; ++c) {
        for (int d = 0; d < 3; ++d) {
          if (a * 3 + b < c * 3 + d) {
            if (((((a) < (c)) ? (a) : (c)) <= 1 &&
                 (((a) > (c)) ? (a) : (c)) >= 1) &&
                ((((b) < (d)) ? (b) : (d)) <= 1 &&
                 (((b) > (d)) ? (b) : (d)) >= 1)) {
              OK[a * 27 + b * 9 + c * 3 + d] = 1;
            }
          }
        }
      }
    }
  }
}
void input() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; ++i) scanf("%d", &cp[i]);
  for (int i = 1; i <= q; ++i)
    scanf("%d%d%d%d", &que[i].l, &que[i].d, &que[i].r, &que[i].u);
}
void work() {
  for (int i = 1; i <= n; ++i) p[cp[i]] = i;
  nk = 0;
  int nx, ny;
  for (int i = 1; i <= q; ++i) {
    for (int j = 0; j < 3; ++j) {
      nx = (j == 0) ? (que[i].d - 1) : (j == 1) ? (que[i].u) : n;
      for (int k = 0; k < 3; ++k) {
        ny = (k == 0) ? (que[i].l - 1) : (k == 1) ? (que[i].r) : n;
        nq[++nk].setd(nk, nx, ny);
      }
    }
  }
  presolve();
  nk = 0;
  long long ans = 0;
  int na, nb, nc, nd;
  for (int i = 1; i <= q; ++i) {
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 3; ++k) {
        nt[j][k] = nq[++nk].ans;
      }
    }
    for (int a = 0; a < 3; ++a) {
      na = a * 27;
      for (int b = 0; b < 3; ++b) {
        nb = na + b * 9;
        for (int c = 0; c < 3; ++c) {
          nc = nb + c * 3;
          for (int d = 0; d < 3; ++d) {
            nd = nc + d;
            if (c <= a && d <= b && ((a ^ c) || (b ^ d))) {
              nt[a][b] -= nt[c][d];
            }
          }
        }
      }
    }
    ans = 0;
    for (int a = 0; a < 3; ++a) {
      na = a * 27;
      for (int b = 0; b < 3; ++b) {
        nb = na + b * 9;
        for (int c = 0; c < 3; ++c) {
          nc = nb + c * 3;
          for (int d = 0; d < 3; ++d) {
            nd = nc + d;
            if (OK[nd]) {
              ans += static_cast<long long>(nt[a][b]) * nt[c][d];
            }
          }
        }
      }
    }
    ans += static_cast<long long>(nt[1][1]) * (nt[1][1] - 1) >> 1;
    printf("%I64d\n", ans);
  }
}
void presolve() {
  sort(nq + 1, nq + 1 + nk, [](const oT &a, const oT &b) {
    return (a.x ^ b.x) ? (a.x < b.x) : (a.y < b.y);
  });
  int iq = 1;
  for (; iq <= nk && nq[iq].x == 0; ++iq)
    ;
  for (int i = 1; i <= n; ++i) {
    ft.upd(p[i], 1);
    for (; iq <= nk && nq[iq].x == i; ++iq) {
      nq[iq].ans = ft.sum(nq[iq].y);
    }
  }
  sort(nq + 1, nq + 1 + nk,
       [](const oT &a, const oT &b) { return a.id < b.id; });
}
