Language: C++

/* by Natsu Kinmoe */
#include <bits/stdc++.h>
using namespace std;
#define SZ(x)               ((int)(x).size())
#define all(x)              (x).begin(), (x).end()
#define loop(i, n)          for(int i = 0; i < (n); i++)
#define cont(i, n)          for(int i = 1; i <= (n); i++)
#define circ(i, a, b)       for(int i = (a); i <= (b); i++)
#define range(i, a, b, c)   for(int i = (a); ((c) > 0 ? i <= (b) : i >= (b)); i += (c))
#define foreach(it, v)      for(__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define y0          y0O0OO00OO0OO0OO0OOO00OO0OO0O0O000OO0
#define y1          y1II11II11III11I1III11II111IIII1II1I1
#define pub         push_back
#define pob         pop_back
#define mak         make_pair
typedef long long   ll;
typedef long double lf;
const int   Inf = 0x3f3f3f3f;
const ll    INF = 0x3f3f3f3f3f3f3f3fll;
/* Source code starts here */

int m;
vector<int> a[1 << 20], dy[1 << 20];
int cnt[1 << 20], pr[1 << 20];
bool lgl[1 << 20];

int main() {
	scanf("%d", &m);
	loop(i, n) a[i].resize(m);
	loop(i, n) loop(j, m) scanf("%d", &a[i][j]);
	cont(i, n - 1) {
		loop(j, m) cnt[a[i - 1][j]]++, cnt[a[i][j]]++, pr[a[i - 1][j]]++;
		loop(j, m) if(cnt[a[i][j]] > m) goto invalid;
		priority_queue<pair<int, int> > pq;
		deque<int> as; loop(j, m) as.pub(a[i][j]);
		sort(all(as));
		loop(j, m) if(!lgl[a[i - 1][j]]) {
			lgl[a[i - 1][j]] = 1;
			pq.push(mak(cnt[a[i - 1][j]], a[i - 1][j]));
		}
		loop(j, m) lgl[a[i - 1][j]] = 0;
		while(SZ(pq)) {
			pair<int, int> X = pq.top(); pq.pop();
			int ts = X.first, x = X.second;
			if(cnt[x] != ts) continue;
			if(!pr[x]) continue;
			int sel;
			if(as.front() != x) sel = as.front(), as.pop_front();
			else if(as.back() != x) sel = as.back(), as.pob();
			cnt[x]--; cnt[sel]--; pr[x]--; dy[x].pub(sel);
			if(cnt[x]) pq.push(mak(cnt[x], x));
			if(cnt[sel]) pq.push(mak(cnt[sel], sel));
		}
		loop(j, m) a[i][j] = dy[a[i - 1][j]].back(), dy[a[i - 1][j]].pob();
	}
	puts("Yes");
	loop(i, n) loop(j, m) printf("%d%c", a[i][j], " \n"[j == m - 1]);
	return 0;

	invalid:
	return 0 & puts("No");
}


Compiler output:
a.cc: In function 'int main()':
a.cc:29:17: error: 'n' was not declared in this scope
   29 |         loop(i, n) a[i].resize(m);
      |                 ^
a.cc:6:49: note: in definition of macro 'loop'
    6 | #define loop(i, n)          for(int i = 0; i < (n); i++)
      |                                                 ^
a.cc:30:17: error: 'n' was not declared in this scope
   30 |         loop(i, n) loop(j, m) scanf("%d", &a[i][j]);
      |                 ^
a.cc:6:49: note: in definition of macro 'loop'
    6 | #define loop(i, n)          for(int i = 0; i < (n); i++)
      |                                                 ^
a.cc:31:17: error: 'n' was not declared in this scope
   31 |         cont(i, n - 1) {
      |                 ^
a.cc:7:50: note: in definition of macro 'cont'
    7 | #define cont(i, n)          for(int i = 1; i <= (n); i++)
      |                                                  ^
a.cc:57:17: error: 'n' was not declared in this scope
   57 |         loop(i, n) loop(j, m) printf("%d%c", a[i][j], " \n"[j == m - 1]);
      |                 ^
a.cc:6:49: note: in definition of macro 'loop'
    6 | #define loop(i, n)          for(int i = 0; i < (n); i++)
      |                                                 ^