#include <bits/stdc++.h>
const int maxn = 1000005;
const int mod = 998244353;
int res, ans, n;
int fac[maxn], inv[maxn];
void update(int &x, int y) {
  x += y;
  if (x < 0) {
    x += mod;
  }
  if (x >= mod) {
    x -= mod;
  }
}
int mpow(int x, int y) {
  update(x, 0);
  int z = 1;
  for (; y; y >>= 1) {
    if (y & 1) {
      z = 1ll * z * x % mod;
    }
    x = 1ll * x * x % mod;
  }
  return z;
}
int calc(int x, int y) {
  return 1ll * fac[x] * inv[y] % mod * inv[x - y] % mod;
}
int main() {
  scanf("%d", &n);
  fac[0] = 1, inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = 1ll * fac[i - 1] * i % mod;
    inv[i] = mpow(fac[i], mod - 2);
  }
  for (int i = 1; i <= n; i++) {
    int a = 1ll * calc(n, i) * mpow(-1, i + 1) % mod;
    int x = mpow(3, (1ll * n * (n - i) + i) % (mod - 1));
    update(ans, 1ll * a * x % mod);
  }
  ans = 2 * ans % mod;
  for (int i = 0; i < n; i++) {
    int t = mod - mpow(3, i);
    int x = (mpow(t + 1, n) + mod - mpow(t, n)) % mod;
    int a = 1ll * calc(n, i) * mpow(-1, i + 1) % mod;
    update(res, 1ll * a * x % mod);
  }
  res = 3ll * res % mod;
  printf("%d\n", (ans + res) % mod);
  return 0;
}
