#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
const int mod = 998244353;
int n;
long long jc[N], inv[N], ans = 0, an = 0;
long long poww(long long x, long long y) {
  long long ans = 1;
  while (y) {
    if (y & 1) {
      ans = ans * x % mod;
    }
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long C(long long n, long long m) {
  return jc[n] * inv[m] % mod * inv[n - m] % mod;
}
int main() {
  scanf("%d", &n);
  jc[0] = inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    jc[i] = jc[i - 1] * i % mod;
    inv[i] = poww(jc[i], mod - 2);
  }
  for (int i = 1; i <= n; i++) {
    ans = (ans +
           poww(3, 1ll * n * (n - i) + i) * C(n, i) % mod * ((i & 1) ? 1 : -1) +
           mod) %
          mod;
  }
  ans = ans * 2 % mod;
  for (int i = 0; i < n; i++) {
    long long t = -poww(3, i);
    an = (an +
          C(n, i) * ((i & 1) ? 1 : -1) * (poww(t + 1, n) - poww(t, n) + mod) %
              mod +
          mod) %
         mod;
  }
  ans = (ans + 3 * an) % mod;
  cout << ans;
}
