#include <bits/stdc++.h>
int n, fac[1000005], inv[1000005], ans;
int qpow(int a, long long b) {
  int s = 1;
  for (; b; b >>= 1, a = 1ll * a * a % 998244353)
    if (b & 1) s = 1ll * s * a % 998244353;
  return s;
}
int C(int n, int m) {
  return 1ll * fac[n] * inv[n - m] % 998244353 * inv[m] % 998244353;
}
int add(int x, int y) { return (x += y) >= 998244353 ? x -= 998244353 : x; }
int sub(int x, int y) { return (x -= y) < 0 ? x += 998244353 : x; }
int main() {
  scanf("%d", &n), fac[0] = 1;
  for (register int i = 1; i <= n; ++i)
    fac[i] = 1ll * fac[i - 1] * i % 998244353;
  inv[n] = qpow(fac[n], 998244353 - 2);
  for (register int i = n; i; --i) inv[i - 1] = 1ll * inv[i] * i % 998244353;
  ans = sub(qpow(3, 1ll * n * n), qpow(sub(qpow(3, n), 3), n));
  for (register int i = 1; i <= n; ++i) {
    int s =
        1ll * C(n, i) *
        add(3ll * qpow(qpow(3, n - i) - 1, n) % 998244353,
            1ll * sub(qpow(3, i), 3) * qpow(3, 1ll * n * (n - i)) % 998244353) %
        998244353;
    if (i & 1)
      ans = add(ans, s);
    else
      ans = sub(ans, s);
  }
  printf("%d\n", ans);
}
