#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, mod = 998244353;
long long jc[N] = {1}, ny[N];
void Add(long long &x, long long y) { x = (x + y + mod) % mod; }
long long q_pow(long long x, long long y) {
  long long ans = 1;
  for (; y; y >>= 1) {
    if (y & 1) ans = ans * x % mod;
    x = x * x % mod;
  }
  return ans;
}
long long C(int x, int y) { return jc[x] * ny[y] % mod * ny[x - y] % mod; }
int main() {
  long long n;
  scanf("%lld", &n);
  ny[0] = 1;
  for (int i = 1; i <= n; ++i)
    jc[i] = jc[i - 1] * i % mod, ny[i] = q_pow(jc[i], mod - 2);
  long long sum = 0;
  long long kk = -1;
  for (int i = 1; i <= n; ++i) {
    kk = -kk;
    Add(sum, kk * C(n, i) % mod * q_pow(3, n * n - i * n + i) % mod);
  }
  Add(sum, sum);
  kk = 1;
  for (int i = 1; i <= n; ++i) {
    kk = -kk;
    long long pos =
        (q_pow(3, n * (n - i)) - q_pow(q_pow(3, n - i) - 1, n) + mod) % mod;
    long long tmp = kk * C(n, i) % mod * 3LL % mod * pos % mod;
    Add(sum, tmp);
  }
  printf("%lld\n", sum);
  return 0;
}
