#include <bits/stdc++.h>
using namespace std;
long long n, m, i, j, k, s, t, l, r, x, mid, y, f, ans, v, mod = 998244353;
long long a[1000010], b[1000010];
long long ksm(long long x, long long y) {
  long long temp = 1;
  y = y % (mod - 1);
  while (y > 0) {
    if (y % 2 == 1) temp = temp * x % mod;
    x = x * x % mod;
    y = y / 2;
  }
  return temp;
}
int main() {
  scanf("%lld", &n);
  a[0] = a[1] = 1;
  for (i = 2; i <= 1000000; i++) a[i] = a[i - 1] * i % mod;
  b[0] = b[1] = 1;
  for (i = 2; i <= 1000000; i++) b[i] = b[i - 1] * ksm(i, mod - 2) % mod;
  ans = 0;
  v = -1;
  for (i = 1; i <= n; i++) {
    v = -v;
    t = a[n] * b[i] % mod;
    t = t * b[n - i] % mod;
    t = t * ksm(3, i + n * n - n * i) % mod;
    t = t * 2 * v % mod;
    ans = (ans + t) % mod;
    t = a[n] * b[i] % mod;
    t = t * b[n - i] % mod;
    t = t * ksm(3, n * n - i * n + 1) % mod;
    ans = (ans - v * t) % mod;
  }
  v = -1;
  for (i = 1; i <= n; i++) {
    v = -v;
    t = a[n] * b[i] % mod;
    t = t * b[n - i] % mod;
    s = (ksm(3, n - i) - 1) % mod;
    s = ksm(s, n) % mod;
    s = s * 3 % mod;
    t = t * s * v % mod;
    ans = (ans + t) % mod;
  }
  ans = ans % mod;
  while (ans < 0) ans += mod;
  printf("%lld\n", ans);
}
