#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void Read(T &x) {
  x = 0;
  int s = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') s = -1;
    ch = getchar();
  };
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  }
  x *= s;
}
const int mod = 998244353;
const int N = 1e6 + 10;
long long n, ans, f[N];
long long ksm(long long x, long long y) {
  long long ret = 1;
  while (y) {
    if (y & 1) ret = (ret * x) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return ret;
}
void init() {
  Read(n);
  long long ans =
      ((ksm(3, n * n) - ksm((ksm(3, n) - 3 + mod) % mod, n) % mod) * 2) % mod;
  f[0] = f[1] = 1;
  for (int i = 2; i <= n; i++) f[i] = (f[i - 1] * i) % mod;
  for (int i = 1; i <= n; i++) {
    long long con =
        (f[n] * ksm(f[i], mod - 2)) % mod * ksm(f[n - i], mod - 2) % mod;
    if (i % 2 == 1) {
      ans =
          ((ans -
            ((ksm(3, (n - i) * n) - ksm(((ksm(3, n - i) - 1) % mod + mod), n)) *
             3 % mod * con % mod)) %
               mod +
           mod) %
          mod;
    } else {
      ans =
          (ans +
           ((ksm(3, (n - i) * n) - ksm(((ksm(3, n - i) - 1) % mod + mod), n)) *
            3 % mod * con % mod)) %
          mod;
    }
  }
  printf("%lld", (ans % mod + mod) % mod);
}
int main() {
  init();
  return 0;
}
